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

windows: Do not check if the extension is executable #56

Closed
N3xed opened this issue Apr 11, 2022 · 12 comments · Fixed by #57
Closed

windows: Do not check if the extension is executable #56

N3xed opened this issue Apr 11, 2022 · 12 comments · Fixed by #57

Comments

@N3xed
Copy link
Contributor

N3xed commented Apr 11, 2022

The current implementation for windows checks all executable extensions in the environment variable PATHEXT, but if the binary path already has an extension that extension is currently ignored if it is not in that environment variable.

if has_executable_extension(&p, &PATH_EXTENSIONS) {

I'd argue this check is detrimental as it prevents some executable files (.py files in my case) to be found. These files may not be executable directly but through a shell.

Now I really don't know what use cases this check has, but I'd nonetheless suggest replacing it with the following:

if p.extension().is_some() {

so forward the file if it has an extension, otherwise try all extensions in PATHEXT appended to file.

An alternative would be, if the extension is not in PATH_EXTENSIONS and file does actually have an extension, add it to the beginning of the iterator with all executable extensions:

        paths
            .into_iter()
            .flat_map(move |p| -> Box<dyn Iterator<Item = _>> {
                // Check if path already have executable extension
                if has_executable_extension(&p, &PATH_EXTENSIONS) {
                    Box::new(iter::once(p))
                } else {
                    // Appended paths with windows executable extensions.
                    // e.g. path `c:/windows/bin` will expend to:
                    // c:/windows/bin.COM
                    // c:/windows/bin.EXE
                    // c:/windows/bin.CMD
                    // ...
                    let bare_file = p.extension().map(|_| p.clone());
                    Box::new(
                        bare_file
                            .into_iter()
                            .chain(PATH_EXTENSIONS.iter().map(move |e| {
                                // Append the extension.
                                let mut p = p.clone().into_os_string();
                                p.push(e);

                                PathBuf::from(p)
                            })),
                    )
                }
            })

Finally, another alternative is, and this would require more effort, to make this behavior configurable.

@harryfei
Copy link
Owner

harryfei commented Apr 12, 2022

There is one question: does xxx.py file belong to exectuables if "py" is not in PATHEXT?

@Xaeroxe
Copy link
Collaborator

Xaeroxe commented Apr 12, 2022

cmd treats it as an executable, so I take that as a clear signal that the answer is yes. On my machine it is in PATHEXT. So my point is moot.

@harryfei harryfei reopened this Apr 12, 2022
@harryfei
Copy link
Owner

harryfei commented Apr 12, 2022

Is searching "xxx.py.EXE" a good behavior if "py" is not in PATHEXT?

@Xaeroxe
Copy link
Collaborator

Xaeroxe commented Apr 12, 2022

Probably. The which command which we are basing ourselves upon specifically states that it returns the file which would be executed if this were typed at the shell. "The shell" is technically ambiguous, but it's reasonable to infer that on Windows this would be the behavior of cmd.exe. cmd.exe won't treat py as executable if it isn't in the PATHEXT, so at minimum this would be a surprising result.

We should probably revert #57.

@Xaeroxe
Copy link
Collaborator

Xaeroxe commented Apr 12, 2022

Well, wait, my cmd.exe is still executing .py files even after I remove them from my PATHEXT.

@Xaeroxe
Copy link
Collaborator

Xaeroxe commented Apr 12, 2022

Okay, so if it's not inside PATHEXT Windows falls back to using the default file handler for the extension which on some systems will "execute" the file, even though technically the file is just being opened. It's worth noting it will only do this if the extension was specified at the cmd prompt. Which means that the behavior in #57 does in fact match cmd.

@harryfei
Copy link
Owner

harryfei commented Apr 12, 2022

Okay, so if it's not inside PATHEXT Windows falls back to using the default file handler for the extension which on some systems will "execute" the file, even though technically the file is just being opened.

Does it mean we should trade every file with extension as executable file on Windows?

@Xaeroxe
Copy link
Collaborator

Xaeroxe commented Apr 12, 2022

A good question! At this rate I'm not even sure what "executable" means on Windows anymore. If you double click and open a word doc you wouldn't say it's "executing" but a python script you double clicked on is definitely executing. For most files it'd be weird to open them and be like "here's your executable!" but a python script can definitely be executed by opening it.

I don't know. It feels weird to carve out an exception just for python files, as there are other script interpreters that might have a similar setup. The question is actually kind of complicated and I think there's strong justification for both approaches.

@harryfei
Copy link
Owner

harryfei commented Apr 12, 2022

Personally, I'm not familiar with Windows.We need have a definition of executable for windows.
Wait the OP to describe his opinion

@Xaeroxe
Copy link
Collaborator

Xaeroxe commented Apr 12, 2022

FWIW the Windows equivalent to which is where.exe, and where doesn't actually try to determine if the file is executable.

C:\Users\kiese>where kernel32.dll
C:\Windows\System32\kernel32.dll

C:\Users\kiese>where C_28599.NLS
C:\Windows\System32\C_28599.NLS

@harryfei
Copy link
Owner

harryfei commented Apr 13, 2022

Aligning with where.exe maybe a good choice.

We could not to make a decision very soon. Let others leave their opinions.

@Xaeroxe Xaeroxe mentioned this issue Oct 9, 2023
@Xaeroxe
Copy link
Collaborator

Xaeroxe commented Oct 17, 2023

Closed in #83

@Xaeroxe Xaeroxe closed this as completed Oct 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants