|
161 | 161 | mod tests; |
162 | 162 |
|
163 | 163 | use crate::convert::Infallible; |
164 | | -use crate::ffi::OsStr; |
| 164 | +use crate::ffi::{OsStr, OsString}; |
165 | 165 | use crate::io::prelude::*; |
166 | 166 | use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut}; |
167 | 167 | use crate::num::NonZero; |
@@ -1157,7 +1157,8 @@ impl Command { |
1157 | 1157 | /// [`Command::env_remove`] can be retrieved with this method. |
1158 | 1158 | /// |
1159 | 1159 | /// Note that this output does not include environment variables inherited from the parent |
1160 | | - /// process. |
| 1160 | + /// process. To see the full list of environment variables, including those inherited from the |
| 1161 | + /// parent process, use [`Command::get_resolved_envs`]. |
1161 | 1162 | /// |
1162 | 1163 | /// Each element is a tuple key/value pair `(&OsStr, Option<&OsStr>)`. A [`None`] value |
1163 | 1164 | /// indicates its key was explicitly removed via [`Command::env_remove`]. The associated key for |
@@ -1186,6 +1187,42 @@ impl Command { |
1186 | 1187 | CommandEnvs { iter: self.inner.get_envs() } |
1187 | 1188 | } |
1188 | 1189 |
|
| 1190 | + /// Returns an iterator of the environment variables that will be set when the process is spawned. |
| 1191 | + /// |
| 1192 | + /// This returns the environment as it would be if the command were executed at the time of calling |
| 1193 | + /// this method. The returned environment includes: |
| 1194 | + /// - All inherited environment variables from the parent process (unless [`Command::env_clear`] was called) |
| 1195 | + /// - All environment variables explicitly set via [`Command::env`] or [`Command::envs`] |
| 1196 | + /// - Excluding any environment variables removed via [`Command::env_remove`] |
| 1197 | + /// |
| 1198 | + /// Note that the returned environment is a snapshot at the time this method is called and will not |
| 1199 | + /// reflect any subsequent changes to the `Command` or the parent process's environment. Additionally, |
| 1200 | + /// it will not reflect changes made in a `pre_exec` hook (on Unix platforms). |
| 1201 | + /// |
| 1202 | + /// Each element is a tuple `(OsString, OsString)` representing an environment variable key and value. |
| 1203 | + /// |
| 1204 | + /// # Examples |
| 1205 | + /// |
| 1206 | + /// ``` |
| 1207 | + /// #![feature(command_get_resolved_envs)] |
| 1208 | + /// use std::process::Command; |
| 1209 | + /// use std::ffi::{OsString, OsStr}; |
| 1210 | + /// use std::env; |
| 1211 | + /// use std::collections::HashMap; |
| 1212 | + /// |
| 1213 | + /// let mut cmd = Command::new("ls"); |
| 1214 | + /// cmd.env("TZ", "UTC"); |
| 1215 | + /// unsafe { env::set_var("EDITOR", "vim"); } |
| 1216 | + /// |
| 1217 | + /// let resolved: HashMap<OsString, OsString> = cmd.get_resolved_envs().collect(); |
| 1218 | + /// assert_eq!(resolved.get(OsStr::new("TZ")), Some(&OsString::from("UTC"))); |
| 1219 | + /// assert_eq!(resolved.get(OsStr::new("EDITOR")), Some(&OsString::from("vim"))); |
| 1220 | + /// ``` |
| 1221 | + #[unstable(feature = "command_get_resolved_envs", issue = "149070")] |
| 1222 | + pub fn get_resolved_envs(&self) -> impl Iterator<Item = (OsString, OsString)> { |
| 1223 | + self.inner.get_resolved_envs() |
| 1224 | + } |
| 1225 | + |
1189 | 1226 | /// Returns the working directory for the child process. |
1190 | 1227 | /// |
1191 | 1228 | /// This returns [`None`] if the working directory will not be changed. |
|
0 commit comments