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:

- https://github.com/rust-lang/rust/issues/149070
- https://github.com/rust-lang/libs-team/issues/194
This commit is contained in:
Schneems 2025-11-18 19:18:33 -06:00
parent 3d461af2a2
commit b6a6f00a2d
6 changed files with 44 additions and 0 deletions

View file

@ -1206,6 +1206,30 @@ impl Command {
pub fn get_current_dir(&self) -> Option<&Path> {
self.inner.get_current_dir()
}
/// Returns whether the environment will be cleared for the child process.
///
/// This returns `true` if [`Command::env_clear`] was called, and `false` otherwise.
/// When `true`, the child process will not inherit any environment variables from
/// its parent process.
///
/// # Examples
///
/// ```
/// #![feature(command_resolved_envs)]
/// use std::process::Command;
///
/// let mut cmd = Command::new("ls");
/// assert_eq!(cmd.get_env_clear(), false);
///
/// cmd.env_clear();
/// assert_eq!(cmd.get_env_clear(), true);
/// ```
#[must_use]
#[unstable(feature = "command_resolved_envs", issue = "149070")]
pub fn get_env_clear(&self) -> bool {
self.inner.get_env_clear()
}
}
#[stable(feature = "rust1", since = "1.0.0")]

View file

@ -98,6 +98,10 @@ impl Command {
self.env.iter()
}
pub fn get_env_clear(&self) -> bool {
self.env.does_clear()
}
pub fn get_current_dir(&self) -> Option<&Path> {
self.cwd.as_ref().map(Path::new)
}

View file

@ -83,6 +83,10 @@ impl Command {
self.env.iter()
}
pub fn get_env_clear(&self) -> bool {
self.env.does_clear()
}
pub fn get_current_dir(&self) -> Option<&Path> {
None
}

View file

@ -263,6 +263,10 @@ impl Command {
self.env.iter()
}
pub fn get_env_clear(&self) -> bool {
self.env.does_clear()
}
pub fn get_current_dir(&self) -> Option<&Path> {
self.cwd.as_ref().map(|cs| Path::new(OsStr::from_bytes(cs.as_bytes())))
}

View file

@ -86,6 +86,10 @@ impl Command {
self.env.iter()
}
pub fn get_env_clear(&self) -> bool {
self.env.does_clear()
}
pub fn get_current_dir(&self) -> Option<&Path> {
self.cwd.as_ref().map(|cs| Path::new(cs))
}

View file

@ -250,6 +250,10 @@ impl Command {
self.env.iter()
}
pub fn get_env_clear(&self) -> bool {
self.env.does_clear()
}
pub fn get_current_dir(&self) -> Option<&Path> {
self.cwd.as_ref().map(Path::new)
}