Skip to content

Commit b4ccc97

Browse files
authored
Build rust_test targets using a crate name different from the underlying lib (#2803)
This PR also makes `rust_test` put its compilation outputs in the same directory as the `rust_library` rule (i.e. not in a `test-{hash}` subdirectory anymore). After this change both the `rust_library` and `rust_test` rules will put all its compilation outputs in the same directory, but there won't be any name collisions in non-sandboxed environments (see #1427 for more context). This is a partial rollback of 1018533 and 26344d4.
1 parent dec889f commit b4ccc97

File tree

8 files changed

+10
-99
lines changed

8 files changed

+10
-99
lines changed

docs/defs.md

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

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.
570+
Run the test with `bazel test //hello_lib:hello_lib_test`.
573571

574572
### Example: `test` directory
575573

docs/flatten.md

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

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.
1053+
Run the test with `bazel test //hello_lib:hello_lib_test`.
10561054

10571055
### Example: `test` directory
10581056

rust/private/rust.bzl

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ 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)
299300
crate_type = "bin"
300301
deps = transform_deps(ctx.attr.deps)
301302
proc_macro_deps = transform_deps(ctx.attr.proc_macro_deps + get_import_macro_deps(ctx))
@@ -309,13 +310,8 @@ def _rust_test_impl(ctx):
309310
# Target is building the crate in `test` config
310311
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
311312

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

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

343339
# Build the test binary using the dependency's srcs.
344340
crate_info_dict = dict(
345-
name = crate.name,
341+
name = crate_name,
346342
type = crate_type,
347343
root = crate.root,
348344
srcs = depset(srcs, transitive = [crate.srcs]),
@@ -368,13 +364,8 @@ def _rust_test_impl(ctx):
368364
crate_root = crate_root_src(ctx.attr.name, ctx.files.srcs, crate_root_type)
369365
srcs, crate_root = transform_sources(ctx, ctx.files.srcs, crate_root)
370366

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

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

387378
# Target is a standalone crate. Build the test binary as its own crate.
388379
crate_info_dict = dict(
389-
name = compute_crate_name(ctx.workspace_name, ctx.label, toolchain, ctx.attr.crate_name),
380+
name = crate_name,
390381
type = crate_type,
391382
root = crate_root,
392383
srcs = depset(srcs),
@@ -1342,9 +1333,7 @@ rust_test = rule(
13421333
)
13431334
```
13441335
1345-
Run the test with `bazel test //hello_lib:hello_lib_test`. The crate
1346-
will be built using the same crate name as the underlying ":hello_lib"
1347-
crate.
1336+
Run the test with `bazel test //hello_lib:hello_lib_test`.
13481337
13491338
### Example: `test` directory
13501339

test/rust/src/lib.rs

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

15-
#![crate_name = "hello_lib"]
1615
pub mod greeter;

test/unit/linkstamps/linkstamps_test.bzl

Lines changed: 2 additions & 6 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_common", "rust_library", "rust_test")
5+
load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
66
load("//test/unit:common.bzl", "assert_action_mnemonic")
77

88
def _is_running_on_linux(ctx):
@@ -20,13 +20,9 @@ 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
2423
workspace_prefix = "" if ctx.workspace_name == "rules_rust" else "/external/rules_rust"
2524

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"
25+
expected_linkstamp_path = tut_out.dirname + "/_objs/" + tut_out.basename + workspace_prefix + "/test/unit/linkstamps/linkstamp.o"
3026
asserts.equals(
3127
env,
3228
linkstamp_out.path,

test/unit/rust_test_outputs_are_in_subdirectory/BUILD.bazel

Lines changed: 0 additions & 4 deletions
This file was deleted.

test/unit/rust_test_outputs_are_in_subdirectory/foo.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/unit/rust_test_outputs_are_in_subdirectory/rust_test_outputs.bzl

Lines changed: 0 additions & 64 deletions
This file was deleted.

0 commit comments

Comments
 (0)