Skip to content

Commit 1970393

Browse files
Fix unwanted rebuilds in xtask commands
Recently, `cargo xtask test` started doing a lot of rebuilding. Possibly this started with cargo 1.85, but I'm not sure. The behavior was: `cargo xtask test` builds `xtask` and all of its dependencies in order to launch `xtask`. Then it would rebuild everything again to run the test command. If you then ran `cargo xtask test` a second time, everything would again be rebuilt. In other words, the build cache essentially wasn't working. Fix by clearing all `CARGO` env vars in `fix_nested_cargo_env`.
1 parent aecfa0a commit 1970393

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

xtask/src/cargo.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,28 @@ fn sanitized_path(orig_path: OsString) -> OsString {
216216
env::join_paths(sanitized_paths).expect("invalid PATH")
217217
}
218218

219-
/// Cargo automatically sets some env vars that can prevent the
220-
/// channel arg (e.g. "+nightly") from working. Unset them in the
221-
/// child's environment.
222-
pub fn fix_nested_cargo_env(cmd: &mut Command) {
219+
/// Update a command env to make nested cargo invocations work correctly.
220+
///
221+
/// Cargo automatically sets some env vars that cause problems with nested cargo
222+
/// invocations. In particular, these env vars can:
223+
/// * Cause unwanted rebuilds, e.g. running `cargo xtask test` multiple times
224+
/// will rebuild every time because cached dependencies are marked as stale.
225+
/// * Prevent channels args (e.g. "+nightly") from working.
226+
///
227+
/// Some related issues:
228+
/// * <https://github.com/rust-lang/cargo/issues/15099>
229+
/// * <https://github.com/rust-lang/rustup/issues/3031>
230+
fn fix_nested_cargo_env(cmd: &mut Command) {
223231
cmd.env_remove("RUSTC");
224232
cmd.env_remove("RUSTDOC");
233+
234+
// Clear all vars starting with `CARGO`.
235+
for (name, _) in env::vars() {
236+
if name.starts_with("CARGO") {
237+
cmd.env_remove(name);
238+
}
239+
}
240+
225241
let orig_path = env::var_os("PATH").unwrap_or_default();
226242
cmd.env("PATH", sanitized_path(orig_path));
227243
}

0 commit comments

Comments
 (0)