diff --git a/src/hatch/env/virtual.py b/src/hatch/env/virtual.py index 4511db853..96c516ac7 100644 --- a/src/hatch/env/virtual.py +++ b/src/hatch/env/virtual.py @@ -189,6 +189,17 @@ def remove(self): if not entries or (entries == [".gitignore"] and self.root in self.storage_path.parents): self.storage_path.remove() + # Also clean up the parent project_name directory if it is now empty + # (e.g. `/env/virtual//` -> remove ``) + storage_parent = self.storage_path.parent + if ( + storage_parent != self.data_directory + and storage_parent != self.platform.home / ".virtualenvs" + and storage_parent.is_dir() + and not any(storage_parent.iterdir()) + ): + storage_parent.remove() + def exists(self): return self.virtual_env.exists() diff --git a/tests/cli/env/test_prune.py b/tests/cli/env/test_prune.py index 56206cd90..fae148b81 100644 --- a/tests/cli/env/test_prune.py +++ b/tests/cli/env/test_prune.py @@ -89,6 +89,8 @@ def test_all(hatch, helpers, temp_dir_data, config_file): ) assert not storage_path.is_dir() + # Issue #737: the project_name directory should also be cleaned up + assert not project_data_path.is_dir() def test_incompatible_ok(hatch, helpers, temp_dir_data, config_file):