Skip to content

Commit

Permalink
poetry run: deprecate uninstalled entry points
Browse files Browse the repository at this point in the history
  • Loading branch information
wagnerluis1982 committed Mar 5, 2023
1 parent f27308f commit 6a99329
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
27 changes: 27 additions & 0 deletions src/poetry/console/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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("")
11 changes: 7 additions & 4 deletions tests/console/commands/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 5 additions & 6 deletions tests/fixtures/scripts/scripts/check_argv0.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 6a99329

Please sign in to comment.