Description
The API which Command exposes implies that environment variables are captured from the parent process at the point where it is constructed, and then may be subsequently modified through the use of env
/env_remove
/env_clear
.
However, in reality the environment variables are captured lazily, when the first environment modifier is applied to the command. There's also no way to explicitly trigger the environment capture without actually modifying the environment.
This results in unpredictable behaviour if environment variables are modified between Command construction, and Command execution. Perhaps this is done as an optimization. However, spawning a new process far outweighs the cost of copying a few strings.
Environment variables are global state: it's a bad idea for accesses to global state to happen at an unpredictable time.
Finally, regardless of whether the behaviour is changed, it should be clearly documented what the behaviour is.