Skip to content

Commit 110acb9

Browse files
authored
refactor: clean up workspace methods (#3114)
1 parent fc3e1a8 commit 110acb9

File tree

8 files changed

+133
-151
lines changed

8 files changed

+133
-151
lines changed

src/cli/lock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub async fn execute(args: Args) -> miette::Result<()> {
2727
.locate()?;
2828

2929
// Save the original lockfile to compare with the new one.
30-
let original_lock_file = workspace.get_lock_file().await?;
30+
let original_lock_file = workspace.load_lock_file().await?;
3131
let new_lock_file = workspace
3232
.update_lock_file(UpdateLockFileOptions {
3333
lock_file_usage: LockFileUsage::Update,

src/cli/update.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::{
66
WorkspaceLocator,
77
};
88
use crate::{
9-
load_lock_file,
109
lock_file::{filter_lock_file, UpdateContext},
1110
Workspace,
1211
};
@@ -145,17 +144,17 @@ pub async fn execute(args: Args) -> miette::Result<()> {
145144

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

150149
// If the user specified a package name, check to see if it is even locked.
151150
if let Some(packages) = &specs.packages {
152151
for package in packages {
153-
ensure_package_exists(&loaded_lock_file, package, &specs)?
152+
ensure_package_exists(loaded_lock_file, package, &specs)?
154153
}
155154
}
156155

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

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

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

177176
// Format as json?
178177
if args.json {
179-
let diff = LockFileDiff::from_lock_files(&loaded_lock_file, &updated_lock_file.lock_file);
178+
let diff = LockFileDiff::from_lock_files(loaded_lock_file, &updated_lock_file.lock_file);
180179
let json_diff = LockFileJsonDiff::new(Some(&workspace), diff);
181180
let json = serde_json::to_string_pretty(&json_diff).expect("failed to convert to json");
182181
println!("{}", json);

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ pub mod build;
1919
mod rlimit;
2020
mod utils;
2121

22-
pub use lock_file::{load_lock_file, UpdateLockFileOptions};
22+
pub use lock_file::UpdateLockFileOptions;
2323
pub use workspace::{DependencyType, Workspace, WorkspaceLocator};

src/lock_file/mod.rs

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ mod update;
88
mod utils;
99

1010
pub use crate::environment::CondaPrefixUpdater;
11-
use crate::Workspace;
12-
use miette::{IntoDiagnostic, WrapErr};
1311
pub(crate) use package_identifier::PypiPackageIdentifier;
1412
use pixi_record::PixiRecord;
15-
use rattler_lock::{LockFile, ParseCondaLockError, PypiPackageData, PypiPackageEnvironmentData};
13+
use rattler_lock::{PypiPackageData, PypiPackageEnvironmentData};
1614
pub(crate) use records_by_name::{PixiRecordsByName, PypiRecordsByName};
1715
pub(crate) use resolve::{
1816
conda::resolve_conda, pypi::resolve_pypi, uv_resolution_context::UvResolutionContext,
@@ -37,57 +35,26 @@ pub type LockedPypiPackages = Vec<PypiRecord>;
3735
/// data. In Pixi we basically always need both.
3836
pub type PypiRecord = (PypiPackageData, PypiPackageEnvironmentData);
3937

40-
/// Loads the lockfile for the specified project or returns a dummy one if none
41-
/// could be found.
42-
pub async fn load_lock_file(project: &Workspace) -> miette::Result<LockFile> {
43-
let lock_file_path = project.lock_file_path();
44-
if lock_file_path.is_file() {
45-
// Spawn a background task because loading the file might be IO bound.
46-
tokio::task::spawn_blocking(move || {
47-
LockFile::from_path(&lock_file_path)
48-
.map_err(|err| match err {
49-
ParseCondaLockError::IncompatibleVersion{ lock_file_version, max_supported_version} => {
50-
miette::miette!(
51-
help="Please update pixi to the latest version and try again.",
52-
"The lock file version is {}, but only up to including version {} is supported by the current version.",
53-
lock_file_version, max_supported_version
54-
)
55-
}
56-
_ => miette::miette!(err),
57-
})
58-
.wrap_err_with(|| {
59-
format!(
60-
"Failed to load lock file from `{}`",
61-
lock_file_path.display()
62-
)
63-
})
64-
})
65-
.await
66-
.unwrap_or_else(|e| Err(e).into_diagnostic())
67-
} else {
68-
Ok(LockFile::default())
69-
}
70-
}
71-
7238
#[cfg(test)]
7339
mod tests {
74-
use crate::{load_lock_file, Workspace};
40+
use crate::Workspace;
7541

7642
#[tokio::test]
7743
async fn test_load_newer_lock_file() {
7844
// Test that loading a lock file with a newer version than the current
7945
// version of pixi will return an error.
8046
let temp_dir = tempfile::tempdir().unwrap();
81-
let project = r#"
47+
let manifest_toml = r#"
8248
[project]
8349
name = "pixi"
8450
channels = []
8551
platforms = []
8652
"#;
87-
let project =
88-
Workspace::from_str(temp_dir.path().join("pixi.toml").as_path(), project).unwrap();
53+
let workspace =
54+
Workspace::from_str(temp_dir.path().join("pixi.toml").as_path(), manifest_toml)
55+
.unwrap();
8956

90-
let lock_file_path = project.lock_file_path();
57+
let lock_file_path = workspace.lock_file_path();
9158
let raw_lock_file = r#"
9259
version: 9999
9360
environments:
@@ -101,7 +68,7 @@ mod tests {
10168
.await
10269
.unwrap();
10370

104-
let err = load_lock_file(&project).await.unwrap_err();
71+
let err = &workspace.load_lock_file().await.unwrap_err();
10572
let dbg_err = format!("{:?}", err);
10673
// Test that the error message contains the correct information.
10774
assert!(dbg_err.contains("The lock file version is 9999, but only up to including version"));

src/lock_file/outdated.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ impl<'p> DisregardLockedContent<'p> {
6161
impl<'p> OutdatedEnvironments<'p> {
6262
/// Constructs a new instance of this struct by examining the project and
6363
/// lock-file and finding any mismatches.
64-
pub(crate) async fn from_project_and_lock_file(
65-
project: &'p Workspace,
64+
pub(crate) async fn from_workspace_and_lock_file(
65+
workspace: &'p Workspace,
6666
lock_file: &LockFile,
6767
glob_hash_cache: GlobHashCache,
6868
) -> Self {
@@ -71,7 +71,7 @@ impl<'p> OutdatedEnvironments<'p> {
7171
mut outdated_conda,
7272
mut outdated_pypi,
7373
disregard_locked_content,
74-
} = find_unsatisfiable_targets(project, lock_file, glob_hash_cache).await;
74+
} = find_unsatisfiable_targets(workspace, lock_file, glob_hash_cache).await;
7575

7676
// Extend the outdated targets to include the solve groups
7777
let (mut conda_solve_groups_out_of_date, mut pypi_solve_groups_out_of_date) =
@@ -80,7 +80,7 @@ impl<'p> OutdatedEnvironments<'p> {
8080
// Find all the solve groups that have inconsistent dependencies between
8181
// environments.
8282
find_inconsistent_solve_groups(
83-
project,
83+
workspace,
8484
lock_file,
8585
&outdated_conda,
8686
&mut conda_solve_groups_out_of_date,

0 commit comments

Comments
 (0)