@@ -1155,7 +1155,8 @@ impl Command {
11551155 /// [`Command::env_remove`] can be retrieved with this method.
11561156 ///
11571157 /// Note that this output does not include environment variables inherited from the parent
1158- /// process.
1158+ /// process. To see the full list of environment variables, including those inherited from the
1159+ /// parent process, use [`Command::get_resolved_envs`].
11591160 ///
11601161 /// Each element is a tuple key/value pair `(&OsStr, Option<&OsStr>)`. A [`None`] value
11611162 /// indicates its key was explicitly removed via [`Command::env_remove`]. The associated key for
@@ -1184,6 +1185,42 @@ impl Command {
11841185 CommandEnvs { iter : self . inner . get_envs ( ) }
11851186 }
11861187
1188+ /// Returns an iterator of the environment variables that will be set when the process is spawned.
1189+ ///
1190+ /// This returns the environment as it would be if the command were executed at the time of calling
1191+ /// this method. The returned environment includes:
1192+ /// - All inherited environment variables from the parent process (unless [`Command::env_clear`] was called)
1193+ /// - All environment variables explicitly set via [`Command::env`] or [`Command::envs`]
1194+ /// - Excluding any environment variables removed via [`Command::env_remove`]
1195+ ///
1196+ /// Note that the returned environment is a snapshot at the time this method is called and will not
1197+ /// reflect any subsequent changes to the `Command` or the parent process's environment. Additionally,
1198+ /// it will not reflect changes made in a `pre_exec` hook (on Unix platforms).
1199+ ///
1200+ /// Each element is a tuple `(OsString, OsString)` representing an environment variable key and value.
1201+ ///
1202+ /// # Examples
1203+ ///
1204+ /// ```
1205+ /// #![feature(command_resolved_envs)]
1206+ /// use std::process::Command;
1207+ /// use std::ffi::{OsString, OsStr};
1208+ /// use std::env;
1209+ /// use std::collections::HashMap;
1210+ ///
1211+ /// let mut cmd = Command::new("ls");
1212+ /// cmd.env("TZ", "UTC");
1213+ /// unsafe { env::set_var("EDITOR", "vim"); }
1214+ ///
1215+ /// let resolved: HashMap<OsString, OsString> = cmd.get_resolved_envs().collect();
1216+ /// assert_eq!(resolved.get(OsStr::new("TZ")), Some(&OsString::from("UTC")));
1217+ /// assert_eq!(resolved.get(OsStr::new("EDITOR")), Some(&OsString::from("vim")));
1218+ /// ```
1219+ #[ unstable( feature = "command_resolved_envs" , issue = "149070" ) ]
1220+ pub fn get_resolved_envs ( & self ) -> CommandResolvedEnvs {
1221+ self . inner . get_resolved_envs ( )
1222+ }
1223+
11871224 /// Returns the working directory for the child process.
11881225 ///
11891226 /// This returns [`None`] if the working directory will not be changed.
@@ -1337,6 +1374,9 @@ impl<'a> fmt::Debug for CommandEnvs<'a> {
13371374 }
13381375}
13391376
1377+ #[ unstable( feature = "command_resolved_envs" , issue = "149070" ) ]
1378+ pub use imp:: CommandResolvedEnvs ;
1379+
13401380/// The output of a finished process.
13411381///
13421382/// This is returned in a Result by either the [`output`] method of a
0 commit comments