Skip to content

Commit b35faad

Browse files
committed
Fix best-interpreter lookups when there is an invalid interpreter in the PATH
1 parent 50fa561 commit b35faad

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

crates/uv-python/src/discovery.rs

+20-8
Original file line numberDiff line numberDiff line change
@@ -1074,10 +1074,16 @@ pub fn find_best_python_installation(
10741074

10751075
// First, check for an exact match (or the first available version if no Python version was provided)
10761076
debug!("Looking for exact match for request {request}");
1077-
let result = find_python_installation(request, environments, preference, cache)?;
1078-
if let Ok(ref installation) = result {
1079-
warn_on_unsupported_python(installation.interpreter());
1080-
return Ok(result);
1077+
let result = find_python_installation(request, environments, preference, cache);
1078+
match result {
1079+
Ok(Ok(installation)) => {
1080+
warn_on_unsupported_python(installation.interpreter());
1081+
return Ok(Ok(installation));
1082+
}
1083+
// Continue if we can't find a matching Python and ignore non-critical discovery errors
1084+
Ok(Err(_)) => {}
1085+
Err(ref err) if !err.is_critical() => {}
1086+
_ => return result,
10811087
}
10821088

10831089
// If that fails, and a specific patch version was requested try again allowing a
@@ -1096,10 +1102,16 @@ pub fn find_best_python_installation(
10961102
_ => None,
10971103
} {
10981104
debug!("Looking for relaxed patch version {request}");
1099-
let result = find_python_installation(&request, environments, preference, cache)?;
1100-
if let Ok(ref installation) = result {
1101-
warn_on_unsupported_python(installation.interpreter());
1102-
return Ok(result);
1105+
let result = find_python_installation(&request, environments, preference, cache);
1106+
match result {
1107+
Ok(Ok(installation)) => {
1108+
warn_on_unsupported_python(installation.interpreter());
1109+
return Ok(Ok(installation));
1110+
}
1111+
// Continue if we can't find a matching Python and ignore non-critical discovery errors
1112+
Ok(Err(_)) => {}
1113+
Err(ref err) if !err.is_critical() => {}
1114+
_ => return result,
11031115
}
11041116
}
11051117

crates/uv/tests/it/pip_compile.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -1412,16 +1412,27 @@ fn compile_fallback_interpreter_broken_in_path() -> Result<()> {
14121412
.arg("3.12")
14131413
// In tests, we ignore `PATH` during Python discovery so we need to add the context `bin`
14141414
.env("UV_TEST_PYTHON_PATH", context.bin_dir.as_os_str()), @r###"
1415-
success: false
1416-
exit_code: 2
1415+
success: true
1416+
exit_code: 0
14171417
----- stdout -----
1418+
# This file was autogenerated by uv via the following command:
1419+
# uv pip compile --cache-dir [CACHE_DIR] requirements.in --python-version 3.12
1420+
black==23.10.[X]
1421+
# via -r requirements.in
1422+
click==8.1.7
1423+
# via black
1424+
mypy-extensions==1.0.0
1425+
# via black
1426+
packaging==24.0
1427+
# via black
1428+
pathspec==0.12.1
1429+
# via black
1430+
platformdirs==4.2.0
1431+
# via black
14181432
14191433
----- stderr -----
1420-
error: Failed to inspect Python interpreter from search path at `[BIN]/python3`
1421-
Caused by: Querying Python at `[BIN]/python3` failed with exit status exit status: 1
1422-
1423-
[stderr]
1424-
error: intentionally broken python executable
1434+
warning: The requested Python version 3.12 is not available; 3.10.[X] will be used to build dependencies instead.
1435+
Resolved 6 packages in [TIME]
14251436
"###
14261437
);
14271438

0 commit comments

Comments
 (0)