Feature gate: #![feature(command_resolved_envs)]
This is a tracking issue for the API Change Proposal (ACP) rust-lang/libs-team#194.
This feature adds two methods to std::process::Command to expose environment variable resolution logic, addressing the inability to observe the effects of env_clear() and the lack of a single source of truth for environment variable resolution.
Public API
// std::process
impl Command {
pub fn get_env_clear(&self) -> bool;
pub fn get_resolved_envs(&self) -> CommandResolvedEnvs;
}
// `get_resolved_envs` returns a concrete iterator type:
pub struct CommandResolvedEnvs { /* .. */ }
impl Iterator for CommandResolvedEnvs {
type Item = (OsString, OsString);
// ..
}
Explanation:
get_env_clear This makes sure people have all the information Command stores.
get_resolved_envs This should include documentation that says it returns the environment as it would be if the command were executed at that point, and will not match if the environment is subsequently changed (including in a pre_exec hook). This avoids duplicating the Command logic.
Steps / History
(Remember to update the S-tracking-* label when checking boxes.)
Unresolved Questions
Both questions raised at filing time have been resolved during implementation:
Should this use impl Iterator in the return type, or a concrete iterator type? Resolved: get_resolved_envs returns a concrete iterator type (CommandResolvedEnvs), matching the precedent set by std::env::vars_os / std::env::VarsOs and leaving room to add impls such as Debug. See #149362 and this comment.
Should we allow borrowing from std::process::Command (or other sources) in the returned iterator, rather than requiring (OsString, OsString) which implies fresh allocations for each environment variable and value on every iteration? Resolved: the iterator yields owned (OsString, OsString), consistent with std::env::vars_os. Anyone able to spawn a process can afford these allocations, which are negligible relative to process spawning.
Feature gate:
#![feature(command_resolved_envs)]This is a tracking issue for the API Change Proposal (ACP) rust-lang/libs-team#194.
This feature adds two methods to
std::process::Commandto expose environment variable resolution logic, addressing the inability to observe the effects ofenv_clear()and the lack of a single source of truth for environment variable resolution.Public API
Explanation:
get_env_clearThis makes sure people have all the information Command stores.get_resolved_envsThis should include documentation that says it returns the environment as it would be if the command were executed at that point, and will not match if the environment is subsequently changed (including in a pre_exec hook). This avoids duplicating the Command logic.Steps / History
(Remember to update the
S-tracking-*label when checking boxes.)get_env_clear: Add Command::get_env_clear #149074get_resolved_envs: Add Command::get_resolved_envs #149362Unresolved Questions
Both questions raised at filing time have been resolved during implementation:
Should this useResolved:impl Iteratorin the return type, or a concrete iterator type?get_resolved_envsreturns a concrete iterator type (CommandResolvedEnvs), matching the precedent set bystd::env::vars_os/std::env::VarsOsand leaving room to add impls such asDebug. See #149362 and this comment.Should we allow borrowing fromResolved: the iterator yields ownedstd::process::Command(or other sources) in the returned iterator, rather than requiring(OsString, OsString)which implies fresh allocations for each environment variable and value on every iteration?(OsString, OsString), consistent withstd::env::vars_os. Anyone able to spawn a process can afford these allocations, which are negligible relative to process spawning.Footnotes
https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html ↩