Show interpreter source during Python discovery query errors (#7928)

Closes https://github.com/astral-sh/uv/issues/4154

e.g.

```
❯ UV_PYTHON=/dev/null cargo run -q -- pip install anyio
error: Failed to inspect Python interpreter from provided path at `/dev/null` 
  Caused by: Failed to query Python interpreter at `/dev/null`
  Caused by: Permission denied (os error 13)
  
❯ VIRTUAL_ENV=/dev/null cargo run -q -- pip install anyio
error: Failed to inspect Python interpreter from active virtual environment at `/dev/null/bin/python3` 
  Caused by: Failed to query Python interpreter
  Caused by: failed to canonicalize path `/dev/null/bin/python3`
  Caused by: Not a directory (os error 20)
```
This commit is contained in:
Zanie Blue 2024-10-07 12:57:18 -05:00 committed by GitHub
parent 719604ae1d
commit e479e8b4b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -198,8 +198,12 @@ pub enum Error {
Io(#[from] io::Error),
/// An error was encountering when retrieving interpreter information.
#[error(transparent)]
Query(#[from] crate::interpreter::Error),
#[error("Failed to inspect Python interpreter from {2} at `{}` ", .1.user_display())]
Query(
#[source] Box<crate::interpreter::Error>,
PathBuf,
PythonSource,
),
/// An error was encountered when interacting with a managed Python installation.
#[error(transparent)]
@ -600,7 +604,7 @@ fn python_interpreters_from_executables<'a>(
path.display()
);
})
.map_err(Error::from)
.map_err(|err| Error::Query(Box::new(err), path, source))
.inspect_err(|err| debug!("{err}")),
Err(err) => Err(err),
})
@ -674,14 +678,17 @@ impl Error {
match self {
// When querying the Python interpreter fails, we will only raise errors that demonstrate that something is broken
// If the Python interpreter returned a bad response, we'll continue searching for one that works
Error::Query(err) => match err {
Error::Query(err, _, source) => match &**err {
InterpreterError::Encode(_)
| InterpreterError::Io(_)
| InterpreterError::SpawnFailed { .. } => true,
InterpreterError::QueryScript { path, .. }
| InterpreterError::UnexpectedResponse { path, .. }
| InterpreterError::StatusCode { path, .. } => {
debug!("Skipping bad interpreter at {}: {err}", path.display());
debug!(
"Skipping bad interpreter at {} from {source}: {err}",
path.display()
);
false
}
InterpreterError::NotFound(path) => {
@ -747,7 +754,11 @@ pub fn find_python_installations<'a>(
environment_preference: environments,
}))
}
Err(err) => Err(err.into()),
Err(err) => Err(Error::Query(
Box::new(err),
path.clone(),
PythonSource::ProvidedPath,
)),
}
} else {
Err(Error::SourceNotAllowed(
@ -770,7 +781,11 @@ pub fn find_python_installations<'a>(
environment_preference: environments,
}))
}
Err(err) => Err(err.into()),
Err(err) => Err(Error::Query(
Box::new(err),
path.clone(),
PythonSource::ProvidedPath,
)),
}
} else {
Err(Error::SourceNotAllowed(