rust-lang / rust-lang/rust

Introduce a interfaces to expose the current `Command` captured env var logic

Open
#149,070 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-tracking-issue S-tracking-needs-to-bake T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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.)

  • ACP: rust-lang/libs-team#194
  • Implementation:
    • get_env_clear: #149074
    • get_resolved_envs: #149362
  • Final comment period (FCP)^1
  • Stabilization PR
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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

This is a tracking issue for std::process::Command APIs; read the ACP in rust-lang/libs-team#194 and the stabilization guidance linked in the issue. The implementation work is already recorded as complete in #149074 and #149362, so the remaining milestone is the final comment period followed by a stabilization PR.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
operating-systems
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.