Skip to content

Commit

Permalink
Added a lockfile() method to ProjectWorkspace, so it can return the p…
Browse files Browse the repository at this point in the history
…rivate or shared one (astral-sh#4574)
  • Loading branch information
idlsoft committed Jun 28, 2024
1 parent 7cb15dd commit e66bada
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 13 deletions.
11 changes: 11 additions & 0 deletions crates/uv-distribution/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions crates/uv-requirements/src/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<LockedRequirements> {
pub async fn read_lockfile(project_workspace: &ProjectWorkspace, upgrade: &Upgrade) -> Result<LockedRequirements> {
// 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::<Lock>(&encoded) {
Ok(lock) => lock,
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/commands/project/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
11 changes: 6 additions & 5 deletions crates/uv/src/commands/project/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -56,7 +56,7 @@ pub(crate) async fn lock(

// Perform the lock operation.
match do_lock(
project.workspace(),
&project,
&interpreter,
settings.as_ref(),
preview,
Expand Down Expand Up @@ -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,
Expand All @@ -94,6 +94,7 @@ pub(super) async fn do_lock(
cache: &Cache,
printer: Printer,
) -> Result<Lock, ProjectError> {
let workspace = project_workspace.workspace();
// Extract the project settings.
let ResolverSettingsRef {
index_locations,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion crates/uv/src/commands/project/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/commands/project/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions crates/uv/src/commands/project/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?
};

Expand Down

0 comments on commit e66bada

Please sign in to comment.