Skip to content

Commit 3f6c685

Browse files
committed
Auto merge of #11477 - kylematsuda:workspace_lockfile, r=weihanglo
Use workspace lockfile when running `cargo package` and `cargo publish` ### What does this PR try to resolve? Fix #11148. ### How should we test and review this PR? Please run the integration test in `tests/testsuite/publish_lockfile.rs` or try the steps from the issue.
2 parents 6ab160b + 8c4f27f commit 3f6c685

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::task::Poll;
99

1010
use crate::core::compiler::{BuildConfig, CompileMode, DefaultExecutor, Executor};
1111
use crate::core::resolver::CliFeatures;
12+
use crate::core::{registry::PackageRegistry, resolver::HasDevUnits};
1213
use crate::core::{Feature, Shell, Verbosity, Workspace};
1314
use crate::core::{Package, PackageId, PackageSet, Resolve, SourceId};
1415
use crate::sources::PathSource;
@@ -388,7 +389,18 @@ fn build_lock(ws: &Workspace<'_>, orig_pkg: &Package) -> CargoResult<String> {
388389

389390
// Regenerate Cargo.lock using the old one as a guide.
390391
let tmp_ws = Workspace::ephemeral(new_pkg, ws.config(), None, true)?;
391-
let (pkg_set, mut new_resolve) = ops::resolve_ws(&tmp_ws)?;
392+
let mut tmp_reg = PackageRegistry::new(ws.config())?;
393+
let mut new_resolve = ops::resolve_with_previous(
394+
&mut tmp_reg,
395+
&tmp_ws,
396+
&CliFeatures::new_all(true),
397+
HasDevUnits::Yes,
398+
orig_resolve.as_ref(),
399+
None,
400+
&[],
401+
true,
402+
)?;
403+
let pkg_set = ops::get_resolved_packages(&new_resolve, tmp_reg)?;
392404

393405
if let Some(orig_resolve) = orig_resolve {
394406
compare_resolve(config, tmp_ws.current()?, &orig_resolve, &new_resolve)?;

tests/testsuite/publish_lockfile.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,3 +492,101 @@ fn ignore_lockfile_inner() {
492492
)
493493
.run();
494494
}
495+
496+
#[cargo_test]
497+
fn use_workspace_root_lockfile() {
498+
// Issue #11148
499+
// Workspace members should use `Cargo.lock` at workspace root
500+
501+
Package::new("serde", "0.2.0").publish();
502+
503+
let p = project()
504+
.file(
505+
"Cargo.toml",
506+
r#"
507+
[package]
508+
name = "foo"
509+
version = "0.0.1"
510+
authors = []
511+
license = "MIT"
512+
description = "foo"
513+
514+
[dependencies]
515+
serde = "0.2"
516+
517+
[workspace]
518+
members = ["bar"]
519+
"#,
520+
)
521+
.file("src/main.rs", "fn main() {}")
522+
.file(
523+
"bar/Cargo.toml",
524+
r#"
525+
[package]
526+
name = "bar"
527+
version = "0.0.1"
528+
authors = []
529+
license = "MIT"
530+
description = "bar"
531+
workspace = ".."
532+
533+
[dependencies]
534+
serde = "0.2"
535+
"#,
536+
)
537+
.file("bar/src/main.rs", "fn main() {}")
538+
.build();
539+
540+
// Create `Cargo.lock` in the workspace root.
541+
p.cargo("generate-lockfile").run();
542+
543+
// Now, add a newer version of `serde`.
544+
Package::new("serde", "0.2.1").publish();
545+
546+
// Expect: package `bar` uses `serde v0.2.0` as required by workspace `Cargo.lock`.
547+
p.cargo("package --workspace")
548+
.with_stderr(
549+
"\
550+
[WARNING] manifest has no documentation, [..]
551+
See [..]
552+
[PACKAGING] bar v0.0.1 ([CWD]/bar)
553+
[UPDATING] `dummy-registry` index
554+
[VERIFYING] bar v0.0.1 ([CWD]/bar)
555+
[DOWNLOADING] crates ...
556+
[DOWNLOADED] serde v0.2.0 ([..])
557+
[COMPILING] serde v0.2.0
558+
[COMPILING] bar v0.0.1 ([CWD][..])
559+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
560+
[PACKAGED] 4 files, [..]
561+
[WARNING] manifest has no documentation, [..]
562+
See [..]
563+
[PACKAGING] foo v0.0.1 ([CWD])
564+
[VERIFYING] foo v0.0.1 ([CWD])
565+
[COMPILING] serde v0.2.0
566+
[COMPILING] foo v0.0.1 ([CWD][..])
567+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
568+
[PACKAGED] 4 files, [..]
569+
",
570+
)
571+
.run();
572+
573+
let package_path = p.root().join("target/package/foo-0.0.1.crate");
574+
assert!(package_path.is_file());
575+
let f = File::open(&package_path).unwrap();
576+
validate_crate_contents(
577+
f,
578+
"foo-0.0.1.crate",
579+
&["Cargo.lock", "Cargo.toml", "Cargo.toml.orig", "src/main.rs"],
580+
&[],
581+
);
582+
583+
let package_path = p.root().join("target/package/bar-0.0.1.crate");
584+
assert!(package_path.is_file());
585+
let f = File::open(&package_path).unwrap();
586+
validate_crate_contents(
587+
f,
588+
"bar-0.0.1.crate",
589+
&["Cargo.lock", "Cargo.toml", "Cargo.toml.orig", "src/main.rs"],
590+
&[],
591+
);
592+
}

0 commit comments

Comments
 (0)