summaryrefslogtreecommitdiff
path: root/xtask/src/main.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2026-08-31 16:59:03 +0200
committerHampusM <hampus@hampusmat.com>2026-08-31 16:59:03 +0200
commit7ed45478384dc8eb5bdf3eae0dbefd033c93e55f (patch)
tree27d83d6b48dcf41297e55a39b0bc3820e742845d /xtask/src/main.rs
parent47504e744398d16c013b976aade043fe5372e31e (diff)
fix(xtask): prevent incorrect CARGO_* env vars when executed command is cargo
Diffstat (limited to 'xtask/src/main.rs')
-rw-r--r--xtask/src/main.rs35
1 files changed, 34 insertions, 1 deletions
diff --git a/xtask/src/main.rs b/xtask/src/main.rs
index e955925..8bf25ae 100644
--- a/xtask/src/main.rs
+++ b/xtask/src/main.rs
@@ -15,10 +15,43 @@ fn main()
return;
};
- if let Err(err) = Command::new(program).args(args).status() {
+ if let Err(err) = Command::new(program)
+ .args(args)
+ .remove_envs(
+ // When a binary crate is run with 'cargo run', cargo sets environment
+ // variables with information about the crate, package & cargo manifest.
+ // These environment variables are unwanted when the program run here
+ // is cargo so they are removed.
+ //
+ // If cargo is the program run here and the environment variables are
+ // kept, cargo does not overwrite them, which can cause unnecessary
+ // rebuilds of dependencies
+ std::env::vars()
+ .map(|(key, _)| key)
+ .filter(|key| key.starts_with("CARGO_") && key != "CARGO_HOME"),
+ )
+ .status()
+ {
println!("Error: Failed to execute command: {err}");
}
} else {
println!("Error: Unknown task '{task}'");
}
}
+
+trait CommandExt
+{
+ fn remove_envs(&mut self, keys: impl IntoIterator<Item = String>) -> &mut Self;
+}
+
+impl CommandExt for Command
+{
+ fn remove_envs(&mut self, keys: impl IntoIterator<Item = String>) -> &mut Self
+ {
+ for env_key in keys {
+ self.env_remove(env_key);
+ }
+
+ self
+ }
+}