Skip to content
Open
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
6 changes: 6 additions & 0 deletions src/bin/cargo/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use cargo::core::features;
use cargo::util::context::WarningHandling;
use cargo::util::network::http::http_handle;
use cargo::util::network::http::needs_custom_http_transport;
use cargo::util::{self, CargoResult, closest_msg, command_prelude};
Expand Down Expand Up @@ -315,6 +316,11 @@ fn execute_subcommand(
None => ProcessBuilder::new(&cargo_exe),
};
cmd.env(cargo::CARGO_ENV, cargo_exe).args(args);
if let Ok(handling) = gctx.warning_handling() {
Copy link
Copy Markdown
Member

@weihanglo weihanglo May 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reasonable and useful enough to special-case it, though we might want to discuss in team meeting

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@epage
It seems worth to me as it is pretty common with clippy in CI we set env var for overrides. However, I don't know if we want to restrict this to clippy-like binary only to avoid over-expanding the stable interface to all external commands

if handling != WarningHandling::Warn {
cmd.env("CARGO_BUILD_WARNINGS", handling.as_str());
}
}
if let Some(client) = gctx.jobserver_from_env() {
cmd.inherit_jobserver(client);
}
Expand Down
10 changes: 10 additions & 0 deletions src/cargo/util/context/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,16 @@ pub enum WarningHandling {
Deny,
}

impl WarningHandling {
pub fn as_str(self) -> &'static str {
match self {
WarningHandling::Warn => "warn",
WarningHandling::Allow => "allow",
WarningHandling::Deny => "deny",
}
}
}

/// Configuration for `build.target`.
///
/// Accepts in the following forms:
Expand Down
48 changes: 48 additions & 0 deletions tests/testsuite/warning_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,3 +527,51 @@ fn cap_lints_allow() {
"#]])
.run();
}

#[cargo_test]
fn config_before_subcommand_forwarded() {
let clippy_mock = project()
.at("cargo-clippy-mock")
.file(
"Cargo.toml",
&cargo_test_support::basic_manifest("cargo-clippy-mock", "0.0.1"),
)
.file(
"src/main.rs",
r#"
fn main() {
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_string());
let mut cmd = std::process::Command::new(cargo);
cmd.arg("check");
cmd.args(std::env::args().skip(2));
let status = cmd.status().unwrap();
std::process::exit(status.code().unwrap_or(1));
}
"#,
)
.build();
clippy_mock.cargo("build").run();

let p = make_project_with_rustc_warning();

let mut path =
std::env::split_paths(&std::env::var_os("PATH").unwrap_or_default()).collect::<Vec<_>>();
path.push(clippy_mock.target_debug_dir());
let path = std::env::join_paths(path.iter()).unwrap();

p.cargo("")
.arg("--config")
.arg("build.warnings='deny'")
.arg("clippy-mock")
.env("PATH", &path)
.with_status(101)
.with_stderr_data(str![[r#"
[CHECKING] foo v0.0.1 ([ROOT]/foo)
Comment thread
raushan728 marked this conversation as resolved.
[WARNING] unused variable: `x`
...
[ERROR] `foo` (bin "foo") generated 1 warning (run `cargo fix --bin "foo" -p foo` to apply 1 suggestion)
[ERROR] warnings are denied by `build.warnings` configuration

"#]])
.run();
}
Loading