Skip to content

ACP: process::resolve_exe and Command::resolve_in_parent_path #591

Closed
@ChrisDenton

Description

@ChrisDenton

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

  1. app_dir, system dirs, PATH
  2. child_path, app dir, system dirs, PATH
  3. 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

We could provide a way to get an absolute executable path from iterator over directories (e.g. as returned by split_paths).
This would enable inspecting and logging the path before the command is spawned.

// in std::process

/// Resolve the program to an absolute path by searching
/// the given paths for the executable.
pub fn resolve_exe(program: S, paths: I) -> io::Result<PathBuf>
where
S: AsRef<Path>.
I: IntoIterator<Item = P>,
P: AsRef<Path>,
{/*...*/

Alternatively have a function on Command that tells it the paths to use to resolve the executable. It is better for some platforms if Command can be given the list of paths to search:

// in std::process

impl Command {
    /// 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;
}

Either way, it would be useful to have a helper for a common case:

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;
}

Alternatives

Don't provide a way to pass arbitrary paths and instead rely purely on boolean settings on Command, E.g. have a resolve_in_system_paths(bool) function on Command (maybe a Windows extension) which together with resolve_in_parent_path would allow selecting the wanted behaviour in many cases.

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ACP-acceptedAPI Change Proposal is accepted (seconded with no objections)T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions