Skip to content

Commit 3b43aa8

Browse files
committed
Auto merge of #13657 - weihanglo:package-cache-lock, r=epage
fix(generate-lockfile): hold lock before querying index
2 parents 5b2b1c0 + f1c1396 commit 3b43aa8

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

src/cargo/ops/cargo_generate_lockfile.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,17 @@ pub fn update_lockfile(ws: &Workspace<'_>, opts: &UpdateOptions<'_>) -> CargoRes
166166
Ok(())
167167
}
168168

169+
/// Prints lockfile change statuses.
170+
///
171+
/// This would acquire the package-cache lock, as it may update the index to
172+
/// show users latest available versions.
169173
pub fn print_lockfile_changes(
170174
gctx: &GlobalContext,
171175
previous_resolve: Option<&Resolve>,
172176
resolve: &Resolve,
173177
registry: &mut PackageRegistry<'_>,
174178
) -> CargoResult<()> {
179+
let _lock = gctx.acquire_package_cache_lock(CacheLockMode::DownloadExclusive)?;
175180
if let Some(previous_resolve) = previous_resolve {
176181
print_lockfile_sync(gctx, previous_resolve, resolve, registry)
177182
} else {
@@ -331,7 +336,7 @@ fn print_lockfile_sync(
331336
Ok(())
332337
}
333338

334-
pub fn print_lockfile_updates(
339+
fn print_lockfile_updates(
335340
gctx: &GlobalContext,
336341
previous_resolve: &Resolve,
337342
resolve: &Resolve,

src/cargo/ops/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ pub use self::cargo_doc::{doc, DocOptions, OutputFormat};
99
pub use self::cargo_fetch::{fetch, FetchOptions};
1010
pub use self::cargo_generate_lockfile::generate_lockfile;
1111
pub use self::cargo_generate_lockfile::print_lockfile_changes;
12-
pub use self::cargo_generate_lockfile::print_lockfile_updates;
1312
pub use self::cargo_generate_lockfile::update_lockfile;
1413
pub use self::cargo_generate_lockfile::UpdateOptions;
1514
pub use self::cargo_install::{install, install_list};

src/cargo/ops/resolve.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -256,11 +256,6 @@ fn resolve_with_registry<'gctx>(
256256
false
257257
};
258258
if print {
259-
// We only want one Cargo at a time resolving a crate graph since this can
260-
// involve a lot of frobbing of the global caches.
261-
let _lock = ws
262-
.gctx()
263-
.acquire_package_cache_lock(CacheLockMode::DownloadExclusive)?;
264259
ops::print_lockfile_changes(ws.gctx(), prev.as_ref(), &resolve, registry)?;
265260
}
266261
Ok(resolve)

tests/testsuite/generate_lockfile.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,39 @@ fn duplicate_entries_in_lockfile() {
237237
)
238238
.run();
239239
}
240+
241+
#[cargo_test]
242+
fn generate_lockfile_holds_lock_and_offline() {
243+
Package::new("syn", "1.0.0").publish();
244+
245+
let p = project()
246+
.file(
247+
"Cargo.toml",
248+
r#"
249+
[package]
250+
name = "foo"
251+
252+
[dependencies]
253+
syn = "1.0"
254+
"#,
255+
)
256+
.file("src/lib.rs", "")
257+
.build();
258+
259+
p.cargo("generate-lockfile")
260+
.with_stderr(
261+
"\
262+
[UPDATING] `[..]` index
263+
[LOCKING] 2 packages
264+
",
265+
)
266+
.run();
267+
268+
p.cargo("generate-lockfile --offline")
269+
.with_stderr_contains(
270+
"\
271+
[LOCKING] 2 packages
272+
",
273+
)
274+
.run();
275+
}

0 commit comments

Comments
 (0)