From e66bada3dce83abe6e7f5728fba939589ecb954b Mon Sep 17 00:00:00 2001 From: Sandu Turcan Date: Fri, 28 Jun 2024 00:45:27 -0400 Subject: [PATCH] Added a lockfile() method to ProjectWorkspace, so it can return the private or shared one (#4574) --- crates/uv-distribution/src/workspace.rs | 11 +++++++++++ crates/uv-requirements/src/upgrade.rs | 6 +++--- crates/uv/src/commands/project/add.rs | 2 +- crates/uv/src/commands/project/lock.rs | 11 ++++++----- crates/uv/src/commands/project/remove.rs | 2 +- crates/uv/src/commands/project/run.rs | 2 +- crates/uv/src/commands/project/sync.rs | 3 +-- 7 files changed, 24 insertions(+), 13 deletions(-) diff --git a/crates/uv-distribution/src/workspace.rs b/crates/uv-distribution/src/workspace.rs index cb1ecaaa4ec55..0e1573f9b9044 100644 --- a/crates/uv-distribution/src/workspace.rs +++ b/crates/uv-distribution/src/workspace.rs @@ -582,6 +582,17 @@ impl ProjectWorkspace { // } } + /// Th path to the private or workspace lockfile + pub fn lockfile(&self) -> PathBuf { + self.workspace.root.join("uv.lock") + // TODO #4574 replace with this: + // if self.current_project().private_lock { + // self.project_root.join("uv.lock") + // } else { + // self.workspace.root.join("uv.lock") + // } + } + /// Returns the [`Workspace`] containing the current project. pub fn workspace(&self) -> &Workspace { &self.workspace diff --git a/crates/uv-requirements/src/upgrade.rs b/crates/uv-requirements/src/upgrade.rs index 0231183071120..9aa0d9c50d29a 100644 --- a/crates/uv-requirements/src/upgrade.rs +++ b/crates/uv-requirements/src/upgrade.rs @@ -6,7 +6,7 @@ use anyhow::Result; use requirements_txt::RequirementsTxt; use uv_client::{BaseClientBuilder, Connectivity}; use uv_configuration::Upgrade; -use uv_distribution::Workspace; +use uv_distribution::ProjectWorkspace; use uv_git::ResolvedRepositoryReference; use uv_resolver::{Lock, Preference, PreferenceError}; @@ -64,14 +64,14 @@ pub async fn read_requirements_txt( } /// Load the preferred requirements from an existing lockfile, applying the upgrade strategy. -pub async fn read_lockfile(workspace: &Workspace, upgrade: &Upgrade) -> Result { +pub async fn read_lockfile(project_workspace: &ProjectWorkspace, upgrade: &Upgrade) -> Result { // As an optimization, skip reading the lockfile is we're upgrading all packages anyway. if upgrade.is_all() { return Ok(LockedRequirements::default()); } // If an existing lockfile exists, build up a set of preferences. - let lockfile = workspace.root().join("uv.lock"); + let lockfile = project_workspace.lockfile(); let lock = match fs_err::tokio::read_to_string(&lockfile).await { Ok(encoded) => match toml::from_str::(&encoded) { Ok(lock) => lock, diff --git a/crates/uv/src/commands/project/add.rs b/crates/uv/src/commands/project/add.rs index 4446fcb97089a..17810adfbc469 100644 --- a/crates/uv/src/commands/project/add.rs +++ b/crates/uv/src/commands/project/add.rs @@ -201,7 +201,7 @@ pub(crate) async fn add( // Lock and sync the environment. let lock = project::lock::do_lock( - project.workspace(), + &project, venv.interpreter(), settings.as_ref().into(), preview, diff --git a/crates/uv/src/commands/project/lock.rs b/crates/uv/src/commands/project/lock.rs index f2be2650992af..196a32defeb8f 100644 --- a/crates/uv/src/commands/project/lock.rs +++ b/crates/uv/src/commands/project/lock.rs @@ -7,7 +7,7 @@ use uv_cache::Cache; use uv_client::{Connectivity, FlatIndexClient, RegistryClientBuilder}; use uv_configuration::{Concurrency, ExtrasSpecification, PreviewMode, Reinstall, SetupPyStrategy}; use uv_dispatch::BuildDispatch; -use uv_distribution::{Workspace, DEV_DEPENDENCIES, ProjectWorkspace}; +use uv_distribution::{DEV_DEPENDENCIES, ProjectWorkspace}; use uv_git::GitResolver; use uv_requirements::upgrade::{read_lockfile, LockedRequirements}; use uv_resolver::{ @@ -56,7 +56,7 @@ pub(crate) async fn lock( // Perform the lock operation. match do_lock( - project.workspace(), + &project, &interpreter, settings.as_ref(), preview, @@ -84,7 +84,7 @@ pub(crate) async fn lock( /// Lock the project requirements into a lockfile. #[allow(clippy::too_many_arguments)] pub(super) async fn do_lock( - workspace: &Workspace, + project_workspace: &ProjectWorkspace, interpreter: &Interpreter, settings: ResolverSettingsRef<'_>, preview: PreviewMode, @@ -94,6 +94,7 @@ pub(super) async fn do_lock( cache: &Cache, printer: Printer, ) -> Result { + let workspace = project_workspace.workspace(); // Extract the project settings. let ResolverSettingsRef { index_locations, @@ -180,7 +181,7 @@ pub(super) async fn do_lock( }; // If an existing lockfile exists, build up a set of preferences. - let LockedRequirements { preferences, git } = read_lockfile(workspace, upgrade).await?; + let LockedRequirements { preferences, git } = read_lockfile(project_workspace, upgrade).await?; // Create the Git resolver. let git = GitResolver::from_refs(git); @@ -240,7 +241,7 @@ pub(super) async fn do_lock( // Write the lockfile to disk. let lock = Lock::from_resolution_graph(&resolution)?; let encoded = lock.to_toml()?; - fs_err::tokio::write(workspace.root().join("uv.lock"), encoded.as_bytes()).await?; + fs_err::tokio::write(project_workspace.lockfile(), encoded.as_bytes()).await?; Ok(lock) } diff --git a/crates/uv/src/commands/project/remove.rs b/crates/uv/src/commands/project/remove.rs index a3de4af0c240d..95e69f65c2939 100644 --- a/crates/uv/src/commands/project/remove.rs +++ b/crates/uv/src/commands/project/remove.rs @@ -105,7 +105,7 @@ pub(crate) async fn remove( // Lock and sync the environment. let lock = project::lock::do_lock( - project.workspace(), + &project, venv.interpreter(), settings.as_ref(), preview, diff --git a/crates/uv/src/commands/project/run.rs b/crates/uv/src/commands/project/run.rs index a37d87eaf0ee3..6ef1f44e47c68 100644 --- a/crates/uv/src/commands/project/run.rs +++ b/crates/uv/src/commands/project/run.rs @@ -90,7 +90,7 @@ pub(crate) async fn run( // Lock and sync the environment. let lock = project::lock::do_lock( - project.workspace(), + &project, venv.interpreter(), settings.as_ref().into(), preview, diff --git a/crates/uv/src/commands/project/sync.rs b/crates/uv/src/commands/project/sync.rs index eb36f52d4dc02..02323eed0ab53 100644 --- a/crates/uv/src/commands/project/sync.rs +++ b/crates/uv/src/commands/project/sync.rs @@ -58,8 +58,7 @@ pub(crate) async fn sync( // Read the lockfile. let lock: Lock = { - let encoded = - fs_err::tokio::read_to_string(project.workspace().root().join("uv.lock")).await?; + let encoded = fs_err::tokio::read_to_string(project.lockfile()).await?; toml::from_str(&encoded)? };