From 7ed9818d3e766d5b483189a44420278f25d4e0a8 Mon Sep 17 00:00:00 2001 From: Christopher Dignam Date: Sun, 31 Mar 2019 17:54:35 -0400 Subject: [PATCH] fix: RunCommand calling scripts with incorrect executable path Fixes GH-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. --- src/poetry/console/commands/run.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/poetry/console/commands/run.py b/src/poetry/console/commands/run.py index 6496b45af24..6df6ab1b7de 100644 --- a/src/poetry/console/commands/run.py +++ b/src/poetry/console/commands/run.py @@ -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"] @@ -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_}()" ]