Skip to content

Commit 51be742

Browse files
committed
fix: Fix unusual errors with RUSTC_WRAPPER
This commit fixes the interaction of `cargo fix` and `RUSTC_WRAPPER`, ensuring that Cargo at least doesn't die internally. For now `RUSTC_WRAPPER` is overridden for normal execution but we can eventually one day probably support `RUSTC_WRAPPER`! Closes #5981
1 parent 065e3ef commit 51be742

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

src/cargo/core/compiler/compilation.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,26 @@ pub struct Compilation<'cfg> {
8282

8383
impl<'cfg> Compilation<'cfg> {
8484
pub fn new<'a>(bcx: &BuildContext<'a, 'cfg>) -> CargoResult<Compilation<'cfg>> {
85-
let mut rustc = bcx.rustc.process();
85+
// If we're using cargo as a rustc wrapper then we're in a situation
86+
// like `cargo fix`. For now just disregard the `RUSTC_WRAPPER` env var
87+
// (which is typically set to `sccache` for now). Eventually we'll
88+
// probably want to implement `RUSTC_WRAPPER` for `cargo fix`, but we'll
89+
// leave that open as a bug for now.
90+
let mut rustc = if bcx.build_config.cargo_as_rustc_wrapper {
91+
let mut rustc = bcx.rustc.process_no_wrapper();
92+
let prog = rustc.get_program().to_owned();
93+
rustc.env("RUSTC", prog);
94+
rustc.program(env::current_exe()?);
95+
rustc
96+
} else {
97+
bcx.rustc.process()
98+
};
8699
for (k, v) in bcx.build_config.extra_rustc_env.iter() {
87100
rustc.env(k, v);
88101
}
89102
for arg in bcx.build_config.extra_rustc_args.iter() {
90103
rustc.arg(arg);
91104
}
92-
if bcx.build_config.cargo_as_rustc_wrapper {
93-
let prog = rustc.get_program().to_owned();
94-
rustc.env("RUSTC", prog);
95-
rustc.program(env::current_exe()?);
96-
}
97105
let srv = bcx.build_config.rustfix_diagnostic_server.borrow();
98106
if let Some(server) = &*srv {
99107
server.configure(&mut rustc);

src/cargo/util/rustc.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,17 @@ impl Rustc {
6868
pub fn process(&self) -> ProcessBuilder {
6969
if let Some(ref wrapper) = self.wrapper {
7070
let mut cmd = util::process(wrapper);
71-
{
72-
cmd.arg(&self.path);
73-
}
71+
cmd.arg(&self.path);
7472
cmd
7573
} else {
76-
util::process(&self.path)
74+
self.process_no_wrapper()
7775
}
7876
}
7977

78+
pub fn process_no_wrapper(&self) -> ProcessBuilder {
79+
util::process(&self.path)
80+
}
81+
8082
pub fn cached_output(&self, cmd: &ProcessBuilder) -> CargoResult<(String, String)> {
8183
self.cache.lock().unwrap().cached_output(cmd)
8284
}

tests/testsuite/fix.rs

+23
Original file line numberDiff line numberDiff line change
@@ -1117,3 +1117,26 @@ fn doesnt_rebuild_dependencies() {
11171117
")
11181118
.run();
11191119
}
1120+
1121+
#[test]
1122+
fn does_not_crash_with_rustc_wrapper() {
1123+
// We don't have /usr/bin/env on Windows.
1124+
if cfg!(windows) {
1125+
return;
1126+
}
1127+
let p = project()
1128+
.file(
1129+
"Cargo.toml",
1130+
r#"
1131+
[package]
1132+
name = "foo"
1133+
version = "0.1.0"
1134+
"#,
1135+
)
1136+
.file("src/lib.rs", "")
1137+
.build();
1138+
1139+
p.cargo("fix --allow-no-vcs")
1140+
.env("RUSTC_WRAPPER", "/usr/bin/env")
1141+
.run();
1142+
}

0 commit comments

Comments
 (0)