diff --git a/src/poetry/console/commands/run.py b/src/poetry/console/commands/run.py index 63af286d3b1..d7d08084fda 100644 --- a/src/poetry/console/commands/run.py +++ b/src/poetry/console/commands/run.py @@ -63,6 +63,9 @@ def run_script(self, script: str | dict[str, str], args: list[str]) -> int: if script_path.exists(): args = [str(script_path), *args[1:]] break + else: + # If reach this point, the script is not installed + self._warning_not_installed_script(args[0]) if isinstance(script, dict): script = script["callable"] @@ -81,3 +84,27 @@ def run_script(self, script: str | dict[str, str], args: list[str]) -> int: ] return self.env.execute(*cmd) + + def _warning_not_installed_script(self, script: str) -> None: + self.line_error( + ( + f"Warning: '{script}' is an entry point defined in pyproject.toml," + " but it's not installed as a script." + " You may get improper `sys.argv[0]`." + ), + style="warning", + ) + self.line_error("") + self.line_error( + ( + "The support to run uninstalled scripts " + "will be removed in a future release." + ), + style="warning", + ) + self.line_error("") + self.line_error( + "Run `poetry install` to resolve and get rid of this message.", + style="warning", + ) + self.line_error("") diff --git a/tests/console/commands/test_run.py b/tests/console/commands/test_run.py index 6b5bd95a728..9c0e3f0ccab 100644 --- a/tests/console/commands/test_run.py +++ b/tests/console/commands/test_run.py @@ -171,12 +171,15 @@ def test_run_script_sys_argv0( environment=tmp_venv, ) assert install_tester.execute() == 0 - if not installed_script: - for path in tmp_venv.script_dirs[0].glob("check-argv0*"): + + script = "check-argv0" + if installed_script: + script = tmp_venv.script_dirs[0] / script + else: + for path in tmp_venv.script_dirs[0].glob(script + "*"): path.unlink() tester = command_tester_factory( "run", poetry=poetry_with_scripts, environment=tmp_venv ) - argv1 = "absolute" if installed_script else "relative" - assert tester.execute(f"check-argv0 {argv1}") == 0 + assert tester.execute(f"check-argv0 {script}") == 0 diff --git a/tests/fixtures/scripts/scripts/check_argv0.py b/tests/fixtures/scripts/scripts/check_argv0.py index e1dbc79d343..f3dac9245b7 100644 --- a/tests/fixtures/scripts/scripts/check_argv0.py +++ b/tests/fixtures/scripts/scripts/check_argv0.py @@ -7,14 +7,13 @@ def main() -> int: path = Path(sys.argv[0]) - if sys.argv[1] == "absolute": - if not path.is_absolute(): - raise RuntimeError(f"sys.argv[0] is not an absolute path: {path}") - if not path.exists(): + if sys.argv[0] == sys.argv[1]: + if path.is_absolute() and not path.exists(): raise RuntimeError(f"sys.argv[0] does not exist: {path}") else: - if path.is_absolute(): - raise RuntimeError(f"sys.argv[0] is an absolute path: {path}") + raise RuntimeError( + f"unexpected sys.argv[0]: '{sys.argv[0]}', should be '{sys.argv[1]}'" + ) return 0