Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build rust_test targets using a crate name different from the underlying lib #2828

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions docs/defs.md
Original file line number Diff line number Diff line change
Expand Up @@ -567,9 +567,7 @@ rust_test(
)
```

Run the test with `bazel test //hello_lib:hello_lib_test`. The crate
will be built using the same crate name as the underlying ":hello_lib"
crate.
Run the test with `bazel test //hello_lib:hello_lib_test`.

### Example: `test` directory

Expand Down
4 changes: 1 addition & 3 deletions docs/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -1050,9 +1050,7 @@ rust_test(
)
```

Run the test with `bazel test //hello_lib:hello_lib_test`. The crate
will be built using the same crate name as the underlying ":hello_lib"
crate.
Run the test with `bazel test //hello_lib:hello_lib_test`.

### Example: `test` directory

Expand Down
50 changes: 30 additions & 20 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,21 @@ def _rust_test_impl(ctx):
# Target is building the crate in `test` config
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

output_hash = determine_output_hash(crate.root, ctx.label)
output = ctx.actions.declare_file(
"test-%s/%s%s" % (
output_hash,
ctx.label.name,
toolchain.binary_ext,
),
)
if toolchain._incompatible_change_rust_test_compilation_output_directory:
crate_name = compute_crate_name(ctx.workspace_name, ctx.label, toolchain, ctx.attr.crate_name)
output = ctx.actions.declare_file(
ctx.label.name + toolchain.binary_ext,
)
else:
crate_name = crate.name
output_hash = determine_output_hash(crate.root, ctx.label)
output = ctx.actions.declare_file(
"test-%s/%s%s" % (
output_hash,
ctx.label.name,
toolchain.binary_ext,
),
)

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

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

# Build the test binary using the dependency's srcs.
crate_info_dict = dict(
name = crate.name,
name = crate_name,
type = crate_type,
root = crate.root,
srcs = depset(srcs, transitive = [crate.srcs]),
Expand All @@ -368,14 +375,19 @@ def _rust_test_impl(ctx):
crate_root = crate_root_src(ctx.attr.name, ctx.files.srcs, crate_root_type)
srcs, crate_root = transform_sources(ctx, ctx.files.srcs, crate_root)

output_hash = determine_output_hash(crate_root, ctx.label)
output = ctx.actions.declare_file(
"test-%s/%s%s" % (
output_hash,
ctx.label.name,
toolchain.binary_ext,
),
)
if toolchain._incompatible_change_rust_test_compilation_output_directory:
output = ctx.actions.declare_file(
ctx.label.name + toolchain.binary_ext,
)
else:
output_hash = determine_output_hash(crate_root, ctx.label)
output = ctx.actions.declare_file(
"test-%s/%s%s" % (
output_hash,
ctx.label.name,
toolchain.binary_ext,
),
)

data_paths = depset(direct = getattr(ctx.attr, "data", [])).to_list()
rustc_env = expand_dict_value_locations(
Expand Down Expand Up @@ -1348,9 +1360,7 @@ rust_test = rule(
)
```

Run the test with `bazel test //hello_lib:hello_lib_test`. The crate
will be built using the same crate name as the underlying ":hello_lib"
crate.
Run the test with `bazel test //hello_lib:hello_lib_test`.

### Example: `test` directory

Expand Down
7 changes: 7 additions & 0 deletions rust/settings/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ incompatible_flag(
issue = "https://github.com/bazelbuild/rules_rust/issues/2039",
)

# A flag to put rust_test compilation outputs in the same directory as the rust_library compilation outputs.
incompatible_flag(
name = "incompatible_change_rust_test_compilation_output_directory",
build_setting_default = False,
issue = "https://github.com/bazelbuild/rules_rust/issues/2827",
)

# A flag to control whether to link libstd dynamically.
bool_flag(
name = "experimental_link_std_dylib",
Expand Down
4 changes: 4 additions & 0 deletions rust/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ def _rust_toolchain_impl(ctx):
_experimental_use_global_allocator = experimental_use_global_allocator,
_experimental_use_coverage_metadata_files = ctx.attr._experimental_use_coverage_metadata_files[BuildSettingInfo].value,
_experimental_toolchain_generated_sysroot = ctx.attr._experimental_toolchain_generated_sysroot[IncompatibleFlagInfo].enabled,
_incompatible_change_rust_test_compilation_output_directory = ctx.attr._incompatible_change_rust_test_compilation_output_directory[IncompatibleFlagInfo].enabled,
_no_std = no_std,
)
return [
Expand Down Expand Up @@ -889,6 +890,9 @@ rust_toolchain = rule(
"This flag is only relevant when used together with --@rules_rust//rust/settings:experimental_use_global_allocator."
),
),
"_incompatible_change_rust_test_compilation_output_directory": attr.label(
default = Label("//rust/settings:incompatible_change_rust_test_compilation_output_directory"),
),
"_no_std": attr.label(
default = Label("//:no_std"),
),
Expand Down
1 change: 0 additions & 1 deletion test/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#![crate_name = "hello_lib"]
felipeamp marked this conversation as resolved.
Show resolved Hide resolved
pub mod greeter;
4 changes: 0 additions & 4 deletions test/unit/rust_test_outputs_are_in_subdirectory/BUILD.bazel
felipeamp marked this conversation as resolved.
Show resolved Hide resolved

This file was deleted.

1 change: 0 additions & 1 deletion test/unit/rust_test_outputs_are_in_subdirectory/foo.rs

This file was deleted.

This file was deleted.

Loading