Skip to content

docs: clarify path search behavior in std::process::Command::new#153469

Open
Albab-Hasan wants to merge 1 commit intorust-lang:mainfrom
Albab-Hasan:doc/command-path-search-behavior
Open

docs: clarify path search behavior in std::process::Command::new#153469
Albab-Hasan wants to merge 1 commit intorust-lang:mainfrom
Albab-Hasan:doc/command-path-search-behavior

Conversation

@Albab-Hasan
Copy link
Contributor

@Albab-Hasan Albab-Hasan commented Mar 6, 2026

the existing docs mentioned that PATH is searched in an "os defined way" and that it could be controlled by setting PATH on the command but never explained which PATH is actually used.

on unix the key thing to understand is that when you modify the childs environment (via env(), env_remove(), or env_clear()), the PATH search uses whatever PATH ends up in the child's environment not the parents. so if you call env_clear() and forget to set PATH, you don't get the parents PATH as a fallback; you get the OS default (typically /bin:/usr/bin) which often won't find what you need.

the three cases are now documented:

  • unmodified env: child inherits parents PATH, that gets searched
  • PATH explicitly set via env(): the new value is searched
  • PATH removed (env_clear or env_remove): falls back to OS default, not the parents PATH

on windows rust resolves the executable before spawning rather than letting CreateProcessW do it. the search order is: childs PATH (if explicitly set) first then system-defined directories then the parents PATH. the existing note about issue #37519 is preserved.

limitations in this doc:

  • the unix fallback path behavior ("typically /bin:/usr/bin") is verified for linux/glibc. behavior on macOS, BSD, and musl is similar in practice but not fully confirmed here.
  • the windows search order is summarized as "system-defined directories" which actually includes the application directory (the directory of the calling process's executable) as a distinct step before the system dirs that detail is omitted for brevity.
  • posix_spawnp PATH source (calling process environ vs envp argument) is ambiguous in the POSIX spec; the behavior here is inferred from the guard in unix.rs that bypasses posix_spawn when PATH has been modified.

closes #137286 (hopefully)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 6, 2026
@rustbot
Copy link
Collaborator

rustbot commented Mar 6, 2026

r? @joboet

rustbot has assigned @joboet.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ChrisDenton, libs
  • @ChrisDenton, libs expanded to 8 candidates
  • Random selection from Mark-Simulacrum, joboet

@rustbot

This comment has been minimized.

the existing docs mentioned that PATH is searched "in an OS-defined way"
and that it could be controlled by setting PATH on the Command, but never
explained *which* PATH is actually used. this is the part that surprises
people.

on unix, the key thing to understand is that when you modify the child's
environment (via env(), env_remove(), or env_clear()), the PATH search
uses whatever PATH ends up in the child's environment — not the parent's.
so if you call env_clear() and forget to set PATH, you don't get the
parent's PATH as a fallback; you get the OS default (typically
/bin:/usr/bin), which often won't find what you need.

the three cases are now documented explicitly:
- unmodified env: child inherits parent's PATH, that gets searched
- PATH explicitly set via env(): the new value is searched
- PATH removed (env_clear or env_remove): falls back to OS default,
  not the parent's PATH

on windows, rust resolves the executable before spawning rather than
letting CreateProcessW do it. the search order is: child's PATH (if
explicitly set) first, then system-defined directories, then the parent's
PATH. the existing note about the windows limitation is preserved.

a few honest limitations in this doc:
- the unix fallback path behavior ("typically /bin:/usr/bin") is verified
  for linux/glibc; behavior on macOS, BSD, and musl is similar in practice
  but not exhaustively confirmed here
- the windows search order is summarized as "system-defined directories"
  which actually includes the application directory as a distinct step —
  that detail is omitted for brevity
- posix_spawnp's PATH source (calling process environ vs envp argument) is
  ambiguous in the POSIX spec; the behavior here is inferred from the guard
  in unix.rs that bypasses posix_spawn when PATH has been modified
@Albab-Hasan Albab-Hasan force-pushed the doc/command-path-search-behavior branch from f2c6cb2 to 4d5ce27 Compare March 6, 2026 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Document the PATH that Command searchs

3 participants