Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pipx run entry point discovery with local path #1422

Merged
merged 3 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/1422.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix discovery of a `pipx run` entry point if a local path was given as package.
10 changes: 8 additions & 2 deletions src/pipx/venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,13 @@ def _find_entry_point(self, app: str) -> Optional[EntryPoint]:
dists = Distribution.discover(name=self.main_package_name, path=[str(get_site_packages(self.python_path))])
for dist in dists:
for ep in dist.entry_points:
if ep.group == "pipx.run" and ep.name == app:
return ep
if ep.group == "pipx.run":
if ep.name == app:
return ep
# Try to infer app name from dist's metadata if given
# local path
if Path(app).exists() and dist.metadata["Name"] == ep.name:
return ep
return None

def run_app(self, app: str, filename: str, app_args: List[str]) -> NoReturn:
Expand All @@ -416,6 +421,7 @@ def run_app(self, app: str, filename: str, app_args: List[str]) -> NoReturn:
# "entry_point.module" and "entry_point.attr" instead.
match = _entry_point_value_pattern.match(entry_point.value)
assert match is not None, "invalid entry point"
logger.info("Using discovered entry point for 'pipx run'")
module, attr = match.group("module", "attr")
code = f"import sys, {module}\nsys.argv[0] = {entry_point.name!r}\nsys.exit({module}.{attr}())\n"
exec_app([str(self.python_path), "-c", code] + app_args)
Expand Down
4 changes: 3 additions & 1 deletion testdata/empty_project/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[build-system]
build-backend = "setuptools.build_meta"

requires = [
"setuptools",
"wheel",
]

[project]
Expand All @@ -21,3 +22,4 @@ classifiers = [
"Programming Language :: Python :: 3.12",
]
scripts.empty-project = "empty_project.main:cli"
entry-points."pipx.run".empty-project = "empty_project.main:cli"
13 changes: 13 additions & 0 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import subprocess
import sys
import textwrap
from pathlib import Path
from unittest import mock

import pytest # type: ignore[import-not-found]
Expand Down Expand Up @@ -401,3 +402,15 @@ def test_run_shared_lib_as_app(pipx_temp_env, monkeypatch, capfd):
run_pipx_cli_exit(["run", "pip", "--help"])
captured = capfd.readouterr()
assert "pip <command> [options]" in captured.out


@mock.patch("os.execvpe", new=execvpe_mock)
def test_run_local_path_entry_point(pipx_temp_env, caplog, root):
empty_project_path = (Path("testdata") / "empty_project").as_posix()
os.chdir(root)

caplog.set_level(logging.INFO)

run_pipx_cli_exit(["run", empty_project_path])

assert "Using discovered entry point for 'pipx run'" in caplog.text