diff --git a/cargo/private/cargo_build_script_wrapper.bzl b/cargo/private/cargo_build_script_wrapper.bzl index 2c7e040eef..23de064399 100644 --- a/cargo/private/cargo_build_script_wrapper.bzl +++ b/cargo/private/cargo_build_script_wrapper.bzl @@ -52,6 +52,7 @@ def cargo_build_script( rustc_env = {}, rustc_env_files = [], rustc_flags = [], + lint_config = None, visibility = None, tags = None, aliases = None, @@ -160,6 +161,7 @@ def cargo_build_script( rustc_env_files (list of label, optional): Files containing additional environment variables to set for rustc when building the build script. rustc_flags (list, optional): List of compiler flags passed to `rustc`. + lint_config (Label, optional): Lints applied while compiling the build script. visibility (list of label, optional): Visibility to apply to the generated build script output. tags: (list of str, optional): Tags to apply to the generated build script output. aliases (dict, optional): Remap crates to a new name or moniker for linkage to this target. \ @@ -221,6 +223,7 @@ def cargo_build_script( rustc_env = rustc_env, rustc_env_files = rustc_env_files, rustc_flags = rustc_flags, + lint_config = lint_config, edition = edition, tags = binary_tags, aliases = aliases, diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index d447c97303..1099e35098 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -1756,10 +1756,10 @@ def rustc_compile_action( # Add flags for any 'rustc' lints that are specified. # - # Exclude lints if we're building in the exec configuration to prevent crates - # used in build scripts from generating warnings. + # An explicit lint configuration belongs to the crate in either + # configuration, including first-party build scripts and procedural macros. lint_files = [] - if hasattr(ctx.attr, "lint_config") and ctx.attr.lint_config and not is_exec_configuration(ctx): + if hasattr(ctx.attr, "lint_config") and ctx.attr.lint_config: rust_flags = rust_flags + ctx.attr.lint_config[LintsInfo].rustc_lint_flags lint_files = lint_files + ctx.attr.lint_config[LintsInfo].rustc_lint_files diff --git a/test/unit/lint_flags/build.rs b/test/unit/lint_flags/build.rs new file mode 100644 index 0000000000..f328e4d9d0 --- /dev/null +++ b/test/unit/lint_flags/build.rs @@ -0,0 +1 @@ +fn main() {} diff --git a/test/unit/lint_flags/lint_flags_test.bzl b/test/unit/lint_flags/lint_flags_test.bzl index 3e7a9dacd8..5fc994f9fc 100644 --- a/test/unit/lint_flags/lint_flags_test.bzl +++ b/test/unit/lint_flags/lint_flags_test.bzl @@ -1,6 +1,7 @@ """Unittest to verify compile_data (attribute) propagation""" -load("@bazel_skylib//lib:unittest.bzl", "analysistest") +load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") +load("//cargo:defs.bzl", "cargo_build_script") load("//rust:defs.bzl", "rust_clippy", "rust_doc", "rust_library", "rust_lint_config") load("//test/unit:common.bzl", "assert_argv_contains", "assert_argv_contains_not") @@ -29,9 +30,12 @@ def _extra_rustc_flags_present_test(ctx): target = analysistest.target_under_test(env) target_action_contains_flag(env, target, ctx.attr.rustc_flags) - # Check the exec configuration target does NOT contain. target = ctx.attr.lib_exec - target_action_contains_not_flag(env, target, ctx.attr.rustc_flags) + if ctx.attr.apply_in_exec: + asserts.true(env, any([action.mnemonic == "Rustc" for action in target.actions]), "expected an exec-configuration Rustc action") + target_action_contains_flag(env, target, ctx.attr.rustc_flags) + else: + target_action_contains_not_flag(env, target, ctx.attr.rustc_flags) return analysistest.end(env) @@ -45,6 +49,7 @@ extra_rustc_flag_present_test = analysistest.make( "rustc_flags": attr.string_list( mandatory = True, ), + "apply_in_exec": attr.bool(), }, ) @@ -64,6 +69,13 @@ def _define_test_targets(): edition = "2018", ) + cargo_build_script( + name = "script", + srcs = ["build.rs"], + lint_config = ":workspace_lints", + edition = "2018", + ) + rust_clippy( name = "clippy", deps = [":lib"], @@ -91,6 +103,18 @@ def lint_flags_test_suite(name): "--allow=unknown_lints", "--check-cfg=cfg(bazel)", ], + apply_in_exec = True, + ) + + extra_rustc_flag_present_test( + name = "build_script_lints_apply_in_exec", + target_under_test = ":script_", + lib_exec = ":script_", + rustc_flags = [ + "--allow=unknown_lints", + "--check-cfg=cfg(bazel)", + ], + apply_in_exec = True, ) extra_rustc_flag_present_test( @@ -111,6 +135,7 @@ def lint_flags_test_suite(name): name = name, tests = [ ":rustc_lints_apply_flags", + ":build_script_lints_apply_in_exec", ":clippy_lints_apply_flags", ":rustdoc_lints_apply_flags", ],