Skip to content

std::process::Command on Windows does not correctly find the program executable #104358

Closed
@pfmoore

Description

@pfmoore

If I run the following code:

use std::process::Command;

fn main() {
    let mut c = Command::new("py");
    c.arg("-V");
    println!("{:?}", c.get_program());
    println!("Command: {:#?}", c);
    let s = c.status();
    println!("Status: {:#?}", s);
}

I expect the C:\Windows\py.exe program to be run with the argument -V, which will print the version of Python I have.

Instead, this happened:

❯ cargo run
   Compiling exxx v0.1.0 (C:\Work\Scratch\exxx)
    Finished dev [unoptimized + debuginfo] target(s) in 0.39s
     Running `target\debug\exxx.exe`
"py"
Command: "py" "-V"
Unable to create process using 'C:\Users\Gustav\AppData\Local\Programs\Python\Python311\py" -V': The system cannot find the file specified.

Status: Ok(
    ExitStatus(
        ExitStatus(
            101,
        ),
    ),
)

The code appears to have somehow decided that the executable is C:\Users\Gustav\AppData\Local\Programs\Python\Python311\py" -V. I understand from #37519 and #87704 that rust implements its own search for the executable, but I'm not clear why it failed to find C:\Windows\py.exe. C:\Windows is the third entry on my PATH, and where py (in the cmd shell) and Get-Command (in powershell) both find the executable.

Meta

rustc --version --verbose:

rustc 1.63.0 (4b91a6ea7 2022-08-08)
binary: rustc
commit-hash: 4b91a6ea7258a947e59c6522cd5898e7c0a6a88f
commit-date: 2022-08-08
host: x86_64-pc-windows-msvc
release: 1.63.0
LLVM version: 14.0.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions