Skip to content

Commit 20f5843

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

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

crates/uv-python/src/discovery.rs

+28-8
Original file line numberDiff line numberDiff line change
@@ -1072,12 +1072,26 @@ pub fn find_best_python_installation(
10721072
) -> Result<FindPythonResult, Error> {
10731073
debug!("Starting Python discovery for {}", request);
10741074

1075+
// fn handle_result(result: Result<FindPythonResult, Error>) -> Option<FindPythonResult> {
1076+
// let discovery_err = match result {
1077+
// Ok(python_result) => {
1078+
1079+
// }
1080+
// }
1081+
// }
1082+
10751083
// First, check for an exact match (or the first available version if no Python version was provided)
10761084
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);
1085+
let result = find_python_installation(request, environments, preference, cache);
1086+
match result {
1087+
Ok(Ok(installation)) => {
1088+
warn_on_unsupported_python(installation.interpreter());
1089+
return Ok(Ok(installation));
1090+
}
1091+
// Continue if we can't find a matching Python and ignore non-critical discovery errors
1092+
Ok(Err(_)) => {}
1093+
Err(ref err) if !err.is_critical() => {}
1094+
_ => return result,
10811095
}
10821096

10831097
// If that fails, and a specific patch version was requested try again allowing a
@@ -1096,10 +1110,16 @@ pub fn find_best_python_installation(
10961110
_ => None,
10971111
} {
10981112
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);
1113+
let result = find_python_installation(&request, environments, preference, cache);
1114+
match result {
1115+
Ok(Ok(installation)) => {
1116+
warn_on_unsupported_python(installation.interpreter());
1117+
return Ok(Ok(installation));
1118+
}
1119+
// Continue if we can't find a matching Python and ignore non-critical discovery errors
1120+
Ok(Err(_)) => {}
1121+
Err(ref err) if !err.is_critical() => {}
1122+
_ => return result,
11031123
}
11041124
}
11051125

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)