Skip to content
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
18 changes: 4 additions & 14 deletions crates/uv-python-commands/src/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::path::Path;
use uv_cache::Cache;
use uv_client::BaseClientBuilder;
use uv_configuration::{ActiveEnvironment, DependencyGroupsWithDefaults};
use uv_errors::ErrorWithHints;
use uv_fs::Simplified;
use uv_python_discovery::ConfigDiscovery;
use uv_python_discovery::PythonInstallation;
Expand All @@ -19,6 +18,7 @@ use uv_workspace::{DiscoveryOptions, VirtualProject, WorkspaceCache, WorkspaceEr

use uv_command_support::ExitStatus;
use uv_command_support::Printer;
use uv_command_support::UvError;
use uv_python_discovery::ProjectPythonRequest;
use uv_python_discovery::ScriptInterpreter;

Expand Down Expand Up @@ -139,7 +139,7 @@ pub async fn find_script(
cache: &Cache,
printer: Printer,
) -> Result<ExitStatus> {
let interpreter = match ScriptInterpreter::discover(
let interpreter = ScriptInterpreter::discover(
script,
None,
client_builder,
Expand All @@ -154,18 +154,8 @@ pub async fn find_script(
printer,
)
.await
{
Err(error) => {
writeln!(
printer.stderr(),
"{}",
ErrorWithHints::new(&error, uv_errors::Hinted::hints(&error))
)?;
return Ok(ExitStatus::Failure);
}
Ok(ScriptInterpreter::Interpreter(selection)) => selection.into_interpreter(),
Ok(ScriptInterpreter::Environment(environment)) => environment.into_interpreter(),
};
.map_err(UvError::user)?
.into_interpreter();

if show_version {
writeln!(printer.stdout(), "{}", interpreter.python_version())?;
Expand Down
40 changes: 37 additions & 3 deletions crates/uv/tests/python/python_find.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;

use anyhow::Result;
use assert_cmd::assert::OutputAssertExt;
use assert_fs::prelude::{FileTouch, PathChild};
Expand Down Expand Up @@ -1083,7 +1086,10 @@ fn python_find_script_no_environment() {

#[test]
fn python_find_script_python_not_found() {
let context = uv_test::test_context_with_versions!(&[]).with_filtered_python_sources();
let context = uv_test::test_context_with_versions!(&[])
.with_env(EnvVars::UV_PYTHON_PREFERENCE, "only-managed")
.with_env(EnvVars::UV_INTERNAL__TEST_PYTHON_MANAGED, "");
context.temp_dir.child(".python-version").touch().unwrap();

let script = context.temp_dir.child("foo.py");

Expand All @@ -1098,12 +1104,40 @@ fn python_find_script_python_not_found() {
uv_snapshot!(context.filters(), context.python_find().arg("--script").arg("foo.py"), @"
exit_code: 1 (failure)
----- stderr -----
No interpreter found in [PYTHON SOURCES]
error: No interpreter found in virtual environments or managed installations

hint: A managed Python download is available, but Python downloads are set to 'never'
");
}

#[cfg(unix)]
#[test]
fn python_find_script_interpreter_error_chain() -> Result<()> {
let context = uv_test::test_context_with_versions!(&[]);
context.temp_dir.child("foo.py").write_str(indoc! {r"
# /// script
# dependencies = []
# ///
"})?;
let python = context.temp_dir.child("broken-python");
python.write_str("#!/bin/sh\nprintf 'interpreter probe failed\\n' >&2\nexit 42\n")?;
fs_err::set_permissions(&python, std::fs::Permissions::from_mode(0o755))?;
context
.temp_dir
.child(".python-version")
.write_str(python.path().to_str().expect("UTF-8 fixture path"))?;
uv_snapshot!(context.filters(), context.python_find().arg("--script").arg("foo.py").arg("-q"), @"
exit_code: 1 (failure)
----- stderr -----
error: Failed to inspect Python interpreter from provided path at `broken-python`
cause: Querying Python at `[TEMP_DIR]/broken-python` failed with exit status exit status: 42

[stderr]
interpreter probe failed
");
Ok(())
}

#[test]
fn python_find_script_no_such_version() {
let context = uv_test::test_context!("3.13")
Expand Down Expand Up @@ -1141,7 +1175,7 @@ fn python_find_script_no_such_version() {
uv_snapshot!(context.filters(), context.python_find().arg("--script").arg("foo.py"), @"
exit_code: 1 (failure)
----- stderr -----
No interpreter found for Python >=3.16 in [PYTHON SOURCES]
error: No interpreter found for Python >=3.16 in [PYTHON SOURCES]
");
}

Expand Down
Loading
⌂ 换网址