Skip to content

Commit ef8ac18

Browse files
authored
Revert "Build rust_test targets using a crate name different from the underlying lib" (#2815)
This reverts commit b4ccc97. That commit makes a behavior change, and thus should have been protected by a feature flag. This will be done in a future PR.
1 parent dff064e commit ef8ac18

File tree

8 files changed

+99
-10
lines changed

8 files changed

+99
-10
lines changed

docs/defs.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,9 @@ rust_test(
567567
)
568568
```
569569

570-
Run the test with `bazel test //hello_lib:hello_lib_test`.
570+
Run the test with `bazel test //hello_lib:hello_lib_test`. The crate
571+
will be built using the same crate name as the underlying ":hello_lib"
572+
crate.
571573

572574
### Example: `test` directory
573575

docs/flatten.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,9 @@ rust_test(
10501050
)
10511051
```
10521052

1053-
Run the test with `bazel test //hello_lib:hello_lib_test`.
1053+
Run the test with `bazel test //hello_lib:hello_lib_test`. The crate
1054+
will be built using the same crate name as the underlying ":hello_lib"
1055+
crate.
10541056

10551057
### Example: `test` directory
10561058

rust/private/rust.bzl

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ def _rust_test_impl(ctx):
296296

297297
toolchain = find_toolchain(ctx)
298298

299-
crate_name = compute_crate_name(ctx.workspace_name, ctx.label, toolchain, ctx.attr.crate_name)
300299
crate_type = "bin"
301300
deps = transform_deps(ctx.attr.deps)
302301
proc_macro_deps = transform_deps(ctx.attr.proc_macro_deps + get_import_macro_deps(ctx))
@@ -310,8 +309,13 @@ def _rust_test_impl(ctx):
310309
# Target is building the crate in `test` config
311310
crate = ctx.attr.crate[rust_common.crate_info] if rust_common.crate_info in ctx.attr.crate else ctx.attr.crate[rust_common.test_crate_info].crate
312311

312+
output_hash = determine_output_hash(crate.root, ctx.label)
313313
output = ctx.actions.declare_file(
314-
ctx.label.name + toolchain.binary_ext,
314+
"test-%s/%s%s" % (
315+
output_hash,
316+
ctx.label.name,
317+
toolchain.binary_ext,
318+
),
315319
)
316320

317321
srcs, crate_root = transform_sources(ctx, ctx.files.srcs, getattr(ctx.file, "crate_root", None))
@@ -338,7 +342,7 @@ def _rust_test_impl(ctx):
338342

339343
# Build the test binary using the dependency's srcs.
340344
crate_info_dict = dict(
341-
name = crate_name,
345+
name = crate.name,
342346
type = crate_type,
343347
root = crate.root,
344348
srcs = depset(srcs, transitive = [crate.srcs]),
@@ -364,8 +368,13 @@ def _rust_test_impl(ctx):
364368
crate_root = crate_root_src(ctx.attr.name, ctx.files.srcs, crate_root_type)
365369
srcs, crate_root = transform_sources(ctx, ctx.files.srcs, crate_root)
366370

371+
output_hash = determine_output_hash(crate_root, ctx.label)
367372
output = ctx.actions.declare_file(
368-
ctx.label.name + toolchain.binary_ext,
373+
"test-%s/%s%s" % (
374+
output_hash,
375+
ctx.label.name,
376+
toolchain.binary_ext,
377+
),
369378
)
370379

371380
data_paths = depset(direct = getattr(ctx.attr, "data", [])).to_list()
@@ -377,7 +386,7 @@ def _rust_test_impl(ctx):
377386

378387
# Target is a standalone crate. Build the test binary as its own crate.
379388
crate_info_dict = dict(
380-
name = crate_name,
389+
name = compute_crate_name(ctx.workspace_name, ctx.label, toolchain, ctx.attr.crate_name),
381390
type = crate_type,
382391
root = crate_root,
383392
srcs = depset(srcs),
@@ -1339,7 +1348,9 @@ rust_test = rule(
13391348
)
13401349
```
13411350
1342-
Run the test with `bazel test //hello_lib:hello_lib_test`.
1351+
Run the test with `bazel test //hello_lib:hello_lib_test`. The crate
1352+
will be built using the same crate name as the underlying ":hello_lib"
1353+
crate.
13431354
13441355
### Example: `test` directory
13451356

test/rust/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#![crate_name = "hello_lib"]
1516
pub mod greeter;

test/unit/linkstamps/linkstamps_test.bzl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
44
load("@rules_cc//cc:defs.bzl", "cc_library")
5-
load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
5+
load("//rust:defs.bzl", "rust_binary", "rust_common", "rust_library", "rust_test")
66
load("//test/unit:common.bzl", "assert_action_mnemonic")
77

88
def _is_running_on_linux(ctx):
@@ -20,9 +20,13 @@ def _supports_linkstamps_test(ctx):
2020
linkstamp_out = linkstamp_action.outputs.to_list()[0]
2121
asserts.equals(env, linkstamp_out.basename, "linkstamp.o")
2222
tut_out = tut.files.to_list()[0]
23+
is_test = tut[rust_common.crate_info].is_test
2324
workspace_prefix = "" if ctx.workspace_name == "rules_rust" else "/external/rules_rust"
2425

25-
expected_linkstamp_path = tut_out.dirname + "/_objs/" + tut_out.basename + workspace_prefix + "/test/unit/linkstamps/linkstamp.o"
26+
# Rust compilation outputs coming from a test are put in test-{hash} directory
27+
# which we need to remove in order to obtain the linkstamp file path.
28+
dirname = "/".join(tut_out.dirname.split("/")[:-1]) if is_test else tut_out.dirname
29+
expected_linkstamp_path = dirname + "/_objs/" + tut_out.basename + workspace_prefix + "/test/unit/linkstamps/linkstamp.o"
2630
asserts.equals(
2731
env,
2832
linkstamp_out.path,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
load(":rust_test_outputs.bzl", "rust_test_outputs_test_suite")
2+
3+
############################ UNIT TESTS #############################
4+
rust_test_outputs_test_suite(name = "rust_test_outputs_test_suite")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub fn main() {}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""Tests for rust_test outputs directory."""
2+
3+
load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts")
4+
load("//rust:defs.bzl", "rust_binary", "rust_common", "rust_test")
5+
6+
def _rust_test_outputs_test(ctx):
7+
env = analysistest.begin(ctx)
8+
tut = analysistest.target_under_test(env)
9+
10+
output = tut[rust_common.crate_info].output
11+
12+
asserts.true(env, output.dirname.split("/")[-1].startswith("test-"))
13+
14+
return analysistest.end(env)
15+
16+
rust_test_outputs_test = analysistest.make(
17+
_rust_test_outputs_test,
18+
)
19+
20+
def _rust_test_outputs_targets():
21+
rust_binary(
22+
name = "bin_outputs",
23+
srcs = ["foo.rs"],
24+
edition = "2018",
25+
)
26+
27+
rust_test(
28+
name = "test_outputs_with_srcs",
29+
srcs = ["foo.rs"],
30+
edition = "2018",
31+
)
32+
33+
rust_test_outputs_test(
34+
name = "rust_test_outputs_using_srcs_attr",
35+
target_under_test = ":test_outputs_with_srcs",
36+
)
37+
38+
rust_test(
39+
name = "test_outputs_with_crate",
40+
crate = "bin_outputs",
41+
edition = "2018",
42+
)
43+
44+
rust_test_outputs_test(
45+
name = "rust_test_outputs_using_crate_attr",
46+
target_under_test = ":test_outputs_with_crate",
47+
)
48+
49+
def rust_test_outputs_test_suite(name):
50+
"""Entry-point macro called from the BUILD file.
51+
52+
Args:
53+
name: Name of the macro.
54+
"""
55+
56+
_rust_test_outputs_targets()
57+
58+
native.test_suite(
59+
name = name,
60+
tests = [
61+
":rust_test_outputs_using_srcs_attr",
62+
":rust_test_outputs_using_crate_attr",
63+
],
64+
)

0 commit comments

Comments
 (0)