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

Improve "run" behavior on Windows #2727

Merged
merged 4 commits into from
Aug 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/2718.behavior
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fallback to shell mode if `run` fails with Windows error 193 to handle non-executable commands. This should improve usability on Windows, where some users run non-executable files without specifying a command, relying on Windows file association to choose the current command.
26 changes: 21 additions & 5 deletions pipenv/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2091,15 +2091,31 @@ def inline_activate_virtual_environment():
os.environ["VIRTUAL_ENV"] = root


def do_run_nt(script):
def _launch_windows_subprocess(script):
import subprocess

command = system_which(script.command)
options = {"universal_newlines": True}
if command: # Try to use CreateProcess directly if possible.
p = subprocess.Popen([command] + script.args, **options)
else: # Command not found, maybe this is a shell built-in?
p = subprocess.Popen(script.cmdify(), shell=True, **options)

# Command not found, maybe this is a shell built-in?
if not command:
return subprocess.Popen(script.cmdify(), shell=True, **options)

# Try to use CreateProcess directly if possible. Specifically catch
# Windows error 193 "Command is not a valid Win32 application" to handle
# a "command" that is non-executable. See pypa/pipenv#2727.
try:
return subprocess.Popen([command] + script.args, **options)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it's a command, but not an executable? I think pythonfinder.which actually handles this correctly then on windows whenever we get around to figuring that out => it will find things even if the execute bit isn't set as just from testing I found that to be unreliable

Copy link
Member Author

@uranusjr uranusjr Aug 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the cause here is that the DOS CLI does not really have a notion of executable files. As far as it concerns, everything can be launched, and it’s up to the operating system to decide what to do. As a consequence, where.exe find all sorts of things, not just applications. It would happily return django-admin.py if it’s in PATH, and subprocess.Popen (actually the CreateProcess API it uses under the hood) would be hung to dry.

So the root issue here is that we are mixing two paradigms when implementing Pipenv for Windows because the deep roots of Windows lacking a real user-facing “executable” flag. I’d doubt even a re-implemented which function would help, unless you dig deep into Windows API to actually detect if a file is a real application (by peeking into the PE header, for example).

except WindowsError as e:
if e.winerror != 193:
raise

# Try shell mode to use Windows's file association for file launch.
return subprocess.Popen(script.cmdify(), shell=True, **options)


def do_run_nt(script):
p = _launch_windows_subprocess(script)
p.communicate()
sys.exit(p.returncode)

Expand Down