Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 53a29e0

Browse files
committed
also print clearing the environment entirely
1 parent 3a28887 commit 53a29e0

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

library/std/src/process/tests.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,29 @@ fn debug_print() {
560560
"FOO": None,
561561
}},
562562
}},
563+
{PIDFD}}}"#
564+
)
565+
);
566+
567+
let mut command_with_cleared_env = Command::new("boring-name");
568+
command_with_cleared_env.env_clear().env("BAR", "val").env_remove("FOO");
569+
assert_eq!(format!("{command_with_cleared_env:?}"), r#"env -i BAR="val" "boring-name""#);
570+
assert_eq!(
571+
format!("{command_with_cleared_env:#?}"),
572+
format!(
573+
r#"Command {{
574+
program: "boring-name",
575+
args: [
576+
"boring-name",
577+
],
578+
env: CommandEnv {{
579+
clear: true,
580+
vars: {{
581+
"BAR": Some(
582+
"val",
583+
),
584+
}},
585+
}},
563586
{PIDFD}}}"#
564587
)
565588
);

library/std/src/sys/unix/process/process_common.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -558,20 +558,25 @@ impl fmt::Debug for Command {
558558
if let Some(ref cwd) = self.cwd {
559559
write!(f, "cd {cwd:?} && ")?;
560560
}
561-
// Removed env vars need a separate command.
562-
// We use a single `unset` command for all of them.
563-
let mut any_removed = false;
564-
for (key, value_opt) in self.get_envs() {
565-
if value_opt.is_none() {
566-
if !any_removed {
567-
write!(f, "unset ")?;
568-
any_removed = true;
561+
if self.env.does_clear() {
562+
write!(f, "env -i ")?;
563+
// Altered env vars will be printed next, that should exactly work as expected.
564+
} else {
565+
// Removed env vars need a separate command.
566+
// We use a single `unset` command for all of them.
567+
let mut any_removed = false;
568+
for (key, value_opt) in self.get_envs() {
569+
if value_opt.is_none() {
570+
if !any_removed {
571+
write!(f, "unset ")?;
572+
any_removed = true;
573+
}
574+
write!(f, "{} ", key.to_string_lossy())?;
569575
}
570-
write!(f, "{} ", key.to_string_lossy())?;
571576
}
572-
}
573-
if any_removed {
574-
write!(f, "&& ")?;
577+
if any_removed {
578+
write!(f, "&& ")?;
579+
}
575580
}
576581
// Altered env vars can just be added in front of the program.
577582
for (key, value_opt) in self.get_envs() {

library/std/src/sys_common/process.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ impl CommandEnv {
8080
self.vars.clear();
8181
}
8282

83+
pub fn does_clear(&self) -> bool {
84+
self.clear
85+
}
86+
8387
pub fn have_changed_path(&self) -> bool {
8488
self.saw_path || self.clear
8589
}

0 commit comments

Comments
 (0)