Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show non-critical Python discovery errors if no other interpreter is found #10716

Merged
merged 7 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions crates/uv-python/src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -977,9 +977,16 @@ pub(crate) fn find_python_installation(
) -> Result<FindPythonResult, Error> {
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() {
if let Err(err) = result {
first_error = Some(err);
}
}
continue;
}

Expand Down Expand Up @@ -1032,6 +1039,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(err) = first_error {
return Err(err);
}

Ok(Err(PythonNotFound {
request: request.clone(),
environment_preference: environments,
Expand Down
9 changes: 4 additions & 5 deletions crates/uv-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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(())
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/tests/it/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ impl TestContext {
.env(EnvVars::UV_PREVIEW, "1")
.env(EnvVars::UV_PYTHON_INSTALL_DIR, "")
.current_dir(&self.temp_dir);
self.add_shared_args(&mut command, true);
self.add_shared_args(&mut command, false);
command
}

Expand Down
18 changes: 17 additions & 1 deletion crates/uv/tests/it/pip_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ fn missing_requirements_txt() {

#[test]
fn missing_venv() -> Result<()> {
let context = TestContext::new("3.12");
let context = TestContext::new("3.12")
.with_filtered_virtualenv_bin()
.with_filtered_python_names();

let requirements = context.temp_dir.child("requirements.txt");
requirements.write_str("anyio")?;
fs::remove_dir_all(&context.venv)?;
Expand All @@ -61,6 +64,19 @@ fn missing_venv() -> Result<()> {
exit_code: 2
----- stdout -----

----- stderr -----
error: Failed to inspect Python interpreter from active virtual environment at `.venv/[BIN]/python`
Caused by: Python interpreter not found at `[VENV]/[BIN]/python`
"###);

assert!(predicates::path::missing().eval(&context.venv));

// If not "active", we hint to create one
uv_snapshot!(context.filters(), context.pip_sync().arg("requirements.txt").env_remove("VIRTUAL_ENV"), @r###"
success: false
exit_code: 2
----- stdout -----

----- stderr -----
error: No virtual environment found; run `uv venv` to create an environment, or pass `--system` to install into a non-virtual environment
"###);
Expand Down
8 changes: 4 additions & 4 deletions crates/uv/tests/it/python_find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ fn python_find_venv_invalid() {
.with_filtered_virtualenv_bin();

// We find the virtual environment
uv_snapshot!(context.filters(), context.python_find(), @r###"
uv_snapshot!(context.filters(), context.python_find().env(EnvVars::VIRTUAL_ENV, context.venv.as_os_str()), @r###"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -581,7 +581,7 @@ fn python_find_venv_invalid() {
// If the binaries are missing from a virtual environment, we fail
fs_err::remove_dir_all(venv_bin_path(&context.venv)).unwrap();

uv_snapshot!(context.filters(), context.python_find(), @r###"
uv_snapshot!(context.filters(), context.python_find().env(EnvVars::VIRTUAL_ENV, context.venv.as_os_str()), @r###"
success: false
exit_code: 2
----- stdout -----
Expand All @@ -592,7 +592,7 @@ fn python_find_venv_invalid() {
"###);

// Unless the virtual environment is not active
uv_snapshot!(context.filters(), context.python_find().env_remove(EnvVars::VIRTUAL_ENV), @r###"
uv_snapshot!(context.filters(), context.python_find(), @r###"
success: true
exit_code: 0
----- stdout -----
Expand All @@ -604,7 +604,7 @@ fn python_find_venv_invalid() {
// If there's not a `pyvenv.cfg` file, it's also non-fatal, we ignore the environment
fs_err::remove_file(context.venv.join("pyvenv.cfg")).unwrap();

uv_snapshot!(context.filters(), context.python_find(), @r###"
uv_snapshot!(context.filters(), context.python_find().env(EnvVars::VIRTUAL_ENV, context.venv.as_os_str()), @r###"
success: true
exit_code: 0
----- stdout -----
Expand Down
Loading