Skip to content

Commit 1018533

Browse files
authored
Build rust_test targets with crate using the same crate name as the underlying library target. (#1332)
This PR changes `rust_test` to use the same crate name as the underlying target, in cases where the `crate` attribute is used. This is consistent with `cargo`, which builds the crate using the name of the library when running `cargo test`. The behavior of `rust_test` targets that don't use the `crate` attribute is unchanged.
1 parent 0049ce3 commit 1018533

File tree

5 files changed

+21
-10
lines changed

5 files changed

+21
-10
lines changed

docs/defs.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,9 @@ rust_test(
482482
deps = ["//some/dev/dep"],
483483
```
484484

485-
Run the test with `bazel build //hello_lib:hello_lib_test`.
485+
Run the test with `bazel test //hello_lib:hello_lib_test`. The crate
486+
will be built using the same crate name as the underlying ":hello_lib"
487+
crate.
486488

487489
### Example: `test` directory
488490

@@ -533,7 +535,7 @@ rust_test(
533535
)
534536
```
535537

536-
Run the test with `bazel build //hello_lib:greeting_test`.
538+
Run the test with `bazel test //hello_lib:greeting_test`.
537539

538540
**ATTRIBUTES**
539541

docs/flatten.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,9 @@ rust_test(
969969
deps = ["//some/dev/dep"],
970970
```
971971

972-
Run the test with `bazel build //hello_lib:hello_lib_test`.
972+
Run the test with `bazel test //hello_lib:hello_lib_test`. The crate
973+
will be built using the same crate name as the underlying ":hello_lib"
974+
crate.
973975

974976
### Example: `test` directory
975977

@@ -1020,7 +1022,7 @@ rust_test(
10201022
)
10211023
```
10221024

1023-
Run the test with `bazel build //hello_lib:greeting_test`.
1025+
Run the test with `bazel test //hello_lib:greeting_test`.
10241026

10251027
**ATTRIBUTES**
10261028

rust/private/rust.bzl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,6 @@ def _rust_test_common(ctx, toolchain, output):
345345
_assert_no_deprecated_attributes(ctx)
346346
_assert_correct_dep_mapping(ctx)
347347

348-
crate_name = compute_crate_name(ctx.workspace_name, ctx.label, toolchain, ctx.attr.crate_name)
349348
crate_type = "bin"
350349

351350
deps = transform_deps(ctx.attr.deps)
@@ -363,7 +362,7 @@ def _rust_test_common(ctx, toolchain, output):
363362

364363
# Build the test binary using the dependency's srcs.
365364
crate_info = rust_common.create_crate_info(
366-
name = crate_name,
365+
name = crate.name,
367366
type = crate_type,
368367
root = crate.root,
369368
srcs = depset(ctx.files.srcs, transitive = [crate.srcs]),
@@ -381,7 +380,7 @@ def _rust_test_common(ctx, toolchain, output):
381380
else:
382381
# Target is a standalone crate. Build the test binary as its own crate.
383382
crate_info = rust_common.create_crate_info(
384-
name = crate_name,
383+
name = compute_crate_name(ctx.workspace_name, ctx.label, toolchain, ctx.attr.crate_name),
385384
type = crate_type,
386385
root = crate_root_src(ctx.attr, ctx.files.srcs, "lib"),
387386
srcs = depset(ctx.files.srcs),
@@ -1063,7 +1062,9 @@ rust_test = rule(
10631062
deps = ["//some/dev/dep"],
10641063
```
10651064
1066-
Run the test with `bazel build //hello_lib:hello_lib_test`.
1065+
Run the test with `bazel test //hello_lib:hello_lib_test`. The crate
1066+
will be built using the same crate name as the underlying ":hello_lib"
1067+
crate.
10671068
10681069
### Example: `test` directory
10691070
@@ -1117,7 +1118,7 @@ rust_test = rule(
11171118
)
11181119
```
11191120
1120-
Run the test with `bazel build //hello_lib:greeting_test`.
1121+
Run the test with `bazel test //hello_lib:greeting_test`.
11211122
"""),
11221123
)
11231124

test/rust/BUILD.bazel

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("//rust:defs.bzl", "rust_binary", "rust_library")
1+
load("//rust:defs.bzl", "rust_binary", "rust_library", "rust_test")
22

33
package(default_visibility = ["//visibility:public"])
44

@@ -17,3 +17,8 @@ rust_binary(
1717
edition = "2018",
1818
deps = [":hello_lib"],
1919
)
20+
21+
rust_test(
22+
name = "hello_lib_test",
23+
crate = ":hello_lib",
24+
)

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;

0 commit comments

Comments
 (0)