Avoid removing empty directories when constructing virtual environments (#14822)

Closes https://github.com/astral-sh/uv/issues/14815

I tested this with the docker-compose reproduction. You can also see a
regression test change at
2ae4464b7e
This commit is contained in:
Zanie Blue 2025-07-22 13:50:14 -05:00 committed by GitHub
parent f0151f3a18
commit 076677da20
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 59 additions and 8 deletions

View file

@ -10,7 +10,7 @@ use fs_err as fs;
use fs_err::File;
use itertools::Itertools;
use owo_colors::OwoColorize;
use tracing::debug;
use tracing::{debug, trace};
use uv_configuration::PreviewMode;
use uv_fs::{CWD, Simplified, cachedir};
@ -85,6 +85,18 @@ pub(crate) fn create(
format!("File exists at `{}`", location.user_display()),
)));
}
Ok(metadata)
if metadata.is_dir()
&& location
.read_dir()
.is_ok_and(|mut dir| dir.next().is_none()) =>
{
// If it's an empty directory, we can proceed
trace!(
"Using empty directory at `{}` for virtual environment",
location.user_display()
);
}
Ok(metadata) if metadata.is_dir() => {
let name = if uv_fs::is_virtualenv_base(location) {
"virtual environment"
@ -100,13 +112,6 @@ pub(crate) fn create(
remove_virtualenv(location)?;
fs::create_dir_all(location)?;
}
OnExisting::Fail
if location
.read_dir()
.is_ok_and(|mut dir| dir.next().is_none()) =>
{
debug!("Ignoring empty directory");
}
OnExisting::Fail => {
match confirm_clear(location, name)? {
Some(true) => {