Ed Page 2dc9c20034
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](https://github.com/rust-lang/cargo/issues/14125#issuecomment-3127467689)
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 https://github.com/rust-lang/cargo/issues/14941
2025-08-12 19:27:53 +00:00
..