From 5434cc0d000ac85b22716438e51598dbe0d24d95 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Fri, 17 Jan 2025 11:24:56 -0600 Subject: [PATCH] Show non-critical Python discovery errors if no other interpreter is found We show the first one, sort of arbitrarily but I think it matches user expectation --- crates/uv-python/src/discovery.rs | 11 +++++++++++ crates/uv-python/src/lib.rs | 9 ++++----- crates/uv/tests/it/python_find.rs | 12 ++++++++---- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/crates/uv-python/src/discovery.rs b/crates/uv-python/src/discovery.rs index d35e3b0df5514..2b864aca42d17 100644 --- a/crates/uv-python/src/discovery.rs +++ b/crates/uv-python/src/discovery.rs @@ -971,9 +971,14 @@ pub(crate) fn find_python_installation( ) -> Result { let installations = find_python_installations(request, environments, preference, cache); let mut first_prerelease = None; + let mut first_error = None; for result in installations { // Iterate until the first critical error or happy result if !result.as_ref().err().map_or(true, Error::is_critical) { + // Track the first non-critical error + if first_error.is_none() { + first_error = Some(result); + } continue; } @@ -1026,6 +1031,12 @@ pub(crate) fn find_python_installation( return Ok(Ok(installation)); } + // If we found a Python, but it was unusable for some reason, report that instead of saying we + // couldn't find any Python interpreters. + if let Some(result) = first_error { + return result; + } + Ok(Err(PythonNotFound { request: request.clone(), environment_preference: environments, diff --git a/crates/uv-python/src/lib.rs b/crates/uv-python/src/lib.rs index 0eb3cb36f7f62..114415043371d 100644 --- a/crates/uv-python/src/lib.rs +++ b/crates/uv-python/src/lib.rs @@ -111,7 +111,7 @@ mod tests { use crate::{ discovery::{ - find_best_python_installation, find_python_installation, EnvironmentPreference, + self, find_best_python_installation, find_python_installation, EnvironmentPreference, }, PythonPreference, }; @@ -589,11 +589,10 @@ mod tests { PythonPreference::default(), &context.cache, ) - })?; + }); assert!( - matches!(result, Err(PythonNotFound { .. })), - // TODO(zanieb): We could improve the error handling to hint this to the user - "If only Python 2 is available, we should not find a python; got {result:?}" + matches!(result, Err(discovery::Error::Query(..))), + "If only Python 2 is available, we should report the interpreter query error; got {result:?}" ); Ok(()) diff --git a/crates/uv/tests/it/python_find.rs b/crates/uv/tests/it/python_find.rs index 4eeb73be704e1..9d6fbc3f4cd3c 100644 --- a/crates/uv/tests/it/python_find.rs +++ b/crates/uv/tests/it/python_find.rs @@ -28,7 +28,8 @@ fn python_find() { ----- stdout ----- ----- stderr ----- - error: No interpreter found in virtual environments, managed installations, or search path + error: Failed to inspect Python interpreter from active virtual environment at `.venv/bin/python3` + Caused by: Python interpreter not found at `[VENV]/bin/python3` "###); } @@ -124,7 +125,8 @@ fn python_find() { ----- stdout ----- ----- stderr ----- - error: No interpreter found for PyPy in virtual environments, managed installations, or search path + error: Failed to inspect Python interpreter from active virtual environment at `.venv/bin/python3` + Caused by: Python interpreter not found at `[VENV]/bin/python3` "###); } @@ -536,7 +538,8 @@ fn python_find_unsupported_version() { ----- stdout ----- ----- stderr ----- - error: No interpreter found for Python 4.2 in virtual environments, managed installations, or search path + error: Failed to inspect Python interpreter from active virtual environment at `.venv/bin/python3` + Caused by: Python interpreter not found at `[VENV]/bin/python3` "###); // Request a low version with a range @@ -546,7 +549,8 @@ fn python_find_unsupported_version() { ----- stdout ----- ----- stderr ----- - error: No interpreter found for Python <3.0 in virtual environments, managed installations, or search path + error: Failed to inspect Python interpreter from active virtual environment at `.venv/bin/python3` + Caused by: Python interpreter not found at `[VENV]/bin/python3` "###); // Request free-threaded Python on unsupported version