Skip to content

Commit

Permalink
Show interpreter source during Python discovery query errors (#7928)
Browse files Browse the repository at this point in the history
Closes #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)
```
  • Loading branch information
zanieb authored Oct 7, 2024
1 parent 719604a commit e479e8b
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions crates/uv-python/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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),
})
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down

0 comments on commit e479e8b

Please sign in to comment.