fix(env/virtual): clean up project_name dir on env removal#2249
Draft
Bojun-Vvibe wants to merge 1 commit into
Draft
fix(env/virtual): clean up project_name dir on env removal#2249Bojun-Vvibe wants to merge 1 commit into
Bojun-Vvibe wants to merge 1 commit into
Conversation
When removing a virtual environment, the project_id storage directory was being cleaned up but its parent project_name directory was left behind, leaving an orphan dir under the virtual env data area after `hatch env prune`. Also remove the parent dir when it becomes empty.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #737
Repo
pypa/hatch
Issue
#737
Root cause
VirtualEnvironment.remove()insrc/hatch/env/virtual.pycleans up the per-projectstorage_path(<data_dir>/env/virtual/<project_name>/<project_id>) when it becomes empty, but never removes the enclosing<project_name>directory. Afterhatch env pruneremoves every environment, that empty<project_name>dir is left orphaned underLOCALAPPDATA\hatch\env\virtual\(or the equivalent data dir).Fix
After successfully removing
storage_path, also remove its parent<project_name>directory if it is now empty and is not itself the data root or~/.virtualenvs.Regression test
tests/cli/env/test_prune.py::test_all— added an assertion thatproject_data_path(the<project_name>dir, e.g.data/env/virtual/my-app) no longer exists afterhatch env prune.Risk
low
Verification
Ran
pytest tests/cli/env/test_prune.py tests/cli/env/test_remove.py tests/cli/env/test_create.pyin a fresh venv with the package installed editable: 52 passed, 1 unrelated failure (test_new_selected_pythonfails due to missing managed Python interpreter in the sandbox, not touched by this change). Confirmed the new assertion fails on the original code and passes after the fix.