Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: empty venv instead of remove it #2282

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/2282.venv-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Change the venv backend clean function `pdm.cli.commands.venv.backend.Backend._ensure_clean` to empty the `.venv` folder instead of deleting it.
10 changes: 9 additions & 1 deletion src/pdm/cli/commands/venv/backends.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import abc
import os
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -71,14 +72,21 @@ def subprocess_call(self, cmd: list[str], **kwargs: Any) -> None:
def _ensure_clean(self, location: Path, force: bool = False) -> None:
if not location.exists():
return
if location.is_dir() and not any(location.iterdir()):
return
if not force:
raise VirtualenvCreateError(f"The location {location} is not empty, add --force to overwrite it.")
if location.is_file():
self.project.core.ui.echo(f"Removing existing file {location}", err=True)
location.unlink()
else:
self.project.core.ui.echo(f"Cleaning existing target directory {location}", err=True)
shutil.rmtree(location)
with os.scandir(location) as entries:
for entry in entries:
if entry.is_dir() and not entry.is_symlink():
shutil.rmtree(entry.path)
else:
os.remove(entry.path)

def get_location(self, name: str | None) -> Path:
venv_parent = Path(self.project.config["venv.location"])
Expand Down