Skip to content

Commit

Permalink
fix: RunCommand calling scripts with incorrect executable path
Browse files Browse the repository at this point in the history
Fixes python-poetryGH-965

Calling `sys.argv` should run the same program as the currently
running program. To make calling Poetry scripts through RunCommand
match this behavior, we must set `sys.argv[0]` to be the full path of
the executable.

This change makes the behavior of calling a script through `poetry run`
the same as calling a script directly from the .venv/bin.
  • Loading branch information
chdsbd authored and wagnerluis1982 committed Oct 26, 2022
1 parent 1af6e23 commit f2bb5b8
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/poetry/console/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ def _module(self) -> Module:
return module

def run_script(self, script: str | dict[str, str], args: str) -> int:
# Calling `sys.argv` should run the same program as the currently
# running program. To make calling Poetry scripts through RunCommand
# match this behavior, we must set `sys.argv[0]` to be the full path of
# the executable.
full_path_args = args.copy()
full_path_args[0] = self.env._bin(full_path_args[0])

if isinstance(script, dict):
script = script["callable"]

Expand All @@ -57,7 +64,7 @@ def run_script(self, script: str | dict[str, str], args: str) -> int:
cmd += [
"import sys; "
"from importlib import import_module; "
f"sys.argv = {args!r}; {src_in_sys_path}"
f"sys.argv = {full_path_args!r}; {src_in_sys_path}"
f"import_module('{module}').{callable_}()"
]

Expand Down

0 comments on commit f2bb5b8

Please sign in to comment.