From e26affd27cd76b37f1fa868687a7b324fbf63ba2 Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Tue, 28 Jan 2025 13:44:32 -0600 Subject: [PATCH] Fix best-interpreter lookups when there is an invalid interpreter in the PATH (#11030) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes https://github.com/astral-sh/uv/issues/10978 The root cause is the same as #10908 — I should have been more careful with the original change. --- crates/uv-python/src/discovery.rs | 28 ++++++++++++++++++++-------- crates/uv/tests/it/pip_compile.rs | 25 ++++++++++++++++++------- 2 files changed, 38 insertions(+), 15 deletions(-) diff --git a/crates/uv-python/src/discovery.rs b/crates/uv-python/src/discovery.rs index 262e00311..bf60b6f3b 100644 --- a/crates/uv-python/src/discovery.rs +++ b/crates/uv-python/src/discovery.rs @@ -1074,10 +1074,16 @@ pub fn find_best_python_installation( // First, check for an exact match (or the first available version if no Python version was provided) debug!("Looking for exact match for request {request}"); - let result = find_python_installation(request, environments, preference, cache)?; - if let Ok(ref installation) = result { - warn_on_unsupported_python(installation.interpreter()); - return Ok(result); + let result = find_python_installation(request, environments, preference, cache); + match result { + Ok(Ok(installation)) => { + warn_on_unsupported_python(installation.interpreter()); + return Ok(Ok(installation)); + } + // Continue if we can't find a matching Python and ignore non-critical discovery errors + Ok(Err(_)) => {} + Err(ref err) if !err.is_critical() => {} + _ => return result, } // If that fails, and a specific patch version was requested try again allowing a @@ -1096,10 +1102,16 @@ pub fn find_best_python_installation( _ => None, } { debug!("Looking for relaxed patch version {request}"); - let result = find_python_installation(&request, environments, preference, cache)?; - if let Ok(ref installation) = result { - warn_on_unsupported_python(installation.interpreter()); - return Ok(result); + let result = find_python_installation(&request, environments, preference, cache); + match result { + Ok(Ok(installation)) => { + warn_on_unsupported_python(installation.interpreter()); + return Ok(Ok(installation)); + } + // Continue if we can't find a matching Python and ignore non-critical discovery errors + Ok(Err(_)) => {} + Err(ref err) if !err.is_critical() => {} + _ => return result, } } diff --git a/crates/uv/tests/it/pip_compile.rs b/crates/uv/tests/it/pip_compile.rs index 617f66966..7535e7298 100644 --- a/crates/uv/tests/it/pip_compile.rs +++ b/crates/uv/tests/it/pip_compile.rs @@ -1412,16 +1412,27 @@ fn compile_fallback_interpreter_broken_in_path() -> Result<()> { .arg("3.12") // In tests, we ignore `PATH` during Python discovery so we need to add the context `bin` .env("UV_TEST_PYTHON_PATH", context.bin_dir.as_os_str()), @r###" - success: false - exit_code: 2 + success: true + exit_code: 0 ----- stdout ----- + # This file was autogenerated by uv via the following command: + # uv pip compile --cache-dir [CACHE_DIR] requirements.in --python-version 3.12 + black==23.10.[X] + # via -r requirements.in + click==8.1.7 + # via black + mypy-extensions==1.0.0 + # via black + packaging==24.0 + # via black + pathspec==0.12.1 + # via black + platformdirs==4.2.0 + # via black ----- stderr ----- - error: Failed to inspect Python interpreter from search path at `[BIN]/python3` - Caused by: Querying Python at `[BIN]/python3` failed with exit status exit status: 1 - - [stderr] - error: intentionally broken python executable + warning: The requested Python version 3.12 is not available; 3.10.[X] will be used to build dependencies instead. + Resolved 6 packages in [TIME] "### );