Skip to content

Commit

Permalink
Build rust_test targets using a crate name different from the under…
Browse files Browse the repository at this point in the history
…lying 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.
  • Loading branch information
felipeamp authored Aug 22, 2024
1 parent dec889f commit b4ccc97
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 99 deletions.
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
23 changes: 6 additions & 17 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ def _rust_test_impl(ctx):

toolchain = find_toolchain(ctx)

crate_name = compute_crate_name(ctx.workspace_name, ctx.label, toolchain, ctx.attr.crate_name)
crate_type = "bin"
deps = transform_deps(ctx.attr.deps)
proc_macro_deps = transform_deps(ctx.attr.proc_macro_deps + get_import_macro_deps(ctx))
Expand All @@ -309,13 +310,8 @@ 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,
),
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 +338,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,13 +364,8 @@ 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,
),
ctx.label.name + toolchain.binary_ext,
)

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

# Target is a standalone crate. Build the test binary as its own crate.
crate_info_dict = dict(
name = compute_crate_name(ctx.workspace_name, ctx.label, toolchain, ctx.attr.crate_name),
name = crate_name,
type = crate_type,
root = crate_root,
srcs = depset(srcs),
Expand Down Expand Up @@ -1342,9 +1333,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
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"]
pub mod greeter;
8 changes: 2 additions & 6 deletions test/unit/linkstamps/linkstamps_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

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

# Rust compilation outputs coming from a test are put in test-{hash} directory
# which we need to remove in order to obtain the linkstamp file path.
dirname = "/".join(tut_out.dirname.split("/")[:-1]) if is_test else tut_out.dirname
expected_linkstamp_path = dirname + "/_objs/" + tut_out.basename + workspace_prefix + "/test/unit/linkstamps/linkstamp.o"
expected_linkstamp_path = tut_out.dirname + "/_objs/" + tut_out.basename + workspace_prefix + "/test/unit/linkstamps/linkstamp.o"
asserts.equals(
env,
linkstamp_out.path,
Expand Down
4 changes: 0 additions & 4 deletions test/unit/rust_test_outputs_are_in_subdirectory/BUILD.bazel

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.

0 comments on commit b4ccc97

Please sign in to comment.