Repository navigation
Description
Bug report
Bug description:
This change (935aa45, PR #103179) changed the behavior of shutil.which so that it checks for the extensionless filename first, before checking extensions from PATHEXT. Previously, the extensionless name was not checked at all.
This is causing problems because, at least in my case, there is both an extensionless and extensioned version of the program, and the extensionless version is a bash script (which apparently can't be executed directly on Windows.)
Full Description
On a GitHub Windows shared runner, node.js is installed.
Run Get-ChildItem -Path "C:\Program Files\nodejs"
Directory: C:\Program Files\nodejs
Mode LastWriteTime Length Name
---- ------------- ------ ----
d---- 9/10/2023 11:53 PM node_modules
[... elided ...]
-a--- 8/8/2023 11:20 PM 71207576 node.exe
-a--- 6/23/2023 6:02 AM 702 nodevars.bat
-a--- 8/8/2023 10:22 PM 1365 npm
-a--- 6/23/2023 6:01 AM 483 npm.cmd
-a--- 8/8/2023 10:22 PM 1567 npx
-a--- 6/23/2023 6:01 AM 539 npx.cmd
Here, npm is a bash script, while npm.cmd is a cmd.exe script.
In python 3.11 (and before), shutil.which("npm") returns "C:\\Program Files\\nodejs\\npm.CMD". That can be executed successfully by subprocess.run.
In python 3.12rc3, shutil.which("npm") returns "C:\\Program Files\\nodejs\\npm", which, being a bash script, appears not to be by executable by subprocess.
subprocess.run((shutil.which("npm"), "install"))fails with OSError: [WinError 193] %1 is not a valid Win32 application on python 3.12rc3.
Notes
On the system in question Get-Command npm returns C:\Program Files\nodejs\npm.cmd. So PowerShell gets it right.
(PATHEXT is .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL.)
I'm not absolutely convinced that this is a bug. But again, I'm also not sure how to work around it without essentially reimplementing shutil.which.
CPython versions tested on:
3.12
Operating systems tested on:
Windows
Linked PRs
- gh-109590: Fix incorrect resolution of paths without extensions in shutil.which on win32. #109797
- gh-109590 - Update shutil.which on win32 to only give back PATHEXT matches by default #109995
- [3.12] gh-109590: Update shutil.which on Windows to prefer a PATHEXT extension on executable files (GH-109995) #110202
Activity
dairiki commentedon Sep 20, 2023
More research as to the history of the change:
The original name, without added extension from PATHEXT, was added to the search list in #103179 to address #75586:
But, later in #75586, there is this:
Assuming that is true, then the original name should only be prepended to the search list if it has an extension. Extensionless names should not be considered when searching for an executable.
[-]Changes in the handling of PATHEXT by shutil.which are causing issues on Windows[/-][+]On Windows, shutil.which incorrectly resolves to extensionless files (python > 3.12.0b1)[/+][-]On Windows, shutil.which incorrectly resolves to extensionless files (python > 3.12.0b1)[/-][+]On Windows, shutil.which incorrectly resolves to extensionless files (python >= 3.12.0b1)[/+][-]On Windows, shutil.which incorrectly resolves to extensionless files (python >= 3.12.0b1)[/-][+]On Windows, shutil.which incorrectly resolves to non-executable extensionless files (python >= 3.12.0b1)[/+]csm10495 commentedon Sep 24, 2023
@eryksun can you give some thoughts here?
I'm a bit nervous about this type of case hitting others in 3.12 and wondering your thoughts.
We could add logic to
shutil.whichto do:I think this gets it in-line with what CMD would do, but I'm not 100% on it.
csm10495 commentedon Sep 24, 2023
Some testing with CMD:
So i guess if you give a directory component, it still consults PATHEXT first.
pythongh-109590 - Fix shutil.which to more closely match CMD on win32
pythongh-109590 - Fix shutil.which to more closely match CMD on win32
csm10495 commentedon Sep 24, 2023
I implemented the '.' in pathext to check in this draft pr: #109797
.. but i couldn't replicate the described behavior in CMD in Win 10.0.19045.3448:
I cleared C:\Program Files\nodejs\npm to be an empty file. If it was called it shouldn't really do anything.. but it kept hitting npm.cmd instead.
I'll wait for @eryksun 's thoughts.
csm10495 commentedon Sep 24, 2023
One last thing before bed :)
Maybe it does work but only if the rest of PATHEXT can't resolve something first (as in: it ignores the order in PATHEXT. Even if the dot is first, it will do all the other extensions before checking for an extensionless file).
I renamed npm.cmd to .npm.cmd, then:
If that's the desired behavior, it wouldn't be too hard to update the PR.
csm10495 commentedon Sep 26, 2023
@zooba any thoughts on this?
zooba commentedon Sep 26, 2023
This sounds reasonable at face value.
CMD is not the hallmark of consistent behaviour. I wouldn't want to set it as our standard. If you want CMD behaviour, pass
shell=True(no, don't really).The closer we get to the underlying API behaviour, the happier I'll be - in this case, it's SearchPathW.
csm10495 commentedon Sep 27, 2023
So unfortunately (or fortunately),
SearchPathWdoesn't match CMD behavior here and actually matches Python 3.12's current behavior:Via pywin32:
So it seems fine giving back a the file without an extension. So I guess we match right now.
I can't seem to find any definitive documentation on the expectation of
PATHEXTand extensionless. So I guess at the moment if we're matchingSearchPath.. we match.8 remaining items
zooba commentedon Sep 28, 2023
Having seen the bugs that are getting in vs. being deferred, I don't think we want this one in. The risk far outweighs the value right now.
If we'd found it before RC1, then definitely. Right now, the important thing to do is to make sure that anyone who fixes this in 3.12.0 is still going to work in 3.12.1 (which is why we check if there's already a PATHEXT extension on the requested name).
pythongh-109590 - Update shutil.which on win32 to only give back PATH…
gh-109590: Update shutil.which on Windows to prefer a PATHEXT extensi…
pythongh-109590: Update shutil.which on Windows to prefer a PATHEXT e…
[3.12] gh-109590: Update shutil.which on Windows to prefer a PATHEXT …
fix: do not use shutil.which to find npm binary
fix: do not use shutil.which to find npm binary
fix: use shell=True rather than shutil.which to find npm binary
fix: use shell=True rather than shutil.which to find npm binary
fix: use shell=True rather than shutil.which to find npm binary
fix: use shell=True rather than shutil.which to find npm binary
Test under python 3.12 (#1167)
hugovk commentedon Nov 9, 2023
Closing as the PRs have been merged. Thanks!
pythongh-109590: Update shutil.which on Windows to prefer a PATHEXT e…