Avoid warning about bad Python interpreter links for empty project environment directories (#7527)

Someone reported this a while back (will try to find the issue), and I
ran into it working on #7522
This commit is contained in:
Zanie Blue 2024-09-19 06:49:35 -05:00 committed by GitHub
parent 4fdf5fc73f
commit 6b08aaecad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 48 additions and 21 deletions

View file

@ -34,6 +34,12 @@ pub struct EnvironmentNotFound {
preference: EnvironmentPreference,
}
#[derive(Clone, Debug, Error)]
pub struct InvalidEnvironment {
path: PathBuf,
reason: String,
}
impl From<PythonNotFound> for EnvironmentNotFound {
fn from(value: PythonNotFound) -> Self {
Self {
@ -98,6 +104,17 @@ impl fmt::Display for EnvironmentNotFound {
}
}
impl std::fmt::Display for InvalidEnvironment {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"Invalid environment at `{}`: {}",
self.path.user_display(),
self.reason
)
}
}
impl PythonEnvironment {
/// Find a [`PythonEnvironment`] matching the given request and preference.
///
@ -133,6 +150,23 @@ impl PythonEnvironment {
}
Err(err) => return Err(Error::Discovery(err.into())),
};
if venv.is_file() {
return Err(InvalidEnvironment {
path: venv,
reason: "expected directory but found a file".to_string(),
}
.into());
}
if !venv.join("pyvenv.cfg").is_file() {
return Err(InvalidEnvironment {
path: venv,
reason: "missing a `pyvenv.cfg` marker".to_string(),
}
.into());
}
let executable = virtualenv_python_executable(venv);
let interpreter = Interpreter::query(executable, cache)?;

View file

@ -78,6 +78,9 @@ pub enum Error {
#[error(transparent)]
MissingEnvironment(#[from] environment::EnvironmentNotFound),
#[error(transparent)]
InvalidEnvironment(#[from] environment::InvalidEnvironment),
}
// The mock interpreters are not valid on Windows so we don't have unit test coverage there