Skip to content

fix(venv): a ~ in venv.location hides every created virtualenv - #3847

Merged
frostming merged 2 commits into
pdm-project:mainfrom
VXNCXNX:fix/venv-location-tilde
Aug 17, 2026
Merged

fix(venv): a ~ in venv.location hides every created virtualenv#3847
frostming merged 2 commits into
pdm-project:mainfrom
VXNCXNX:fix/venv-location-tilde

Conversation

@VXNCXNX

@VXNCXNX VXNCXNX commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

A ~ in venv.location is expanded when a virtualenv is created but not when
one is looked up, so every pdm venv subcommand reports nothing after a
successful create.

With venv.location = ~/my-venvs:

before:
$ pdm venv create
Virtualenv /home/me/my-venvs/demo-DmFqMEeT-3.12 is created successfully
$ pdm venv list
Virtualenvs created with this project:

$ pdm venv purge --force
No virtualenvs to purge, quitting.
$ pdm venv activate 3.12
[PdmUsageError]: No virtualenv with key '3.12' is found, must be one of [].

after:
$ pdm venv list
-  3.12: /home/me/my-venvs/demo-dSE-z8Fz-3.12
$ pdm venv activate 3.12
source /home/me/my-venvs/demo-dSE-z8Fz-3.12/bin/activate

list, remove, activate, purge and pdm venv --path are all affected. The
venv exists and works, it is simply invisible to pdm.

Cause

Backend.get_location expands the path:

venv_parent = Path(self.project.config["venv.location"]).expanduser()

but the two iterators do not:

venv_parent = Path(project.config["venv.location"])

So the venv is created under /home/me/my-venvs and then looked for under a
directory literally named ~, which is also why a stray ~/ directory appears
in the working directory.

The fix

One get_venv_parent(project) helper doing the expansion, used by
get_location, iter_venvs and iter_central_venvs, so the three cannot drift
apart again.

Verification

test_venv_list_with_tilde_location sets a ~ location against a fake home,
creates a venv and checks pdm venv list finds it.

Removing the expansion from the helper fails it, and the failure names the
symptom exactly:

FAILED tests/cli/test_venv.py::test_venv_list_with_tilde_location - AssertionError: assert False
 +  where False = is_relative_to(PosixPath('.../fake-home') / 'my-venvs')
 +    where PosixPath('~/my-venvs/test_venv_list_with_tilde_loca0-xVyO888H-3.12').is_relative_to

tests/cli/test_venv.py is 36 passed. On the wider suite the baseline is 1346
passed on a stashed tree and 1347 with this change, no failures either way.

Two tests are excluded from that count because they fail identically on an
unmodified tree here: test_create_venv_in_project[venv-True] (this sandbox's
python -m venv --prompt exits non-zero) and the OIDC tests in
tests/cli/test_publish.py, which need pytest-httpx.

ruff check and ruff format --check are clean.

Pull Request Checklist

  • A news fragment is added in news/ describing what is new.
  • Test cases added for changed code.

Disclosure: written with AI assistance (Claude Code). I produced the before and after by running the real pdm venv commands against a ~ location, and ran the mutation check and the suite baseline myself.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@codecov

codecov Bot commented Aug 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.37%. Comparing base (5a6ea88) to head (627774a).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3847   +/-   ##
=======================================
  Coverage   88.36%   88.37%           
=======================================
  Files         121      121           
  Lines       13249    13251    +2     
  Branches     2251     2251           
=======================================
+ Hits        11708    11710    +2     
  Misses        969      969           
  Partials      572      572           
Flag Coverage Δ
unittests 88.25% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@frostming
frostming merged commit ef747c9 into pdm-project:main Aug 17, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants