From 37388f09875c90f29ba1b1b52a7736b34c6cfc6a Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 29 Jul 2024 16:47:34 -0400 Subject: [PATCH] Make directory a required argument (#5573) --- crates/uv-python/src/version_files.rs | 26 ++++++++---------------- crates/uv/src/commands/project/mod.rs | 4 +--- crates/uv/src/commands/project/run.rs | 2 +- crates/uv/src/commands/python/install.rs | 2 +- crates/uv/src/commands/python/pin.rs | 7 +++++-- crates/uv/src/commands/venv.rs | 5 ++++- 6 files changed, 20 insertions(+), 26 deletions(-) diff --git a/crates/uv-python/src/version_files.rs b/crates/uv-python/src/version_files.rs index b3b36de54..ba794b7bc 100644 --- a/crates/uv-python/src/version_files.rs +++ b/crates/uv-python/src/version_files.rs @@ -16,7 +16,7 @@ pub static PYTHON_VERSIONS_FILENAME: &str = ".python-versions"; /// Prefers `.python-versions` then `.python-version`. /// If only one Python version is desired, use [`request_from_version_files`] which prefers the `.python-version` file. pub async fn requests_from_version_file( - directory: Option<&Path>, + directory: &Path, ) -> Result>, std::io::Error> { if let Some(versions) = read_versions_file(directory).await? { Ok(Some( @@ -40,7 +40,7 @@ pub async fn requests_from_version_file( /// Prefers `.python-version` then the first entry of `.python-versions`. /// If multiple Python versions are desired, use [`requests_from_version_files`] instead. pub async fn request_from_version_file( - directory: Option<&Path>, + directory: &Path, ) -> Result, std::io::Error> { if let Some(version) = read_version_file(directory).await? { Ok(Some(PythonRequest::parse(&version))) @@ -61,15 +61,9 @@ pub async fn write_version_file(version: &str) -> Result<(), std::io::Error> { fs::tokio::write(PYTHON_VERSION_FILENAME, format!("{version}\n")).await } -async fn read_versions_file( - directory: Option<&Path>, -) -> Result>, std::io::Error> { - let file_path = directory.map(|pth| pth.join(PYTHON_VERSIONS_FILENAME)); - let path = file_path - .as_deref() - .unwrap_or(Path::new(PYTHON_VERSIONS_FILENAME)); - - match fs::tokio::read_to_string(path).await { +async fn read_versions_file(directory: &Path) -> Result>, std::io::Error> { + let path = directory.join(PYTHON_VERSIONS_FILENAME); + match fs::tokio::read_to_string(&path).await { Ok(content) => { debug!("Reading requests from `{}`", path.display()); Ok(Some( @@ -89,13 +83,9 @@ async fn read_versions_file( } } -async fn read_version_file(directory: Option<&Path>) -> Result, std::io::Error> { - let file_path = directory.map(|pth| pth.join(PYTHON_VERSION_FILENAME)); - let path = file_path - .as_deref() - .unwrap_or(Path::new(PYTHON_VERSION_FILENAME)); - - match fs::tokio::read_to_string(path).await { +async fn read_version_file(directory: &Path) -> Result, std::io::Error> { + let path = directory.join(PYTHON_VERSION_FILENAME); + match fs::tokio::read_to_string(&path).await { Ok(content) => { debug!("Reading requests from `{}`", path.display()); Ok(content diff --git a/crates/uv/src/commands/project/mod.rs b/crates/uv/src/commands/project/mod.rs index 57cf67488..999c89db9 100644 --- a/crates/uv/src/commands/project/mod.rs +++ b/crates/uv/src/commands/project/mod.rs @@ -161,9 +161,7 @@ impl FoundInterpreter { let python_request = if let Some(request) = python_request { Some(request) // (2) Request from `.python-version` - } else if let Some(request) = - request_from_version_file(Some(workspace.install_path())).await? - { + } else if let Some(request) = request_from_version_file(workspace.install_path()).await? { Some(request) // (3) `Requires-Python` in `pyproject.toml` } else { diff --git a/crates/uv/src/commands/project/run.rs b/crates/uv/src/commands/project/run.rs index 53ac62d44..3a58ef208 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) = request_from_version_file(Some(&directory)).await? { + } else if let Some(request) = request_from_version_file(&directory).await? { Some(request) // (3) `Requires-Python` in `pyproject.toml` } else { diff --git a/crates/uv/src/commands/python/install.rs b/crates/uv/src/commands/python/install.rs index ae7bb3d46..6032f0ad3 100644 --- a/crates/uv/src/commands/python/install.rs +++ b/crates/uv/src/commands/python/install.rs @@ -55,7 +55,7 @@ pub(crate) async fn install( } None } else { - requests_from_version_file(None).await? + requests_from_version_file(&std::env::current_dir()?).await? }; version_file_requests.unwrap_or_else(|| vec![PythonRequest::Any]) } else { diff --git a/crates/uv/src/commands/python/pin.rs b/crates/uv/src/commands/python/pin.rs index 23ca8dada..74053a2b2 100644 --- a/crates/uv/src/commands/python/pin.rs +++ b/crates/uv/src/commands/python/pin.rs @@ -49,7 +49,7 @@ pub(crate) async fn pin( let Some(request) = request else { // Display the current pinned Python version - if let Some(pins) = requests_from_version_file(None).await? { + if let Some(pins) = requests_from_version_file(&std::env::current_dir()?).await? { for pin in pins { writeln!(printer.stdout(), "{}", pin.to_canonical_string())?; if let Some(virtual_project) = &virtual_project { @@ -126,7 +126,10 @@ pub(crate) async fn pin( request.to_canonical_string() }; - let existing = request_from_version_file(None).await.ok().flatten(); + let existing = request_from_version_file(&std::env::current_dir()?) + .await + .ok() + .flatten(); write_version_file(&output).await?; if let Some(existing) = existing diff --git a/crates/uv/src/commands/venv.rs b/crates/uv/src/commands/venv.rs index 6ebab8b3a..67236e56e 100644 --- a/crates/uv/src/commands/venv.rs +++ b/crates/uv/src/commands/venv.rs @@ -140,7 +140,10 @@ async fn venv_impl( let mut interpreter_request = python_request.map(PythonRequest::parse); if preview.is_enabled() && interpreter_request.is_none() { - interpreter_request = request_from_version_file(None).await.into_diagnostic()?; + interpreter_request = + request_from_version_file(&std::env::current_dir().into_diagnostic()?) + .await + .into_diagnostic()?; } if preview.is_disabled() && relocatable { warn_user_once!("`--relocatable` is experimental and may change without warning");