Description
Proposal
Problem statement
std::process::Command
has the surprising behaviour of searching the child's PATH
environment variable. Sometimes this is not wanted. Additionally rust on Windows has some further oddities in how it searches for executables whereas sometimes users do just want to look at the PATH
.
Motivating examples or use cases
If you want to set child PATH such that it may not contain the application you want to run then you must manually find the binary. The following will panic on Unix systems:
std::process::Command::new("echo")
.arg("hello world")
.env("PATH", "")
.spawn()
.unwrap(); // panic!
Windows background
There are three basic search strategies people encounter on Windows (note here child_path
is the PATH
that's set on the child process, whereas PATH
is from the parent's environment):
- app_dir, system dirs,
PATH
child_path
, app dir, system dirs,PATH
PATH
The first is what happens when you use Command
without setting a child PATH
.
The second is what happens when you do.
The third can be kind of emulated via the second by using cmd.env(env::var("PATH").unwrap())
.
People want the first for compatibility with other Windows applications and for when they do want to prioritise the app directory or find a system application.
People want the third when they want to do what powershell and Linux does or they explicitly don't want the system binary. For example, some varieties of Windows have WSL bash.exe in the system directory whereas people want to run git bash.exe.
The second can be used to kinda emulate searching PATH
, if you don't mind giving up the ability to set the child PATH
to something different.
Solution sketch
// in std::process
impl Command {
/// If `PATH` is set on the child and `program` is not a path
/// then force the program to be resolved as if the child `PATH` wasn't set.
fn resolve_in_parent_path(&mut self, use_parent: bool) -> &mut Self;
/// Resolve the program by searching only the given paths for the executable.
/// `paths` should be in the same format as `PATH`.
fn resolve_in_paths(&mut self, paths: impl AsRef<OsStr>) -> &mut Self;
}
Alternatives
We could have a resolve_exe
function that takes an iterator of paths and returns an absolute path to the executable.
This would enable inspecting and logging the path before the command is spawned.
However, it's better for some platforms if Command
can be given the list of paths to search.
Links and related work
What happens now?
This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.
Possible responses
The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):
- We think this problem seems worth solving, and the standard library might be the right place to solve it.
- We think that this probably doesn't belong in the standard library.
Second, if there's a concrete solution:
- We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
- We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.