Skip to content

Commit

Permalink
Auto merge of #11477 - kylematsuda:workspace_lockfile, r=weihanglo
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Dec 19, 2022
2 parents 6ab160b + 8c4f27f commit 3f6c685
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::task::Poll;

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

// Regenerate Cargo.lock using the old one as a guide.
let tmp_ws = Workspace::ephemeral(new_pkg, ws.config(), None, true)?;
let (pkg_set, mut new_resolve) = ops::resolve_ws(&tmp_ws)?;
let mut tmp_reg = PackageRegistry::new(ws.config())?;
let mut new_resolve = ops::resolve_with_previous(
&mut tmp_reg,
&tmp_ws,
&CliFeatures::new_all(true),
HasDevUnits::Yes,
orig_resolve.as_ref(),
None,
&[],
true,
)?;
let pkg_set = ops::get_resolved_packages(&new_resolve, tmp_reg)?;

if let Some(orig_resolve) = orig_resolve {
compare_resolve(config, tmp_ws.current()?, &orig_resolve, &new_resolve)?;
Expand Down
98 changes: 98 additions & 0 deletions tests/testsuite/publish_lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,3 +492,101 @@ fn ignore_lockfile_inner() {
)
.run();
}

#[cargo_test]
fn use_workspace_root_lockfile() {
// Issue #11148
// Workspace members should use `Cargo.lock` at workspace root

Package::new("serde", "0.2.0").publish();

let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
license = "MIT"
description = "foo"
[dependencies]
serde = "0.2"
[workspace]
members = ["bar"]
"#,
)
.file("src/main.rs", "fn main() {}")
.file(
"bar/Cargo.toml",
r#"
[package]
name = "bar"
version = "0.0.1"
authors = []
license = "MIT"
description = "bar"
workspace = ".."
[dependencies]
serde = "0.2"
"#,
)
.file("bar/src/main.rs", "fn main() {}")
.build();

// Create `Cargo.lock` in the workspace root.
p.cargo("generate-lockfile").run();

// Now, add a newer version of `serde`.
Package::new("serde", "0.2.1").publish();

// Expect: package `bar` uses `serde v0.2.0` as required by workspace `Cargo.lock`.
p.cargo("package --workspace")
.with_stderr(
"\
[WARNING] manifest has no documentation, [..]
See [..]
[PACKAGING] bar v0.0.1 ([CWD]/bar)
[UPDATING] `dummy-registry` index
[VERIFYING] bar v0.0.1 ([CWD]/bar)
[DOWNLOADING] crates ...
[DOWNLOADED] serde v0.2.0 ([..])
[COMPILING] serde v0.2.0
[COMPILING] bar v0.0.1 ([CWD][..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[PACKAGED] 4 files, [..]
[WARNING] manifest has no documentation, [..]
See [..]
[PACKAGING] foo v0.0.1 ([CWD])
[VERIFYING] foo v0.0.1 ([CWD])
[COMPILING] serde v0.2.0
[COMPILING] foo v0.0.1 ([CWD][..])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[PACKAGED] 4 files, [..]
",
)
.run();

let package_path = p.root().join("target/package/foo-0.0.1.crate");
assert!(package_path.is_file());
let f = File::open(&package_path).unwrap();
validate_crate_contents(
f,
"foo-0.0.1.crate",
&["Cargo.lock", "Cargo.toml", "Cargo.toml.orig", "src/main.rs"],
&[],
);

let package_path = p.root().join("target/package/bar-0.0.1.crate");
assert!(package_path.is_file());
let f = File::open(&package_path).unwrap();
validate_crate_contents(
f,
"bar-0.0.1.crate",
&["Cargo.lock", "Cargo.toml", "Cargo.toml.orig", "src/main.rs"],
&[],
);
}

0 comments on commit 3f6c685

Please sign in to comment.