Skip to content

Commit b6a6f00

Browse files
committed
Add Command::get_env_clear
This addition allows an end-user to inspect whether the env clear value is set on a `Command` instance or not. Discussed in: - #149070 - rust-lang/libs-team#194
1 parent 3d461af commit b6a6f00

File tree

6 files changed

+44
-0
lines changed

6 files changed

+44
-0
lines changed

library/std/src/process.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,30 @@ impl Command {
12061206
pub fn get_current_dir(&self) -> Option<&Path> {
12071207
self.inner.get_current_dir()
12081208
}
1209+
1210+
/// Returns whether the environment will be cleared for the child process.
1211+
///
1212+
/// This returns `true` if [`Command::env_clear`] was called, and `false` otherwise.
1213+
/// When `true`, the child process will not inherit any environment variables from
1214+
/// its parent process.
1215+
///
1216+
/// # Examples
1217+
///
1218+
/// ```
1219+
/// #![feature(command_resolved_envs)]
1220+
/// use std::process::Command;
1221+
///
1222+
/// let mut cmd = Command::new("ls");
1223+
/// assert_eq!(cmd.get_env_clear(), false);
1224+
///
1225+
/// cmd.env_clear();
1226+
/// assert_eq!(cmd.get_env_clear(), true);
1227+
/// ```
1228+
#[must_use]
1229+
#[unstable(feature = "command_resolved_envs", issue = "149070")]
1230+
pub fn get_env_clear(&self) -> bool {
1231+
self.inner.get_env_clear()
1232+
}
12091233
}
12101234

12111235
#[stable(feature = "rust1", since = "1.0.0")]

library/std/src/sys/process/motor.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ impl Command {
9898
self.env.iter()
9999
}
100100

101+
pub fn get_env_clear(&self) -> bool {
102+
self.env.does_clear()
103+
}
104+
101105
pub fn get_current_dir(&self) -> Option<&Path> {
102106
self.cwd.as_ref().map(Path::new)
103107
}

library/std/src/sys/process/uefi.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ impl Command {
8383
self.env.iter()
8484
}
8585

86+
pub fn get_env_clear(&self) -> bool {
87+
self.env.does_clear()
88+
}
89+
8690
pub fn get_current_dir(&self) -> Option<&Path> {
8791
None
8892
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,10 @@ impl Command {
263263
self.env.iter()
264264
}
265265

266+
pub fn get_env_clear(&self) -> bool {
267+
self.env.does_clear()
268+
}
269+
266270
pub fn get_current_dir(&self) -> Option<&Path> {
267271
self.cwd.as_ref().map(|cs| Path::new(OsStr::from_bytes(cs.as_bytes())))
268272
}

library/std/src/sys/process/unsupported.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ impl Command {
8686
self.env.iter()
8787
}
8888

89+
pub fn get_env_clear(&self) -> bool {
90+
self.env.does_clear()
91+
}
92+
8993
pub fn get_current_dir(&self) -> Option<&Path> {
9094
self.cwd.as_ref().map(|cs| Path::new(cs))
9195
}

library/std/src/sys/process/windows.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,10 @@ impl Command {
250250
self.env.iter()
251251
}
252252

253+
pub fn get_env_clear(&self) -> bool {
254+
self.env.does_clear()
255+
}
256+
253257
pub fn get_current_dir(&self) -> Option<&Path> {
254258
self.cwd.as_ref().map(Path::new)
255259
}

0 commit comments

Comments
 (0)