Skip to content

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

Merged
rust-bors[bot] merged 2 commits intorust-lang:mainfrom
Albab-Hasan:doc/command-path-search-behavior
Apr 15, 2026
Merged

docs: clarify path search behavior in std::process::Command::new#153469
rust-bors[bot] merged 2 commits intorust-lang:mainfrom
Albab-Hasan:doc/command-path-search-behavior

Conversation

@Albab-Hasan
Copy link
Copy Markdown
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
Copy Markdown
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.

@Albab-Hasan Albab-Hasan force-pushed the doc/command-path-search-behavior branch from f2c6cb2 to 4d5ce27 Compare March 6, 2026 06:23
@joboet
Copy link
Copy Markdown
Member

joboet commented Mar 12, 2026

r? @ChrisDenton
You're familiar with the details of this, I believe?

@rustbot rustbot assigned ChrisDenton and unassigned joboet Mar 12, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Mar 12, 2026

ChrisDenton is not on the review rotation at the moment.
They may take a while to respond.

Copy link
Copy Markdown
Member

@ChrisDenton ChrisDenton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tricky bit here is that we do want to reserve the right to change it in the future. At this point the Windows implementation is mostly for historic reasons. It was in a fairly broken state for awhile and while that has now been cleaned up, I'm not sure it's in the end state we want (then again. I'm not sure how much we can change it without breaking people).

I'm going to nominate this for libs-api to at least discuss the Unix behaviour. I'm not sure how much we want to guarantee there.

View changes since this review

@ChrisDenton ChrisDenton added the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Apr 14, 2026
@Albab-Hasan
Copy link
Copy Markdown
Contributor Author

@ChrisDenton would a caveat on windows + keeping Unix as is address your concern or do you want unix stripped back too? just want to know your opinion. gladly will wait for libs api decision.

@ChrisDenton
Copy link
Copy Markdown
Member

There are a wide variety of Unix (and unix-like) systems so my personal instinct would favour being cautious about what we guarantee. But that is a very tentative opinion. And I do think it's useful to document the current behaviour in any case.

@ChrisDenton
Copy link
Copy Markdown
Member

This was discussed today in today's @rust-lang/libs-api meeting. It was felt we should keep the option open to make changes in the future, even for Unix where changes are much less likely.

Could you move this under the "Platform-specific behavior" heading and start with a paragraph that says these details may change in the future?

@Albab-Hasan Albab-Hasan force-pushed the doc/command-path-search-behavior branch from 4d5ce27 to 97761a0 Compare April 14, 2026 17:05
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 14, 2026

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@Albab-Hasan
Copy link
Copy Markdown
Contributor Author

@ChrisDenton moved everything under "# Platform-specific behavior" and added the "may change" caveat like you said. also corrected the windows search order. the application directory step was missing from my original. verified against search_paths() in the windows impl: it goes child PATH → app directory → system dirs → windows dir → parent PATH. also added a note that clearing PATH on windows still falls through to the parent PATH at step 5. which is different from the unix execvp fallback behavior.

Copy link
Copy Markdown
Member

@ChrisDenton ChrisDenton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me thanks! One nit:

View changes since this review

Comment thread library/std/src/process.rs Outdated
@Albab-Hasan
Copy link
Copy Markdown
Contributor Author

@ChrisDenton sorry dont even know how that ended up in the pr lol. probably forgot to clean it up after copy pasting from my notes

@ChrisDenton ChrisDenton removed the I-libs-api-nominated Nominated for discussion during a libs-api team meeting. label Apr 15, 2026
@ChrisDenton
Copy link
Copy Markdown
Member

Thanks for working on this. I've did a last check and this all looks good so...

@bors r+ rollup

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Apr 15, 2026

📌 Commit a34d15f has been approved by ChrisDenton

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 15, 2026
rust-bors Bot pushed a commit that referenced this pull request Apr 15, 2026
Rollup of 13 pull requests

Successful merges:

 - #154882 (Gate tuple const params behind `min_adt_const_params` feature)
 - #155259 (explicit-tail-calls: disable two tests on LoongArch)
 - #155293 (fix arch names in cfg pretty printer)
 - #155314 (`BorrowedBuf`: Update outdated safety comments in `set_init` users.)
 - #153469 (docs: clarify path search behavior in std::process::Command::new)
 - #154765 (Clarify ascii whitespace exclusion of vertical tab in the doc)
 - #155172 (Some small nits for supertrait_item_shadowing, and additional testing)
 - #155279 (Test/lexer unicode pattern white space)
 - #155280 (Tests for precise-capture through RPIT and TAIT)
 - #155301 (Delete unused `rustc_trait_selection` errors.)
 - #155303 (remove ibraheemdev from review rotation)
 - #155304 (remove PointeeParser)
 - #155319 (Remove dead diagnostic structs.)
@rust-bors rust-bors Bot merged commit 3f19aa5 into rust-lang:main Apr 15, 2026
11 checks passed
@rustbot rustbot added this to the 1.97.0 milestone Apr 15, 2026
rust-timer added a commit that referenced this pull request Apr 15, 2026
Rollup merge of #153469 - Albab-Hasan:doc/command-path-search-behavior, r=ChrisDenton

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

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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

4 participants