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:
Zanie Blue
2024-11-25 16:23:49 -06:00
committed by GitHub
parent 5759cb9891
commit 21aa9bc53a
3 changed files with 28 additions and 7 deletions
+10
View File
@@ -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