Skip to content

Commit 13790be

Browse files
authored
Rollup merge of #109272 - schneems:schneems/add-docs-to-command-env-methods, r=Amanieu
Add Command environment variable inheritance docs The interaction between the environment variable methods can be confusing. Specifically `env_clear` and `remove_env` have a side effects not mentioned: they disable inheriting environment variables from the parent process. I wanted to fully document this behavior as well as explain relevant edge cases in each of the `Command` env methods. This is further confused by the return of `get_envs` which will return key/None if `remove_env` has been used, but an empty iterator if `env_clear` has been called. Or a non-empty iterator if `env_clear` was called and later explicit mappings are added. Currently there is no way (that I'm able to find) of observing whether or not the internal `env_clear=true` been toggled on the `Command` struct via its public API. Ultimately environment variable mappings can be in one of several states: - Explicitly set value (via `envs` / `env`) will take precedence over parent mapping - Not explicitly set, will inherit mapping from parent - Explicitly removed via `remove_env`, this single mapping will not inherit from parent - Implicitly removed via `env_clear`, no mappings will inherit from parent I tried to represent this in the relevant sections of the docs. This is my second-ever doc PR (whoop!). I'm happy to take specific or general doc feedback. Also happy to explain the logic behind any changes or additions I made.
2 parents 7bfccb3 + e612d78 commit 13790be

File tree

1 file changed

+61
-15
lines changed

1 file changed

+61
-15
lines changed

library/std/src/process.rs

+61-15
Original file line numberDiff line numberDiff line change
@@ -652,10 +652,19 @@ impl Command {
652652
self
653653
}
654654

655-
/// Inserts or updates an environment variable mapping.
655+
/// Inserts or updates an explicit environment variable mapping.
656656
///
657-
/// Note that environment variable names are case-insensitive (but case-preserving) on Windows,
658-
/// and case-sensitive on all other platforms.
657+
/// This method allows you to add an environment variable mapping to the spawned process or
658+
/// overwrite a previously set value. You can use [`Command::envs`] to set multiple environment
659+
/// variables simultaneously.
660+
///
661+
/// Child processes will inherit environment variables from their parent process by default.
662+
/// Environment variables explicitly set using [`Command::env`] take precedence over inherited
663+
/// variables. You can disable environment variable inheritance entirely using
664+
/// [`Command::env_clear`] or for a single key using [`Command::env_remove`].
665+
///
666+
/// Note that environment variable names are case-insensitive (but
667+
/// case-preserving) on Windows and case-sensitive on all other platforms.
659668
///
660669
/// # Examples
661670
///
@@ -679,7 +688,19 @@ impl Command {
679688
self
680689
}
681690

682-
/// Adds or updates multiple environment variable mappings.
691+
/// Inserts or updates multiple explicit environment variable mappings.
692+
///
693+
/// This method allows you to add multiple environment variable mappings to the spawned process
694+
/// or overwrite previously set values. You can use [`Command::env`] to set a single environment
695+
/// variable.
696+
///
697+
/// Child processes will inherit environment variables from their parent process by default.
698+
/// Environment variables explicitly set using [`Command::envs`] take precedence over inherited
699+
/// variables. You can disable environment variable inheritance entirely using
700+
/// [`Command::env_clear`] or for a single key using [`Command::env_remove`].
701+
///
702+
/// Note that environment variable names are case-insensitive (but case-preserving) on Windows
703+
/// and case-sensitive on all other platforms.
683704
///
684705
/// # Examples
685706
///
@@ -716,7 +737,18 @@ impl Command {
716737
self
717738
}
718739

719-
/// Removes an environment variable mapping.
740+
/// Removes an explicitly set environment variable and prevents inheriting it from a parent
741+
/// process.
742+
///
743+
/// This method will remove the explicit value of an environment variable set via
744+
/// [`Command::env`] or [`Command::envs`]. In addition, it will prevent the spawned child
745+
/// process from inheriting that environment variable from its parent process.
746+
///
747+
/// After calling [`Command::env_remove`], the value associated with its key from
748+
/// [`Command::get_envs`] will be [`None`].
749+
///
750+
/// To clear all explicitly set environment variables and disable all environment variable
751+
/// inheritance, you can use [`Command::env_clear`].
720752
///
721753
/// # Examples
722754
///
@@ -736,7 +768,17 @@ impl Command {
736768
self
737769
}
738770

739-
/// Clears the entire environment map for the child process.
771+
/// Clears all explicitly set environment variables and prevents inheriting any parent process
772+
/// environment variables.
773+
///
774+
/// This method will remove all explicitly added environment variables set via [`Command::env`]
775+
/// or [`Command::envs`]. In addition, it will prevent the spawned child process from inheriting
776+
/// any environment variable from its parent process.
777+
///
778+
/// After calling [`Command::env_remove`], the iterator from [`Command::get_envs`] will be
779+
/// empty.
780+
///
781+
/// You can use [`Command::env_remove`] to clear a single mapping.
740782
///
741783
/// # Examples
742784
///
@@ -988,17 +1030,21 @@ impl Command {
9881030
CommandArgs { inner: self.inner.get_args() }
9891031
}
9901032

991-
/// Returns an iterator of the environment variables that will be set when
992-
/// the process is spawned.
1033+
/// Returns an iterator of the environment variables explicitly set for the child process.
1034+
///
1035+
/// Environment variables explicitly set using [`Command::env`], [`Command::envs`], and
1036+
/// [`Command::env_remove`] can be retrieved with this method.
1037+
///
1038+
/// Note that this output does not include environment variables inherited from the parent
1039+
/// process.
9931040
///
994-
/// Each element is a tuple `(&OsStr, Option<&OsStr>)`, where the first
995-
/// value is the key, and the second is the value, which is [`None`] if
996-
/// the environment variable is to be explicitly removed.
1041+
/// Each element is a tuple key/value pair `(&OsStr, Option<&OsStr>)`. A [`None`] value
1042+
/// indicates its key was explicitly removed via [`Command::env_remove`]. The associated key for
1043+
/// the [`None`] value will no longer inherit from its parent process.
9971044
///
998-
/// This only includes environment variables explicitly set with
999-
/// [`Command::env`], [`Command::envs`], and [`Command::env_remove`]. It
1000-
/// does not include environment variables that will be inherited by the
1001-
/// child process.
1045+
/// An empty iterator can indicate that no explicit mappings were added or that
1046+
/// [`Command::env_clear`] was called. After calling [`Command::env_clear`], the child process
1047+
/// will not inherit any environment variables from its parent process.
10021048
///
10031049
/// # Examples
10041050
///

0 commit comments

Comments
 (0)