Skip to content

fix: remove empty project_name dir after env prune#2246

Open
terminalchai wants to merge 2 commits into
pypa:masterfrom
terminalchai:fix/prune-leaves-project-name-dir
Open

fix: remove empty project_name dir after env prune#2246
terminalchai wants to merge 2 commits into
pypa:masterfrom
terminalchai:fix/prune-leaves-project-name-dir

Conversation

@terminalchai
Copy link
Copy Markdown

Summary

Fixes #737

hatch env prune removes individual environment directories (data_dir/project_name/project_id/venv_name) and then cleans up the project_id directory if empty, but it never cleans up the parent project_name directory. This leaves a stale directory behind in %LOCALAPPDATA%\hatch\env\virtual\ (or equivalent on other platforms) even after all environments for a project have been removed.

Root Cause

In VirtualEnvironment.remove(), after removing storage_path (the project_id dir), the parent directory (project_name dir = storage_path.parent) was never checked or cleaned up.

Fix

After successfully removing storage_path, check whether its parent (project_name dir) is now empty and, if so, remove it too — guarded by a check that it is not the data_directory itself (to avoid accidentally removing the top-level env store).

Tests

  • Extended the existing est_all assertion to also verify the project_name directory is removed.
  • Added est_prune_removes_project_name_dir which specifically exercises the single-environment case reported in the issue.

terminalchai and others added 2 commits April 19, 2026 03:35
When using the standard storage layout (data_dir/project_name/project_id/venv),
hatch env prune would remove the project_id directory but leave the parent
project_name directory behind (e.g. %LOCALAPPDATA%\hatch\env\virtual\myproject).

After removing storage_path (project_id dir), also remove its parent
(project_name dir) if it is now empty and is not the data_directory itself.

Fixes pypa#737
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

2 participants