Skip to content

Commit 07051f3

Browse files
Fix unwanted rebuilds in cargo xtask diff-walk
Rustup/cargo don't handle nested invocations well. Ideally, `cargo run` would behave the same as `cargo build && <run the program>`, but in practice it does not, because rustup/cargo set env vars which affect nested invocations of rustup/cargo. Recently (possibly in 1.85, but not sure) this resulted in `cargo xtask diff-walk` always building twice: once for `cargo xtask`, then a second time for the release build of `mount_and_walk`. Running the same command again would not use the cache, but rebuild everything. Fix by clearing all env vars, except for the PATH. The PATH needs to be preserved in order to find which `cargo` to execute.
1 parent 9fbc69f commit 07051f3

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

xtask/src/diff_walk.rs

+25-8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::{capture_cmd, run_cmd, sudo};
1010
use anyhow::{Result, bail};
1111
use ext4_view::{Ext4, Ext4Error};
1212
use sha2::{Digest, Sha256};
13+
use std::env;
1314
use std::fs::File;
1415
use std::io::Read;
1516
use std::os::unix::ffi::OsStrExt;
@@ -155,14 +156,30 @@ fn is_compressed(path: &Path) -> Result<bool> {
155156
/// requires elevated permissions.
156157
pub fn diff_walk(orig_path: &Path) -> Result<()> {
157158
// Build `mount_and_walk` in release mode.
158-
run_cmd(Command::new("cargo").args([
159-
"build",
160-
"--release",
161-
"--package",
162-
"xtask",
163-
"--bin",
164-
"mount_and_walk",
165-
]))?;
159+
let path = env::var("PATH")?;
160+
run_cmd(
161+
Command::new("cargo")
162+
.args([
163+
"build",
164+
"--release",
165+
"--package",
166+
"xtask",
167+
"--bin",
168+
"mount_and_walk",
169+
])
170+
// Clear the env except for the PATH var. This avoids
171+
// unwanted rebuilds. The details are complicated, but
172+
// basically rustup/cargo are not designed to allow nested
173+
// invocations. Clearing env vars tricks rustup/cargo into
174+
// behaving correctly.
175+
//
176+
// Some past issues that may be relevant (but probably don't
177+
// tell the whole story):
178+
// * https://github.com/rust-lang/rustup/issues/3036
179+
// * https://github.com/rust-lang/cargo/issues/15099
180+
.env_clear()
181+
.env("PATH", path),
182+
)?;
166183

167184
// If the input file is compressed, decompress it and write to a
168185
// temporary file.

0 commit comments

Comments
 (0)