Allow syncing to empty virtual environment directories (#9427)
As discussed in https://github.com/astral-sh/uv/issues/9423, it's confusing that we do not allow `uv sync` just because the `.venv` directory _exists_. This change matches `uv venv`.
This commit is contained in:
@@ -43,6 +43,7 @@ pub struct InvalidEnvironment {
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum InvalidEnvironmentKind {
|
||||
NotDirectory,
|
||||
Empty,
|
||||
MissingExecutable(PathBuf),
|
||||
}
|
||||
|
||||
@@ -128,6 +129,7 @@ impl std::fmt::Display for InvalidEnvironmentKind {
|
||||
Self::MissingExecutable(path) => {
|
||||
write!(f, "missing Python executable at `{}`", path.user_display())
|
||||
}
|
||||
Self::Empty => write!(f, "directory is empty"),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -178,6 +180,14 @@ impl PythonEnvironment {
|
||||
.into());
|
||||
}
|
||||
|
||||
if venv.read_dir().is_ok_and(|mut dir| dir.next().is_none()) {
|
||||
return Err(InvalidEnvironment {
|
||||
path: venv,
|
||||
kind: InvalidEnvironmentKind::Empty,
|
||||
}
|
||||
.into());
|
||||
}
|
||||
|
||||
let executable = virtualenv_python_executable(&venv);
|
||||
|
||||
// Check if the executable exists before querying so we can provide a more specific error
|
||||
|
||||
Reference in New Issue
Block a user