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 reinstall crash with absolute path #1329

Merged
merged 6 commits into from
Apr 16, 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/1324.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Don't allow paths to be passed into `pipx reinstall`, as this might wreak havoc.
17 changes: 16 additions & 1 deletion src/pipx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,29 @@ def run_pipx_command(args: argparse.Namespace, subparsers: Dict[str, argparse.Ar

if "package" in args:
package = args.package
if urllib.parse.urlparse(package).scheme:
url_parse_package = urllib.parse.urlparse(package)
if url_parse_package.scheme and url_parse_package.netloc:
raise PipxError("Package cannot be a url")

if "spec" in args and args.spec is not None:
if urllib.parse.urlparse(args.spec).scheme:
if "#egg=" not in args.spec:
args.spec = args.spec + f"#egg={package}"

if args.command == "reinstall":
# Passing paths into `reinstall` might have unintended
# side effects.
if Path(package).is_absolute() or Path(package).exists():
raise PipxError(
pipx_wrap(
f"""
Error: Path '{package}' given as
package. Expected the name of
an installed package.
"""
)
)

venv_dir = venv_container.get_venv_dir(package)
logger.info(f"Virtual Environment location is {venv_dir}")

Expand Down
14 changes: 14 additions & 0 deletions tests/test_reinstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,17 @@ def test_reinstall_specifier(pipx_temp_env, capsys):
assert not run_pipx_cli(["reinstall", "--python", sys.executable, "pylint"])
captured = capsys.readouterr()
assert "installed package pylint 2.3.1" in captured.out


def test_reinstall_with_path(pipx_temp_env, capsys, tmp_path):
path = tmp_path / "some" / "path"

assert run_pipx_cli(["reinstall", str(path)])
captured = capsys.readouterr()

assert "Expected the name of an installed package" in captured.err.replace("\n", " ")

assert run_pipx_cli(["reinstall", str(path.resolve())])
captured = capsys.readouterr()

assert "Expected the name of an installed package" in captured.err.replace("\n", " ")