Skip to content

Commit 2dc9c20

Browse files
authored
perf(package): Always reuse the workspace's target-dir (#15783)
### What does this PR try to resolve? While preparing to stabilize `build-dir`, [it was discovered](#14125 (comment)) that `cargo package` will sometimes use the build cache for dependency crates if `CARGO_TARGET_DIR` is explicitly set. If target-dir is not set, the build will be preformed in an "inner target dir" at `target/package/{name}-{version}/target` and rebuild everything. This PR changes the default behavior to always use the build cache, matching the behavior of having `target-dir` set. ### How to test and review this PR? Added a dedicated test to verify the previous behavior (cargo recompiled non-workspace crates) in the first commit. Then updated it to verify the new behavior. ### Related links/notes * I did some Git archaeology and I _think_ this behavior was introduced in #2912 * While searching through the commit/pr history was never able to find any explicit reason why the behavior is the way it is. * Even if it was intentional, it has been 9 years since and Cargo's fingerprinting/rebuild detection is much more mature now so I believe its worth revisiting if this is still the desired behavior. * Would help mitigate #14941
2 parents ece031c + 81f119d commit 2dc9c20

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

src/cargo/ops/cargo_package/verify.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,13 @@ pub fn run_verify(
6666
let new_pkg = src.root_package()?;
6767
let pkg_fingerprint = hash_all(&dst)?;
6868

69+
// When packaging we use an ephemeral workspace but reuse the build cache to reduce
70+
// verification time if the user has already compiled the dependencies and the fingerprint
71+
// is unchanged.
6972
let target_dir = if gctx.cli_unstable().build_dir {
7073
Some(ws.build_dir())
7174
} else {
72-
None
75+
Some(ws.target_dir())
7376
};
7477

7578
let mut ws = Workspace::ephemeral(new_pkg, gctx, target_dir, true)?;

tests/testsuite/package.rs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,6 +1827,45 @@ fn package_two_kinds_of_deps() {
18271827
p.cargo("package --no-verify").run();
18281828
}
18291829

1830+
#[cargo_test]
1831+
fn package_should_use_build_cache() {
1832+
Package::new("other", "1.0.0").publish();
1833+
let p = project()
1834+
.file(
1835+
"Cargo.toml",
1836+
r#"
1837+
[package]
1838+
name = "foo"
1839+
version = "0.0.1"
1840+
edition = "2015"
1841+
authors = []
1842+
1843+
[dependencies]
1844+
other = "1.0"
1845+
"#,
1846+
)
1847+
.file("src/lib.rs", "")
1848+
.build();
1849+
1850+
// Build once so that the build cache is populated
1851+
p.cargo("build").run();
1852+
1853+
// Run package and verify we do not rebuild the `other` crate
1854+
p.cargo("package")
1855+
.with_stderr_data(str![[r#"
1856+
[WARNING] manifest has no description, license, license-file, documentation, homepage or repository.
1857+
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
1858+
[PACKAGING] foo v0.0.1 ([ROOT]/foo)
1859+
[UPDATING] `dummy-registry` index
1860+
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
1861+
[VERIFYING] foo v0.0.1 ([ROOT]/foo)
1862+
[COMPILING] foo v0.0.1 ([ROOT]/foo/target/package/foo-0.0.1)
1863+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
1864+
1865+
"#]])
1866+
.run();
1867+
}
1868+
18301869
#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")]
18311870
fn package_public_dep() {
18321871
Package::new("bar", "1.0.0").publish();
@@ -5705,7 +5744,6 @@ fn workspace_with_local_deps() {
57055744
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
57065745
[VERIFYING] level1 v0.0.1 ([ROOT]/foo/level1)
57075746
[UNPACKING] level2 v0.0.1 (registry `[ROOT]/foo/target/package/tmp-registry`)
5708-
[COMPILING] level3 v0.0.1
57095747
[COMPILING] level2 v0.0.1
57105748
[COMPILING] level1 v0.0.1 ([ROOT]/foo/target/package/level1-0.0.1)
57115749
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
@@ -6614,7 +6652,6 @@ fn workspace_with_renamed_member() {
66146652
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
66156653
[VERIFYING] obeli-sk-utils v0.16.2 ([ROOT]/foo/crates/utils)
66166654
[UNPACKING] obeli-sk-concepts v0.16.2 (registry `[ROOT]/foo/target/package/tmp-registry`)
6617-
[COMPILING] obeli-sk-val-json v0.16.2
66186655
[COMPILING] obeli-sk-concepts v0.16.2
66196656
[COMPILING] obeli-sk-utils v0.16.2 ([ROOT]/foo/target/package/obeli-sk-utils-0.16.2)
66206657
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
@@ -6997,7 +7034,6 @@ fn registry_inference_ignores_unpublishable() {
69977034
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
69987035
[VERIFYING] main v0.0.1 ([ROOT]/foo/main)
69997036
[UPDATING] `alternative` index
7000-
[COMPILING] dep v0.1.0 (registry `alternative`)
70017037
[COMPILING] main v0.0.1 ([ROOT]/foo/target/package/main-0.0.1)
70027038
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
70037039

tests/testsuite/publish.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,6 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for
13411341
[UPDATING] crates.io index
13421342
[PACKAGED] 4 files, [FILE_SIZE]B ([FILE_SIZE]B compressed)
13431343
[VERIFYING] foo v0.0.1 ([ROOT]/foo)
1344-
[COMPILING] bar v1.0.0
13451344
[COMPILING] foo v0.0.1 ([ROOT]/foo/target/package/foo-0.0.1)
13461345
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
13471346
[UPLOADING] foo v0.0.1 ([ROOT]/foo)
@@ -3746,7 +3745,6 @@ fn workspace_with_local_deps() {
37463745
[VERIFYING] level1 v0.0.1 ([ROOT]/foo/level1)
37473746
[UPDATING] crates.io index
37483747
[UNPACKING] level2 v0.0.1 (registry `[ROOT]/foo/target/package/tmp-registry`)
3749-
[COMPILING] level3 v0.0.1
37503748
[COMPILING] level2 v0.0.1
37513749
[COMPILING] level1 v0.0.1 ([ROOT]/foo/target/package/level1-0.0.1)
37523750
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s

tests/testsuite/publish_lockfile.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,6 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for
563563
[COMPILING] bar v0.0.1 ([ROOT]/foo/target/package/bar-0.0.1)
564564
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
565565
[VERIFYING] foo v0.0.1 ([ROOT]/foo)
566-
[COMPILING] serde v0.2.0
567566
[COMPILING] foo v0.0.1 ([ROOT]/foo/target/package/foo-0.0.1)
568567
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
569568

0 commit comments

Comments
 (0)