diff --git a/crates/uv-python/src/version_files.rs b/crates/uv-python/src/version_files.rs index c592e1433..c7c7c0f30 100644 --- a/crates/uv-python/src/version_files.rs +++ b/crates/uv-python/src/version_files.rs @@ -25,7 +25,9 @@ impl PythonVersionFile { /// Find a Python version file in the given directory. pub async fn discover( working_directory: impl AsRef, + // TODO(zanieb): Create a `DiscoverySettings` struct for these options no_config: bool, + prefer_versions: bool, ) -> Result, std::io::Error> { let versions_path = working_directory.as_ref().join(PYTHON_VERSIONS_FILENAME); let version_path = working_directory.as_ref().join(PYTHON_VERSION_FILENAME); @@ -39,12 +41,16 @@ impl PythonVersionFile { return Ok(None); } - if let Some(result) = Self::try_from_path(version_path).await? { - return Ok(Some(result)); - }; - if let Some(result) = Self::try_from_path(versions_path).await? { - return Ok(Some(result)); + let paths = if prefer_versions { + [versions_path, version_path] + } else { + [version_path, versions_path] }; + for path in paths { + if let Some(result) = Self::try_from_path(path).await? { + return Ok(Some(result)); + }; + } Ok(None) } diff --git a/crates/uv/src/commands/project/add.rs b/crates/uv/src/commands/project/add.rs index 1383f32b1..5e7edaedd 100644 --- a/crates/uv/src/commands/project/add.rs +++ b/crates/uv/src/commands/project/add.rs @@ -125,7 +125,7 @@ pub(crate) async fn add( let python_request = if let Some(request) = python.as_deref() { // (1) Explicit request from user PythonRequest::parse(request) - } else if let Some(request) = PythonVersionFile::discover(&*CWD, false) + } else if let Some(request) = PythonVersionFile::discover(&*CWD, false, false) .await? .and_then(PythonVersionFile::into_version) { @@ -156,7 +156,7 @@ pub(crate) async fn add( let python_request = if let Some(request) = python.as_deref() { // (1) Explicit request from user Some(PythonRequest::parse(request)) - } else if let Some(request) = PythonVersionFile::discover(&*CWD, false) + } else if let Some(request) = PythonVersionFile::discover(&*CWD, false, false) .await? .and_then(PythonVersionFile::into_version) { diff --git a/crates/uv/src/commands/project/mod.rs b/crates/uv/src/commands/project/mod.rs index 9e8f1329b..4c5258391 100644 --- a/crates/uv/src/commands/project/mod.rs +++ b/crates/uv/src/commands/project/mod.rs @@ -177,9 +177,10 @@ impl WorkspacePython { let python_request = if let Some(request) = python_request { Some(request) // (2) Request from `.python-version` - } else if let Some(request) = PythonVersionFile::discover(workspace.install_path(), false) - .await? - .and_then(PythonVersionFile::into_version) + } else if let Some(request) = + PythonVersionFile::discover(workspace.install_path(), false, false) + .await? + .and_then(PythonVersionFile::into_version) { Some(request) // (3) `Requires-Python` in `pyproject.toml` diff --git a/crates/uv/src/commands/project/run.rs b/crates/uv/src/commands/project/run.rs index 43d978d16..789025219 100644 --- a/crates/uv/src/commands/project/run.rs +++ b/crates/uv/src/commands/project/run.rs @@ -109,7 +109,7 @@ pub(crate) async fn run( let python_request = if let Some(request) = python.as_deref() { Some(PythonRequest::parse(request)) // (2) Request from `.python-version` - } else if let Some(request) = PythonVersionFile::discover(&*CWD, false) + } else if let Some(request) = PythonVersionFile::discover(&*CWD, false, false) .await? .and_then(PythonVersionFile::into_version) { @@ -449,7 +449,7 @@ pub(crate) async fn run( Some(PythonRequest::parse(request)) // (2) Request from `.python-version` } else { - PythonVersionFile::discover(&*CWD, no_config) + PythonVersionFile::discover(&*CWD, no_config, false) .await? .and_then(PythonVersionFile::into_version) }; diff --git a/crates/uv/src/commands/python/find.rs b/crates/uv/src/commands/python/find.rs index d2f84052c..a1384f677 100644 --- a/crates/uv/src/commands/python/find.rs +++ b/crates/uv/src/commands/python/find.rs @@ -26,7 +26,7 @@ pub(crate) async fn find( // (2) Request from `.python-version` if request.is_none() { - request = PythonVersionFile::discover(&*CWD, no_config) + request = PythonVersionFile::discover(&*CWD, no_config, false) .await? .and_then(PythonVersionFile::into_version); } diff --git a/crates/uv/src/commands/python/install.rs b/crates/uv/src/commands/python/install.rs index 74ddad19a..ea71a6f1a 100644 --- a/crates/uv/src/commands/python/install.rs +++ b/crates/uv/src/commands/python/install.rs @@ -38,7 +38,7 @@ pub(crate) async fn install( let targets = targets.into_iter().collect::>(); let requests: Vec<_> = if targets.is_empty() { - PythonVersionFile::discover(&*CWD, no_config) + PythonVersionFile::discover(&*CWD, no_config, true) .await? .map(uv_python::PythonVersionFile::into_versions) .unwrap_or_else(|| vec![PythonRequest::Any]) diff --git a/crates/uv/src/commands/python/pin.rs b/crates/uv/src/commands/python/pin.rs index df509fcdd..fa200b4f5 100644 --- a/crates/uv/src/commands/python/pin.rs +++ b/crates/uv/src/commands/python/pin.rs @@ -38,7 +38,7 @@ pub(crate) async fn pin( } }; - let version_file = PythonVersionFile::discover(&*CWD, false).await; + let version_file = PythonVersionFile::discover(&*CWD, false, false).await; let Some(request) = request else { // Display the current pinned Python version diff --git a/crates/uv/src/commands/venv.rs b/crates/uv/src/commands/venv.rs index d5f05622c..7aa395649 100644 --- a/crates/uv/src/commands/venv.rs +++ b/crates/uv/src/commands/venv.rs @@ -146,7 +146,7 @@ async fn venv_impl( // (2) Request from `.python-version` if interpreter_request.is_none() { // TODO(zanieb): Support `--no-config` here - interpreter_request = PythonVersionFile::discover(&*CWD, false) + interpreter_request = PythonVersionFile::discover(&*CWD, false, false) .await .into_diagnostic()? .and_then(PythonVersionFile::into_version);