Skip to content

Commit 03f084c

Browse files
committed
Auto merge of #8497 - alexcrichton:fix-rebuild-on-rename, r=ehuss
Fix freshness checks for build scripts on renamed dirs This commit fixes an issue in Cargo where when an entire project directory is renamed (preserving the target directory) then path dependencies with build scripts would have their build scripts rereun when building again. The problem with this was that when a build script doesn't print `rerun-if-changed` Cargo's conservative fingerprint listed an absolute path in it, which was intended to be a relative path. The fix here is to use a relative path in the fingerprint to ensure that it's not the reason a rebuild happens when directories are renamed.
2 parents 0a9f2ef + 64a4682 commit 03f084c

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

src/cargo/core/workspace.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -873,11 +873,7 @@ impl<'cfg> Workspace<'cfg> {
873873
MaybePackage::Package(ref p) => p.clone(),
874874
MaybePackage::Virtual(_) => continue,
875875
};
876-
let mut src = PathSource::new(
877-
pkg.manifest_path(),
878-
pkg.package_id().source_id(),
879-
self.config,
880-
);
876+
let mut src = PathSource::new(pkg.root(), pkg.package_id().source_id(), self.config);
881877
src.preload_with(pkg);
882878
registry.add_preloaded(Box::new(src));
883879
}

src/cargo/sources/path.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,10 @@ impl<'cfg> Source for PathSource<'cfg> {
522522

523523
fn fingerprint(&self, pkg: &Package) -> CargoResult<String> {
524524
let (max, max_path) = self.last_modified_file(pkg)?;
525+
// Note that we try to strip the prefix of this package to get a
526+
// relative path to ensure that the fingerprint remains consistent
527+
// across entire project directory renames.
528+
let max_path = max_path.strip_prefix(&self.path).unwrap_or(&max_path);
525529
Ok(format!("{} ({})", max, max_path.display()))
526530
}
527531

tests/testsuite/freshness.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -857,20 +857,30 @@ fn no_rebuild_when_rename_dir() {
857857
.file(
858858
"Cargo.toml",
859859
r#"
860-
[package]
861-
name = "bar"
862-
version = "0.0.1"
863-
authors = []
860+
[package]
861+
name = "bar"
862+
version = "0.0.1"
863+
authors = []
864864
865-
[dependencies]
866-
foo = { path = "foo" }
867-
"#,
865+
[workspace]
866+
867+
[dependencies]
868+
foo = { path = "foo" }
869+
"#,
868870
)
869-
.file("src/lib.rs", "")
871+
.file("src/_unused.rs", "")
872+
.file("build.rs", "fn main() {}")
870873
.file("foo/Cargo.toml", &basic_manifest("foo", "0.0.1"))
871874
.file("foo/src/lib.rs", "")
875+
.file("foo/build.rs", "fn main() {}")
872876
.build();
873877

878+
// make sure the most recently modified file is `src/lib.rs`, not
879+
// `Cargo.toml`, to expose a historical bug where we forgot to strip the
880+
// `Cargo.toml` path from looking for the package root.
881+
cargo_test_support::sleep_ms(100);
882+
fs::write(p.root().join("src/lib.rs"), "").unwrap();
883+
874884
p.cargo("build").run();
875885
let mut new = p.root();
876886
new.pop();

0 commit comments

Comments
 (0)