Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
pfmoore opened this issue Nov 13, 2022 · 4 comments
Closed
Labels
C-bug Category: This is a bug.

Comments

@pfmoore
Copy link

pfmoore commented Nov 13, 2022

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
@ChrisDenton
Copy link
Member

What's the output of py -V on the command line?

@pfmoore
Copy link
Author

pfmoore commented Nov 13, 2022

Python 3.11.0

@ChrisDenton
Copy link
Member

Ok, so this status means that Rust ran the py command successfully but that py returned an error (101).

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

The following line seems to be coming from the py launcher itself (and not Rust):

Unable to create process using 'C:\Users\Gustav\AppData\Local\Programs\Python\Python311\py" -V': The system cannot find the file specified.

The fact py -V works on the command line suggests that it's something different in the environment. You could try setting the PYLAUNCHER_DEBUG environment variable to 1 and noting the differences when run from Rust vs. on the command line. I think this is ultimately a Python launcher issue (possibly related to python/cpython#99442) though it's also possible Rust should be doing something differently.

In short, Rust is correctly running C:\Windows\py.exe but that application is failing for some reason.

@pfmoore
Copy link
Author

pfmoore commented Nov 13, 2022

Hmm, interesting. Thanks. I thought I'd managed to eliminate the launcher as the issue, apparently not :-(

Yeah, PYLAUNCHER_DEBUG shows the problem is clearly with the launcher. I should have thought to try that. The Python 3.11 launcher is a complete rewrite, and it's had some weird bugs. I thought we'd caught the worst ones though. This looks similar to one I've seen before.

Thanks for the help, and my apologies for the incorrect analysis, pinning the blame on rust!

I'll close this in favour of python/cpython#99442

@pfmoore pfmoore closed this as completed Nov 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants