Skip to content

Commit 01e1ab5

Browse files
committed
Auto merge of #14587 - weihanglo:cargo-rustc, r=epage
fix(cargo-rustc): give trailing flags higher precedence on nightly ### What does this PR try to resolve? 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. ### How should we test and review this PR? 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. ### Additional information See discussion on https://rust-lang.zulipchat.com/#narrow/stream/246057-t-cargo/topic/rustflags.20precendence.20of.20.60cargo.20rustc.60 Fixes #14346
2 parents cf781da + 35bb3de commit 01e1ab5

File tree

2 files changed

+98
-4
lines changed

2 files changed

+98
-4
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: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,3 +795,66 @@ windows
795795
)
796796
.run();
797797
}
798+
799+
#[cargo_test]
800+
fn precedence() {
801+
// Ensure that the precedence of cargo-rustc is only lower than RUSTFLAGS,
802+
// but higher than most flags set by cargo.
803+
//
804+
// See rust-lang/cargo#14346
805+
let p = project()
806+
.file(
807+
"Cargo.toml",
808+
r#"
809+
[package]
810+
name = "foo"
811+
edition = "2021"
812+
813+
[lints.rust]
814+
unexpected_cfgs = "allow"
815+
"#,
816+
)
817+
.file("src/lib.rs", "")
818+
.build();
819+
820+
p.cargo("rustc --release -v -- --cfg cargo_rustc -C strip=symbols")
821+
.env("RUSTFLAGS", "--cfg from_rustflags")
822+
.masquerade_as_nightly_cargo(&["cargo-rustc-precedence"])
823+
.with_stderr_data(str![[r#"
824+
[COMPILING] foo v0.0.0 ([ROOT]/foo)
825+
[RUNNING] `rustc [..]-C strip=debuginfo [..]--cfg cargo_rustc -C strip=symbols --cfg from_rustflags`
826+
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
827+
828+
"#]])
829+
.run();
830+
831+
// Ensure the short-live env var to work
832+
p.cargo("clean").run();
833+
p.cargo("rustc --release -v -- --cfg cargo_rustc -C strip=symbols")
834+
.env("RUSTFLAGS", "--cfg from_rustflags")
835+
.env("__CARGO_RUSTC_ORIG_ARGS_PRIO", "1")
836+
.masquerade_as_nightly_cargo(&["cargo-rustc-precedence"])
837+
.with_stderr_data(
838+
str![[r#"
839+
[COMPILING] foo v0.0.0 ([ROOT]/foo)
840+
[RUNNING] `rustc [..]--cfg cargo_rustc -C strip=symbols [..]-C strip=debuginfo [..]--cfg from_rustflags`
841+
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
842+
843+
"#]]
844+
)
845+
.run();
846+
847+
// Ensure non-nightly to work as before
848+
p.cargo("clean").run();
849+
p.cargo("rustc --release -v -- --cfg cargo_rustc -C strip=symbols")
850+
.env("RUSTFLAGS", "--cfg from_rustflags")
851+
.with_stderr_data(
852+
str![[r#"
853+
[COMPILING] foo v0.0.0 ([ROOT]/foo)
854+
[RUNNING] `rustc [..]--cfg cargo_rustc -C strip=symbols [..]-C strip=debuginfo [..]--cfg from_rustflags`
855+
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
856+
857+
"#]]
858+
)
859+
.run();
860+
}

0 commit comments

Comments
 (0)