Improve handling of invalid virtual environments during interpreter discovery (#8086)

## Summary

Fix #8075.

Invalid discovered environments in the working directory should be
filtered out.

## Test Plan

- Test python_find

---------

Co-authored-by: Zanie Blue <contact@zanie.dev>
This commit is contained in:
potoo 2024-12-11 02:56:52 +08:00 committed by GitHub
parent 624e79a8a9
commit 459269fc95
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 75 additions and 3 deletions

View file

@ -25,6 +25,7 @@ use crate::managed::ManagedPythonInstallations;
use crate::microsoft_store::find_microsoft_store_pythons;
#[cfg(windows)]
use crate::py_launcher::{registry_pythons, WindowsPython};
use crate::virtualenv::Error as VirtualEnvError;
use crate::virtualenv::{
conda_environment_from_env, virtualenv_from_env, virtualenv_from_working_dir,
virtualenv_python_executable, CondaEnvironmentKind,
@ -729,10 +730,27 @@ impl Error {
false
}
InterpreterError::NotFound(path) => {
trace!("Skipping missing interpreter at {}", path.display());
false
// If the interpreter is from an active, valid virtual environment, we should
// fail because it's broken
if let Some(Ok(true)) = matches!(source, PythonSource::ActiveEnvironment)
.then(|| {
path.parent()
.and_then(Path::parent)
.map(|path| path.join("pyvenv.cfg").try_exists())
})
.flatten()
{
true
} else {
trace!("Skipping missing interpreter at {}", path.display());
false
}
}
},
Error::VirtualEnv(VirtualEnvError::MissingPyVenvCfg(path)) => {
trace!("Skipping broken virtualenv at {}", path.display());
false
}
_ => true,
}
}