Skip to content

Commit 6a4d756

Browse files
committed
fix cargo_env_vars not being cleared
Signed-off-by: Jorge Prendes <jorge.prendes@gmail.com>
1 parent 5cd0130 commit 6a4d756

1 file changed

Lines changed: 31 additions & 15 deletions

File tree

src/command.rs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::collections::{BTreeMap, HashMap};
22
use std::convert::Infallible;
3-
use std::env::VarsOs;
43
use std::ffi::{OsStr, OsString, c_char};
54
use std::fmt::Debug;
65
use std::path::{Path, PathBuf};
@@ -77,10 +76,16 @@ impl Debug for Command {
7776
}
7877
for (k, v) in cmd.get_envs() {
7978
match v {
80-
Some(v) => write!(f, "{}={:?} ", k.to_string_lossy(), v)?,
79+
Some(_) => (), // will be done next
8180
None => write!(f, "-u {} ", k.to_string_lossy())?,
8281
}
8382
}
83+
for (k, v) in cmd.get_envs() {
84+
match v {
85+
Some(v) => write!(f, "{}={:?} ", k.to_string_lossy(), v)?,
86+
None => (), // already done above
87+
}
88+
}
8489
write!(f, "{:?} ", self.get_program())?;
8590
for arg in &self.args {
8691
write!(f, "{arg:?} ")?;
@@ -288,12 +293,14 @@ impl Command {
288293
self
289294
}
290295

291-
/// Clears all `CARGO_` environment variables that will be set for the child process.
296+
/// Clears all `CARGO_` environment variables that will be set for the child process,
297+
/// except for `CARGO_HOME`.
292298
///
293299
/// This method will remove all environment variables starting with `CARGO_`
294300
/// from the child process, including those that would normally be inherited
295-
/// from the parent process. Other environment variables will remain unaffected.
296-
/// Environment variables can be added back individually using [`env`].
301+
/// from the parent process, except for `CARGO_HOME`. Other environment variables
302+
/// will remain unaffected. Environment variables can be added back individually
303+
/// using [`env`].
297304
///
298305
/// This is particularly useful when using cargo-hyperlight from a build script
299306
/// or other cargo-invoked context where `CARGO_` variables may change the behavior
@@ -317,7 +324,7 @@ impl Command {
317324
/// [`env`]: Command::env
318325
pub fn env_clear_cargo_vars(&mut self) -> &mut Self {
319326
self.inherit_cargo_envs = false;
320-
self.envs.retain(|k, _| !k.starts_with("CARGO_"));
327+
self.envs.retain(|k, _| !is_cargo_env(k));
321328
self
322329
}
323330

@@ -491,13 +498,16 @@ impl Command {
491498
/// from the current process, taking into account whether [`env_clear`] has been called.
492499
///
493500
/// [`env_clear`]: Command::env_clear
494-
fn base_env(&self) -> VarsOs {
495-
let mut env = env::vars_os();
496-
if !self.inherit_envs {
497-
// iterate over the whole VarOs to consume it
498-
env.find(|_| false);
499-
}
500-
env
501+
fn base_env(&self) -> impl Iterator<Item = (OsString, OsString)> {
502+
env::vars_os().filter(|(k, _)| {
503+
if !self.inherit_envs {
504+
false
505+
} else if !self.inherit_cargo_envs {
506+
!is_cargo_env(k)
507+
} else {
508+
true
509+
}
510+
})
501511
}
502512

503513
fn resolve_env(&self) -> HashMap<OsString, OsString> {
@@ -514,8 +524,10 @@ impl Command {
514524
command.env_clear();
515525
}
516526
if !self.inherit_cargo_envs {
517-
for (k, _) in std::env::vars_os().filter(|(k, _)| k.starts_with("CARGO_")) {
518-
command.env_remove(k);
527+
for (k, _) in env::vars_os() {
528+
if is_cargo_env(&k) {
529+
command.env_remove(k);
530+
}
519531
}
520532
}
521533
if let Some(rustup_toolchain) = &self.cargo.rustup_toolchain {
@@ -734,3 +746,7 @@ fn exec(
734746

735747
Err(std::io::Error::last_os_error())
736748
}
749+
750+
fn is_cargo_env(key: &OsStr) -> bool {
751+
key != "CARGO_HOME" && key.starts_with("CARGO_")
752+
}

0 commit comments

Comments
 (0)