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

Virtual Environment location is invalid when passing an absolute path #1354

Closed
huxuan opened this issue Apr 19, 2024 · 3 comments · Fixed by #1363
Closed

Virtual Environment location is invalid when passing an absolute path #1354

huxuan opened this issue Apr 19, 2024 · 3 comments · Fixed by #1363
Labels
bug Something isn't working

Comments

@huxuan
Copy link
Member

huxuan commented Apr 19, 2024

Describe the bug

When passing an absolute path as package argument, VenvContainer.get_venv_dir() returns the absolute path directly and brings bunch of abnormal behaviors. Esepcially when the path is normalized which gets identical result after packaging.util.canonicalize_name(), reinstall and uninstall will remove the absolute path unexpectedly (as reported in #1324).

  1. All related commands has error in logs: pipx >(run_pipx_command:197): Virtual Environment location is /tmp/empty-project
  2. pipx uninstall and pipx reinstall will remove the directory /tmp/empty-project

How to reproduce

$ cp -r /path/to/pipx/testdata/empty_project tmp/empty-project

$ pipx runpip --verbose /tmp/empty-project list
pipx >(setup:924): pipx version is 1.5.0
pipx >(setup:925): Default python interpreter is '/usr/local/pipx/venvs/pipx/bin/python'
pipx >(run_pipx_command:197): Virtual Environment location is /tmp/empty-project
venv for '/tmp/empty-project' was not found. Was '/tmp/empty-project' installed with pipx?

$ pipx upgrade --verbose /tmp/workspace
pipx >(setup:924): pipx version is 1.5.0
pipx >(setup:925): Default python interpreter is '/usr/local/pipx/venvs/pipx/bin/python'
pipx >(run_pipx_command:197): Virtual Environment location is /tmp/empty-project
pipx >(_upgrade_venv:136): Ignoring --python as not combined with --install
Not upgrading empty-project. It has missing internal pipx metadata.
It was likely installed using a pipx version before 0.15.0.0.
Please uninstall and install this package to fix.

$ pipx inject --verbose /tmp/empty-project pywcowsay
pipx >(setup:924): pipx version is 1.5.0
pipx >(setup:925): Default python interpreter is '/usr/local/pipx/venvs/pipx/bin/python'
pipx >(run_pipx_command:197): Virtual Environment location is /tmp/empty-project
Can't inject 'pywcowsay' into Virtual Environment 'empty-project'. 'empty-project' has missing internal pipx metadata. It was likely installed using a pipx version before 0.15.0.0.
Please uninstall and install 'empty-project', or reinstall-all to fix.

$ pipx uninject --verbose /tmp/empty-project pywcowsay
pipx >(setup:924): pipx version is 1.5.0
pipx >(setup:925): Default python interpreter is '/usr/local/pipx/venvs/pipx/bin/python'
pipx >(run_pipx_command:197): Virtual Environment location is /tmp/empty-project
Can't uninject from Virtual Environment 'empty-project'. 'empty-project' has missing internal pipx metadata. It was likely installed using a pipx version before 0.15.0.0. Please
uninstall and install 'empty-project' manually to fix.

$ pipx uninstall --verbose /tmp/empty-project
pipx >(setup:924): pipx version is 1.5.0
pipx >(setup:925): Default python interpreter is '/usr/local/pipx/venvs/pipx/bin/python'
pipx >(run_pipx_command:197): Virtual Environment location is /tmp/empty-project
pipx >(rmdir:55): removing directory /tmp/empty-project
uninstalled empty-project! ✨ 🌟 ✨

$ pipx reinstall --verbose /tmp/empty-project
pipx >(setup:924): pipx version is 1.5.0
pipx >(setup:925): Default python interpreter is '/usr/local/pipx/venvs/pipx/bin/python'
pipx >(run_pipx_command:197): Virtual Environment location is /tmp/empty-project
pipx >(rmdir:55): removing directory /tmp/empty-project
uninstalled empty-project! ✨ 🌟 ✨
creating virtual environment...
pipx >(run_subprocess:174): running /usr/local/pipx/venvs/pipx/bin/python -m venv --without-pip /tmp/empty-project
pipx >(run_subprocess:174): running <checking pip's availability>
pipx >(run_subprocess:174): running /tmp/empty-project/bin/python -c import sysconfig; print(sysconfig.get_path('purelib'))
pipx >(run_subprocess:174): running /usr/local/pipx/shared/bin/python -c import sysconfig; print(sysconfig.get_path('purelib'))
pipx >(run_subprocess:174): running /tmp/empty-project/bin/python --version
pipx >(_parsed_package_to_package_or_url:137): cleaned package spec: empty-project
installing empty-project...
pipx >(run_subprocess:174): running /tmp/empty-project/bin/python -m pip --no-input install empty-project
pipx >(subprocess_post_check_handle_pip_error:331): '/tmp/empty-project/bin/python -m pip --no-input install empty-project' failed
pipx >(subprocess_post_check_handle_pip_error:346): Fatal error from pip prevented installation. Full pip output in file:
    /usr/local/pipx/logs/cmd_2024-04-19_11.16.52_pip_errors.log

Some possibly relevant errors from pip install:
    ERROR: Could not find a version that satisfies the requirement empty-project (from versions: none)
    ERROR: No matching distribution found for empty-project

pipx >(rmdir:55): removing directory /tmp/empty-project
Error installing empty-project.

The behavior can also be reproduced on Windows

PS C:\> Copy-Item -Path "C:\path\to\pipx\testdata\empty_project\" -Destination "C:\empty-project" -Recurse

PS C:\> pipx.local uninstall --verbose C:\empty-project\
pipx >(setup:990): pipx version is 1.5.1.dev14+g4402362
pipx >(setup:991): Default python interpreter is 'C:\Users\huxuan\AppData\Local\pipx\pipx\venvs\pipx-local\Scripts\python.exe'
pipx >(run_pipx_command:212): Virtual Environment location is c:\empty-project
pipx >(rmdir:55): removing directory c:\empty-project
uninstalled empty-project! ✨ 🌟 ✨

Expected behavior

We should not use the invalid virtual environment in logs and the following steps.

Proposed Solution

Though #1324 is fixed by #1329, it does not seem to cover all the cases. There are two options I can think of:

  1. AFAIK, seems there is no scenario that package need to be an absolute path, so we can just raise a PipxError if the package is an absolute path.
  2. Check whether the venv_dir is relative to paths.ctx.venvs inside VenvContainer.get_venv_dir().

Let me know if you have any comments or preference.

@huxuan huxuan changed the title Virtual Environment location is wrong when passing an absolute path Virtual Environment location is invalid when passing an absolute path Apr 19, 2024
@chrysle
Copy link
Contributor

chrysle commented Apr 19, 2024

I think we should extend the check I already installed in main.py to the other pipx commands (except for install).

@chrysle chrysle added the bug Something isn't working label Apr 19, 2024
@huxuan
Copy link
Member Author

huxuan commented Apr 19, 2024

I think we should extend the check I already installed in main.py to the other pipx commands (except for install).

It seems that the args in install command is package_spec now [1], so maybe we can just apply the check to all related commands?

[1]

p.add_argument("package_spec", help="package name(s) or pip installation spec(s)", nargs="+")

@chrysle
Copy link
Contributor

chrysle commented Apr 19, 2024

It seems that the args in install command is package_spec now [1], so maybe we can just apply the check to all related commands?

Ah, I wasn't aware of that. Yes, sounds good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants