Skip to content

Commit a99f026

Browse files
committed
Build rust_test targets using a crate name different from the
underlying lib This is a rollforward of bazelbuild#2803, but behind a feature flag. 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 bazelbuild#1427 for more context). Issue: bazelbuild#2827
1 parent ef8ac18 commit a99f026

File tree

9 files changed

+43
-96
lines changed

9 files changed

+43
-96
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: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,21 @@ def _rust_test_impl(ctx):
309309
# Target is building the crate in `test` config
310310
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
311311

312-
output_hash = determine_output_hash(crate.root, ctx.label)
313-
output = ctx.actions.declare_file(
314-
"test-%s/%s%s" % (
315-
output_hash,
316-
ctx.label.name,
317-
toolchain.binary_ext,
318-
),
319-
)
312+
if toolchain._incompatible_change_rust_test_compilation_output_directory:
313+
crate_name = compute_crate_name(ctx.workspace_name, ctx.label, toolchain, ctx.attr.crate_name)
314+
output = ctx.actions.declare_file(
315+
ctx.label.name + toolchain.binary_ext,
316+
)
317+
else:
318+
crate_name = crate.name
319+
output_hash = determine_output_hash(crate.root, ctx.label)
320+
output = ctx.actions.declare_file(
321+
"test-%s/%s%s" % (
322+
output_hash,
323+
ctx.label.name,
324+
toolchain.binary_ext,
325+
),
326+
)
320327

321328
srcs, crate_root = transform_sources(ctx, ctx.files.srcs, getattr(ctx.file, "crate_root", None))
322329

@@ -342,7 +349,7 @@ def _rust_test_impl(ctx):
342349

343350
# Build the test binary using the dependency's srcs.
344351
crate_info_dict = dict(
345-
name = crate.name,
352+
name = crate_name,
346353
type = crate_type,
347354
root = crate.root,
348355
srcs = depset(srcs, transitive = [crate.srcs]),
@@ -368,14 +375,19 @@ def _rust_test_impl(ctx):
368375
crate_root = crate_root_src(ctx.attr.name, ctx.files.srcs, crate_root_type)
369376
srcs, crate_root = transform_sources(ctx, ctx.files.srcs, crate_root)
370377

371-
output_hash = determine_output_hash(crate_root, ctx.label)
372-
output = ctx.actions.declare_file(
373-
"test-%s/%s%s" % (
374-
output_hash,
375-
ctx.label.name,
376-
toolchain.binary_ext,
377-
),
378-
)
378+
if toolchain._incompatible_change_rust_test_compilation_output_directory:
379+
output = ctx.actions.declare_file(
380+
ctx.label.name + toolchain.binary_ext,
381+
)
382+
else:
383+
output_hash = determine_output_hash(crate_root, ctx.label)
384+
output = ctx.actions.declare_file(
385+
"test-%s/%s%s" % (
386+
output_hash,
387+
ctx.label.name,
388+
toolchain.binary_ext,
389+
),
390+
)
379391

380392
data_paths = depset(direct = getattr(ctx.attr, "data", [])).to_list()
381393
rustc_env = expand_dict_value_locations(
@@ -1348,9 +1360,7 @@ rust_test = rule(
13481360
)
13491361
```
13501362
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.
1363+
Run the test with `bazel test //hello_lib:hello_lib_test`.
13541364
13551365
### Example: `test` directory
13561366

rust/settings/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ incompatible_flag(
9292
issue = "https://github.com/bazelbuild/rules_rust/issues/2039",
9393
)
9494

95+
# A flag to put rust_test compilation outputs in the same directory as the rust_library compilation outputs.
96+
incompatible_flag(
97+
name = "incompatible_change_rust_test_compilation_output_directory",
98+
build_setting_default = False,
99+
issue = "https://github.com/bazelbuild/rules_rust/issues/2827",
100+
)
101+
95102
# A flag to control whether to link libstd dynamically.
96103
bool_flag(
97104
name = "experimental_link_std_dylib",

rust/toolchain.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@ def _rust_toolchain_impl(ctx):
698698
_experimental_use_global_allocator = experimental_use_global_allocator,
699699
_experimental_use_coverage_metadata_files = ctx.attr._experimental_use_coverage_metadata_files[BuildSettingInfo].value,
700700
_experimental_toolchain_generated_sysroot = ctx.attr._experimental_toolchain_generated_sysroot[IncompatibleFlagInfo].enabled,
701+
_incompatible_change_rust_test_compilation_output_directory = ctx.attr._incompatible_change_rust_test_compilation_output_directory[IncompatibleFlagInfo].enabled,
701702
_no_std = no_std,
702703
)
703704
return [
@@ -889,6 +890,9 @@ rust_toolchain = rule(
889890
"This flag is only relevant when used together with --@rules_rust//rust/settings:experimental_use_global_allocator."
890891
),
891892
),
893+
"_incompatible_change_rust_test_compilation_output_directory": attr.label(
894+
default = Label("//rust/settings:incompatible_change_rust_test_compilation_output_directory"),
895+
),
892896
"_no_std": attr.label(
893897
default = Label("//:no_std"),
894898
),

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/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)