Skip to content

Commit 35bb3de

Browse files
committed
fix(cargo-rustc): give trailing flags higher precedence
Previously `cargo rustc -- <flags>` got a lower precedence than some of the flags set by cargo internal. This is a bit unintuitive as Cargo generally treats user-provided CLI flags with the highest priority. This commit changes `cargo rustc -- <flags>` to a higher precedence: higher than most of flags set by Cargo, and only lower than `build.rustflags` family. Unsure if this affects people's workflow, so this behavior is only enabled on nightly for collectin feedback. A environment variable `__CARGO_RUSTC_ORIG_ARGS_PRIO=1` is provided for users to opt-out. If everything goes well, the nightly gate will be removed after a few of releases. See discussion on https://rust-lang.zulipchat.com/#narrow/stream/246057-t-cargo/topic/rustflags.20precendence.20of.20.60cargo.20rustc.60
1 parent 7ded268 commit 35bb3de

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,11 @@ fn prepare_rustc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResult
694694
base.inherit_jobserver(&build_runner.jobserver);
695695
build_deps_args(&mut base, build_runner, unit)?;
696696
add_cap_lints(build_runner.bcx, unit, &mut base);
697+
if cargo_rustc_higher_args_precedence(build_runner) {
698+
if let Some(args) = build_runner.bcx.extra_args_for(unit) {
699+
base.args(args);
700+
}
701+
}
697702
base.args(&unit.rustflags);
698703
if build_runner.bcx.gctx.cli_unstable().binary_dep_depinfo {
699704
base.arg("-Z").arg("binary-dep-depinfo");
@@ -753,8 +758,11 @@ fn prepare_rustdoc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResu
753758
}
754759

755760
rustdoc.args(unit.pkg.manifest().lint_rustflags());
756-
if let Some(args) = build_runner.bcx.extra_args_for(unit) {
757-
rustdoc.args(args);
761+
762+
if !cargo_rustc_higher_args_precedence(build_runner) {
763+
if let Some(args) = build_runner.bcx.extra_args_for(unit) {
764+
rustdoc.args(args);
765+
}
758766
}
759767

760768
let metadata = build_runner.metadata_for_doc_units[unit];
@@ -795,6 +803,11 @@ fn prepare_rustdoc(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoResu
795803

796804
rustdoc::add_output_format(build_runner, unit, &mut rustdoc)?;
797805

806+
if cargo_rustc_higher_args_precedence(build_runner) {
807+
if let Some(args) = build_runner.bcx.extra_args_for(unit) {
808+
rustdoc.args(args);
809+
}
810+
}
798811
rustdoc.args(&unit.rustdocflags);
799812

800813
if !crate_version_flag_already_present(&rustdoc) {
@@ -1097,8 +1110,10 @@ fn build_base_args(
10971110

10981111
cmd.args(unit.pkg.manifest().lint_rustflags());
10991112
cmd.args(&profile_rustflags);
1100-
if let Some(args) = build_runner.bcx.extra_args_for(unit) {
1101-
cmd.args(args);
1113+
if !cargo_rustc_higher_args_precedence(build_runner) {
1114+
if let Some(args) = build_runner.bcx.extra_args_for(unit) {
1115+
cmd.args(args);
1116+
}
11021117
}
11031118

11041119
// `-C overflow-checks` is implied by the setting of `-C debug-assertions`,
@@ -1969,3 +1984,19 @@ fn scrape_output_path(build_runner: &BuildRunner<'_, '_>, unit: &Unit) -> CargoR
19691984
.outputs(unit)
19701985
.map(|outputs| outputs[0].path.clone())
19711986
}
1987+
1988+
/// Provides a way to change the precedence of `cargo rustc -- <flags>`.
1989+
///
1990+
/// This is intended to be a short-live function.
1991+
///
1992+
/// See <https://github.com/rust-lang/cargo/issues/14346>
1993+
fn cargo_rustc_higher_args_precedence(build_runner: &BuildRunner<'_, '_>) -> bool {
1994+
build_runner.bcx.gctx.nightly_features_allowed
1995+
&& build_runner
1996+
.bcx
1997+
.gctx
1998+
.get_env("__CARGO_RUSTC_ORIG_ARGS_PRIO")
1999+
.ok()
2000+
.as_deref()
2001+
!= Some("1")
2002+
}

tests/testsuite/rustc.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -819,14 +819,13 @@ fn precedence() {
819819

820820
p.cargo("rustc --release -v -- --cfg cargo_rustc -C strip=symbols")
821821
.env("RUSTFLAGS", "--cfg from_rustflags")
822-
.with_stderr_data(
823-
str![[r#"
822+
.masquerade_as_nightly_cargo(&["cargo-rustc-precedence"])
823+
.with_stderr_data(str![[r#"
824824
[COMPILING] foo v0.0.0 ([ROOT]/foo)
825-
[RUNNING] `rustc [..]--cfg cargo_rustc -C strip=symbols [..]-C strip=debuginfo [..]--cfg from_rustflags`
825+
[RUNNING] `rustc [..]-C strip=debuginfo [..]--cfg cargo_rustc -C strip=symbols --cfg from_rustflags`
826826
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
827827
828-
"#]]
829-
)
828+
"#]])
830829
.run();
831830

832831
// Ensure the short-live env var to work

0 commit comments

Comments
 (0)