@@ -1185,7 +1185,8 @@ impl Command {
11851185 /// [`Command::env_remove`] can be retrieved with this method.
11861186 ///
11871187 /// Note that this output does not include environment variables inherited from the parent
1188- /// process.
1188+ /// process. To see the full list of environment variables, including those inherited from the
1189+ /// parent process, use [`Command::get_resolved_envs`].
11891190 ///
11901191 /// Each element is a tuple key/value pair `(&OsStr, Option<&OsStr>)`. A [`None`] value
11911192 /// indicates its key was explicitly removed via [`Command::env_remove`]. The associated key for
@@ -1214,6 +1215,42 @@ impl Command {
12141215 CommandEnvs { iter : self . inner . get_envs ( ) }
12151216 }
12161217
1218+ /// Returns an iterator of the environment variables that will be set when the process is spawned.
1219+ ///
1220+ /// This returns the environment as it would be if the command were executed at the time of calling
1221+ /// this method. The returned environment includes:
1222+ /// - All inherited environment variables from the parent process (unless [`Command::env_clear`] was called)
1223+ /// - All environment variables explicitly set via [`Command::env`] or [`Command::envs`]
1224+ /// - Excluding any environment variables removed via [`Command::env_remove`]
1225+ ///
1226+ /// Note that the returned environment is a snapshot at the time this method is called and will not
1227+ /// reflect any subsequent changes to the `Command` or the parent process's environment. Additionally,
1228+ /// it will not reflect changes made in a `pre_exec` hook (on Unix platforms).
1229+ ///
1230+ /// Each element is a tuple `(OsString, OsString)` representing an environment variable key and value.
1231+ ///
1232+ /// # Examples
1233+ ///
1234+ /// ```
1235+ /// #![feature(command_resolved_envs)]
1236+ /// use std::process::Command;
1237+ /// use std::ffi::{OsString, OsStr};
1238+ /// use std::env;
1239+ /// use std::collections::HashMap;
1240+ ///
1241+ /// let mut cmd = Command::new("ls");
1242+ /// cmd.env("TZ", "UTC");
1243+ /// unsafe { env::set_var("EDITOR", "vim"); }
1244+ ///
1245+ /// let resolved: HashMap<OsString, OsString> = cmd.get_resolved_envs().collect();
1246+ /// assert_eq!(resolved.get(OsStr::new("TZ")), Some(&OsString::from("UTC")));
1247+ /// assert_eq!(resolved.get(OsStr::new("EDITOR")), Some(&OsString::from("vim")));
1248+ /// ```
1249+ #[ unstable( feature = "command_resolved_envs" , issue = "149070" ) ]
1250+ pub fn get_resolved_envs ( & self ) -> CommandResolvedEnvs {
1251+ self . inner . get_resolved_envs ( )
1252+ }
1253+
12171254 /// Returns the working directory for the child process.
12181255 ///
12191256 /// This returns [`None`] if the working directory will not be changed.
@@ -1367,6 +1404,9 @@ impl<'a> fmt::Debug for CommandEnvs<'a> {
13671404 }
13681405}
13691406
1407+ #[ unstable( feature = "command_resolved_envs" , issue = "149070" ) ]
1408+ pub use imp:: CommandResolvedEnvs ;
1409+
13701410/// The output of a finished process.
13711411///
13721412/// This is returned in a Result by either the [`output`] method of a
0 commit comments