mirror of
https://github.com/rust-lang/cargo.git
synced 2025-09-25 11:14:46 +00:00

### 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