Skip to content

Commit

Permalink
refactor: clean up workspace methods (#3114)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hofer-Julian authored Feb 12, 2025
1 parent fc3e1a8 commit 110acb9
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 151 deletions.
2 changes: 1 addition & 1 deletion src/cli/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {
.locate()?;

// Save the original lockfile to compare with the new one.
let original_lock_file = workspace.get_lock_file().await?;
let original_lock_file = workspace.load_lock_file().await?;
let new_lock_file = workspace
.update_lock_file(UpdateLockFileOptions {
lock_file_usage: LockFileUsage::Update,
Expand Down
11 changes: 5 additions & 6 deletions src/cli/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{
WorkspaceLocator,
};
use crate::{
load_lock_file,
lock_file::{filter_lock_file, UpdateContext},
Workspace,
};
Expand Down Expand Up @@ -145,17 +144,17 @@ pub async fn execute(args: Args) -> miette::Result<()> {

// Load the current lock-file, if any. If none is found, a dummy lock-file is
// returned.
let loaded_lock_file = load_lock_file(&workspace).await?;
let loaded_lock_file = &workspace.load_lock_file().await?;

// If the user specified a package name, check to see if it is even locked.
if let Some(packages) = &specs.packages {
for package in packages {
ensure_package_exists(&loaded_lock_file, package, &specs)?
ensure_package_exists(loaded_lock_file, package, &specs)?
}
}

// Unlock dependencies in the lock-file that we want to update.
let relaxed_lock_file = unlock_packages(&workspace, &loaded_lock_file, &specs);
let relaxed_lock_file = unlock_packages(&workspace, loaded_lock_file, &specs);

// Update the packages in the lock-file.
let updated_lock_file = UpdateContext::builder(&workspace)
Expand All @@ -172,11 +171,11 @@ pub async fn execute(args: Args) -> miette::Result<()> {
}

// Determine the diff between the old and new lock-file.
let diff = LockFileDiff::from_lock_files(&loaded_lock_file, &updated_lock_file.lock_file);
let diff = LockFileDiff::from_lock_files(loaded_lock_file, &updated_lock_file.lock_file);

// Format as json?
if args.json {
let diff = LockFileDiff::from_lock_files(&loaded_lock_file, &updated_lock_file.lock_file);
let diff = LockFileDiff::from_lock_files(loaded_lock_file, &updated_lock_file.lock_file);
let json_diff = LockFileJsonDiff::new(Some(&workspace), diff);
let json = serde_json::to_string_pretty(&json_diff).expect("failed to convert to json");
println!("{}", json);
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ pub mod build;
mod rlimit;
mod utils;

pub use lock_file::{load_lock_file, UpdateLockFileOptions};
pub use lock_file::UpdateLockFileOptions;
pub use workspace::{DependencyType, Workspace, WorkspaceLocator};
49 changes: 8 additions & 41 deletions src/lock_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ mod update;
mod utils;

pub use crate::environment::CondaPrefixUpdater;
use crate::Workspace;
use miette::{IntoDiagnostic, WrapErr};
pub(crate) use package_identifier::PypiPackageIdentifier;
use pixi_record::PixiRecord;
use rattler_lock::{LockFile, ParseCondaLockError, PypiPackageData, PypiPackageEnvironmentData};
use rattler_lock::{PypiPackageData, PypiPackageEnvironmentData};
pub(crate) use records_by_name::{PixiRecordsByName, PypiRecordsByName};
pub(crate) use resolve::{
conda::resolve_conda, pypi::resolve_pypi, uv_resolution_context::UvResolutionContext,
Expand All @@ -37,57 +35,26 @@ pub type LockedPypiPackages = Vec<PypiRecord>;
/// data. In Pixi we basically always need both.
pub type PypiRecord = (PypiPackageData, PypiPackageEnvironmentData);

/// Loads the lockfile for the specified project or returns a dummy one if none
/// could be found.
pub async fn load_lock_file(project: &Workspace) -> miette::Result<LockFile> {
let lock_file_path = project.lock_file_path();
if lock_file_path.is_file() {
// Spawn a background task because loading the file might be IO bound.
tokio::task::spawn_blocking(move || {
LockFile::from_path(&lock_file_path)
.map_err(|err| match err {
ParseCondaLockError::IncompatibleVersion{ lock_file_version, max_supported_version} => {
miette::miette!(
help="Please update pixi to the latest version and try again.",
"The lock file version is {}, but only up to including version {} is supported by the current version.",
lock_file_version, max_supported_version
)
}
_ => miette::miette!(err),
})
.wrap_err_with(|| {
format!(
"Failed to load lock file from `{}`",
lock_file_path.display()
)
})
})
.await
.unwrap_or_else(|e| Err(e).into_diagnostic())
} else {
Ok(LockFile::default())
}
}

#[cfg(test)]
mod tests {
use crate::{load_lock_file, Workspace};
use crate::Workspace;

#[tokio::test]
async fn test_load_newer_lock_file() {
// Test that loading a lock file with a newer version than the current
// version of pixi will return an error.
let temp_dir = tempfile::tempdir().unwrap();
let project = r#"
let manifest_toml = r#"
[project]
name = "pixi"
channels = []
platforms = []
"#;
let project =
Workspace::from_str(temp_dir.path().join("pixi.toml").as_path(), project).unwrap();
let workspace =
Workspace::from_str(temp_dir.path().join("pixi.toml").as_path(), manifest_toml)
.unwrap();

let lock_file_path = project.lock_file_path();
let lock_file_path = workspace.lock_file_path();
let raw_lock_file = r#"
version: 9999
environments:
Expand All @@ -101,7 +68,7 @@ mod tests {
.await
.unwrap();

let err = load_lock_file(&project).await.unwrap_err();
let err = &workspace.load_lock_file().await.unwrap_err();
let dbg_err = format!("{:?}", err);
// Test that the error message contains the correct information.
assert!(dbg_err.contains("The lock file version is 9999, but only up to including version"));
Expand Down
8 changes: 4 additions & 4 deletions src/lock_file/outdated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ impl<'p> DisregardLockedContent<'p> {
impl<'p> OutdatedEnvironments<'p> {
/// Constructs a new instance of this struct by examining the project and
/// lock-file and finding any mismatches.
pub(crate) async fn from_project_and_lock_file(
project: &'p Workspace,
pub(crate) async fn from_workspace_and_lock_file(
workspace: &'p Workspace,
lock_file: &LockFile,
glob_hash_cache: GlobHashCache,
) -> Self {
Expand All @@ -71,7 +71,7 @@ impl<'p> OutdatedEnvironments<'p> {
mut outdated_conda,
mut outdated_pypi,
disregard_locked_content,
} = find_unsatisfiable_targets(project, lock_file, glob_hash_cache).await;
} = find_unsatisfiable_targets(workspace, lock_file, glob_hash_cache).await;

// Extend the outdated targets to include the solve groups
let (mut conda_solve_groups_out_of_date, mut pypi_solve_groups_out_of_date) =
Expand All @@ -80,7 +80,7 @@ impl<'p> OutdatedEnvironments<'p> {
// Find all the solve groups that have inconsistent dependencies between
// environments.
find_inconsistent_solve_groups(
project,
workspace,
lock_file,
&outdated_conda,
&mut conda_solve_groups_out_of_date,
Expand Down
Loading

0 comments on commit 110acb9

Please sign in to comment.