Skip to content

Commit

Permalink
Inform users that crate and srcs are mutually exclusive (#1650)
Browse files Browse the repository at this point in the history
This should avoid confusion in cases where users have defined tests with
both `rust_test.crate` and `rust_test.srcs` set and later find
`rust_test.srcs` are actually unused and not being tested.

relates to #2324
  • Loading branch information
UebelAndre authored Dec 14, 2023
1 parent 3089f92 commit bf68435
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 55 deletions.
1 change: 0 additions & 1 deletion examples/crate_universe/no_cargo_manifests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ rust_binary(

rust_test(
name = "unit_test",
srcs = glob(["**/*.rs"]),
crate = ":no_cargo_manifests",
edition = "2018",
)
1 change: 0 additions & 1 deletion examples/crate_universe/vendor_local_pkgs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ rust_binary(

rust_test(
name = "unit_test",
srcs = glob(["**/*.rs"]),
crate = ":vendor_local",
edition = "2018",
)
1 change: 0 additions & 1 deletion examples/crate_universe/vendor_remote_pkgs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ rust_binary(

rust_test(
name = "unit_test",
srcs = glob(["**/*.rs"]),
crate = ":vendor_remote",
edition = "2018",
)
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ rust_binary(

rust_test(
name = "unit_test",
srcs = glob(["**/*.rs"]),
crate = ":vendor_remote",
edition = "2018",
)
6 changes: 6 additions & 0 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,12 @@ def _rust_test_impl(ctx):
deps = transform_deps(ctx.attr.deps)
proc_macro_deps = transform_deps(ctx.attr.proc_macro_deps + get_import_macro_deps(ctx))

if toolchain._incompatible_test_attr_crate_and_srcs_mutually_exclusive:
if ctx.attr.crate and ctx.attr.srcs:
fail("rust_test.crate and rust_test.srcs are mutually exclusive. Update {} to use only one of these attributes".format(
ctx.label,
))

if ctx.attr.crate:
# 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
Expand Down
17 changes: 12 additions & 5 deletions rust/settings/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ load(":incompatible.bzl", "incompatible_flag")

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

bzl_library(
name = "bzl_lib",
srcs = glob(["**/*.bzl"]),
)

# A flag controlling whether to rename first-party crates such that their names
# encode the Bazel package and target name, instead of just the target name.
#
Expand Down Expand Up @@ -60,14 +65,16 @@ bool_flag(
build_setting_default = False,
)

bzl_library(
name = "bzl_lib",
srcs = glob(["**/*.bzl"]),
)

# A flag to set rustc --sysroot flag to the sysroot generated by rust_toolchain
incompatible_flag(
name = "experimental_toolchain_generated_sysroot",
build_setting_default = True,
issue = "https://github.com/bazelbuild/rules_rust/issues/2039",
)

# A flag to make `rust_test.crate` and `rust_test.srcs` mutually exclusive.
incompatible_flag(
name = "incompatible_test_attr_crate_and_srcs_mutually_exclusive",
build_setting_default = True,
issue = "https://github.com/bazelbuild/rules_rust/issues/2324",
)
4 changes: 4 additions & 0 deletions rust/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,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_test_attr_crate_and_srcs_mutually_exclusive = ctx.attr._incompatible_test_attr_crate_and_srcs_mutually_exclusive[IncompatibleFlagInfo].enabled,
_no_std = no_std,
)
return [
Expand Down Expand Up @@ -806,6 +807,9 @@ rust_toolchain = rule(
"This flag is only relevant when used together with --@rules_rust//rust/settings:experimental_use_global_allocator."
),
),
"_incompatible_test_attr_crate_and_srcs_mutually_exclusive": attr.label(
default = Label("//rust/settings:incompatible_test_attr_crate_and_srcs_mutually_exclusive"),
),
"_no_std": attr.label(
default = Label("//:no_std"),
),
Expand Down
20 changes: 0 additions & 20 deletions test/inline_test_with_deps/test_with_srcs/BUILD.bazel

This file was deleted.

4 changes: 0 additions & 4 deletions test/inline_test_with_deps/test_with_srcs/src/extra.rs

This file was deleted.

18 changes: 0 additions & 18 deletions test/inline_test_with_deps/test_with_srcs/src/lib.rs

This file was deleted.

5 changes: 1 addition & 4 deletions test/out_dir_in_tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ rust_doc_test(
rust_test_suite(
name = "suite",
srcs = glob(["tests/**"]),
# Add the 'crate' argument, which will be passed as a kwarg
# to the underlying rust_test rules. This will make OUT_DIR
# available when compiling integration tests.
crate = ":demo_lib",
edition = "2018",
deps = [":build_script"],
)

0 comments on commit bf68435

Please sign in to comment.