mirror of
https://github.com/astral-sh/uv.git
synced 2025-07-16 01:35:00 +00:00
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:
parent
4fdf5fc73f
commit
6b08aaecad
4 changed files with 48 additions and 21 deletions
|
@ -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)?;
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue