Skip to content

Commit 7273f41

Browse files
chdsbdwagnerluis1982
authored andcommitted
fix: RunCommand calling scripts with incorrect executable path
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.
1 parent 28b0b1d commit 7273f41

File tree

1 file changed

+8
-1
lines changed
  • src/poetry/console/commands

1 file changed

+8
-1
lines changed

src/poetry/console/commands/run.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ def _module(self) -> Module:
4545
return module
4646

4747
def run_script(self, script: str | dict[str, str], args: str) -> int:
48+
# Calling `sys.argv` should run the same program as the currently
49+
# running program. To make calling Poetry scripts through RunCommand
50+
# match this behavior, we must set `sys.argv[0]` to be the full path of
51+
# the executable.
52+
full_path_args = args.copy()
53+
full_path_args[0] = self.env._bin(full_path_args[0])
54+
4855
if isinstance(script, dict):
4956
script = script["callable"]
5057

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

0 commit comments

Comments
 (0)