Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions cargo/private/cargo_build_script_wrapper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def cargo_build_script(
rustc_env = {},
rustc_env_files = [],
rustc_flags = [],
lint_config = None,
visibility = None,
tags = None,
aliases = None,
Expand Down Expand Up @@ -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. \
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions test/unit/lint_flags/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn main() {}
31 changes: 28 additions & 3 deletions test/unit/lint_flags/lint_flags_test.bzl
Original file line number Diff line number Diff line change
@@ -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")

Expand Down Expand Up @@ -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)

Expand All @@ -45,6 +49,7 @@ extra_rustc_flag_present_test = analysistest.make(
"rustc_flags": attr.string_list(
mandatory = True,
),
"apply_in_exec": attr.bool(),
},
)

Expand All @@ -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"],
Expand Down Expand Up @@ -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(
Expand All @@ -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",
],
Expand Down