Skip to content

fix(env/virtual): clean up project_name dir on env removal#2249

Draft
Bojun-Vvibe wants to merge 1 commit into
pypa:masterfrom
Bojun-Vvibe:fix/pypa-hatch-737
Draft

fix(env/virtual): clean up project_name dir on env removal#2249
Bojun-Vvibe wants to merge 1 commit into
pypa:masterfrom
Bojun-Vvibe:fix/pypa-hatch-737

Conversation

@Bojun-Vvibe

Copy link
Copy Markdown

Closes #737

Repo

pypa/hatch

Issue

#737

Root cause

VirtualEnvironment.remove() in src/hatch/env/virtual.py cleans up the per-project storage_path (<data_dir>/env/virtual/<project_name>/<project_id>) when it becomes empty, but never removes the enclosing <project_name> directory. After hatch env prune removes every environment, that empty <project_name> dir is left orphaned under LOCALAPPDATA\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 that project_data_path (the <project_name> dir, e.g. data/env/virtual/my-app) no longer exists after hatch env prune.

Risk

low

Verification

Ran pytest tests/cli/env/test_prune.py tests/cli/env/test_remove.py tests/cli/env/test_create.py in a fresh venv with the package installed editable: 52 passed, 1 unrelated failure (test_new_selected_python fails 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.

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.
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.

hatch env prune leaves a directory in LOCALAPPDATA

1 participant