Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(generate-lockfile): hold lock before querying index #13657

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/cargo/ops/cargo_generate_lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,17 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes
Ok(())
}

/// Prints lockfile change statuses.
///
/// This would acquire the package-cache lock, as it may update the index to
/// show users latest available versions.
pub fn print_lockfile_changes(
gctx: &GlobalContext,
previous_resolve: Option<&Resolve>,
resolve: &Resolve,
registry: &mut PackageRegistry<'_>,
) -> CargoResult<()> {
let _lock = gctx.acquire_package_cache_lock(CacheLockMode::DownloadExclusive)?;
if let Some(previous_resolve) = previous_resolve {
print_lockfile_sync(gctx, previous_resolve, resolve, registry)
} else {
Expand Down Expand Up @@ -331,7 +336,7 @@ fn print_lockfile_sync(
Ok(())
}

pub fn print_lockfile_updates(
fn print_lockfile_updates(
gctx: &GlobalContext,
previous_resolve: &Resolve,
resolve: &Resolve,
Expand Down
1 change: 0 additions & 1 deletion src/cargo/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub use self::cargo_doc::{doc, DocOptions, OutputFormat};
pub use self::cargo_fetch::{fetch, FetchOptions};
pub use self::cargo_generate_lockfile::generate_lockfile;
pub use self::cargo_generate_lockfile::print_lockfile_changes;
pub use self::cargo_generate_lockfile::print_lockfile_updates;
pub use self::cargo_generate_lockfile::update_lockfile;
pub use self::cargo_generate_lockfile::UpdateOptions;
pub use self::cargo_install::{install, install_list};
Expand Down
5 changes: 0 additions & 5 deletions src/cargo/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,6 @@ fn resolve_with_registry<'gctx>(
false
};
if print {
// We only want one Cargo at a time resolving a crate graph since this can
// involve a lot of frobbing of the global caches.
let _lock = ws
.gctx()
.acquire_package_cache_lock(CacheLockMode::DownloadExclusive)?;
ops::print_lockfile_changes(ws.gctx(), prev.as_ref(), &resolve, registry)?;
}
Ok(resolve)
Expand Down
36 changes: 36 additions & 0 deletions tests/testsuite/generate_lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,39 @@ fn duplicate_entries_in_lockfile() {
)
.run();
}

#[cargo_test]
fn generate_lockfile_holds_lock_and_offline() {
Package::new("syn", "1.0.0").publish();

let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"

[dependencies]
syn = "1.0"
"#,
)
.file("src/lib.rs", "")
.build();

p.cargo("generate-lockfile")
.with_stderr(
"\
[UPDATING] `[..]` index
[LOCKING] 2 packages
",
)
.run();

p.cargo("generate-lockfile --offline")
.with_stderr_contains(
"\
[LOCKING] 2 packages
",
)
.run();
}