Skip to content

Commit

Permalink
Auto merge of #8497 - alexcrichton:fix-rebuild-on-rename, r=ehuss
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bors committed Jul 17, 2020
2 parents 0a9f2ef + 64a4682 commit 03f084c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
6 changes: 1 addition & 5 deletions src/cargo/core/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,11 +873,7 @@ impl<'cfg> Workspace<'cfg> {
MaybePackage::Package(ref p) => p.clone(),
MaybePackage::Virtual(_) => continue,
};
let mut src = PathSource::new(
pkg.manifest_path(),
pkg.package_id().source_id(),
self.config,
);
let mut src = PathSource::new(pkg.root(), pkg.package_id().source_id(), self.config);
src.preload_with(pkg);
registry.add_preloaded(Box::new(src));
}
Expand Down
4 changes: 4 additions & 0 deletions src/cargo/sources/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,10 @@ impl<'cfg> Source for PathSource<'cfg> {

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

Expand Down
26 changes: 18 additions & 8 deletions tests/testsuite/freshness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,20 +857,30 @@ fn no_rebuild_when_rename_dir() {
.file(
"Cargo.toml",
r#"
[package]
name = "bar"
version = "0.0.1"
authors = []
[package]
name = "bar"
version = "0.0.1"
authors = []
[dependencies]
foo = { path = "foo" }
"#,
[workspace]
[dependencies]
foo = { path = "foo" }
"#,
)
.file("src/lib.rs", "")
.file("src/_unused.rs", "")
.file("build.rs", "fn main() {}")
.file("foo/Cargo.toml", &basic_manifest("foo", "0.0.1"))
.file("foo/src/lib.rs", "")
.file("foo/build.rs", "fn main() {}")
.build();

// make sure the most recently modified file is `src/lib.rs`, not
// `Cargo.toml`, to expose a historical bug where we forgot to strip the
// `Cargo.toml` path from looking for the package root.
cargo_test_support::sleep_ms(100);
fs::write(p.root().join("src/lib.rs"), "").unwrap();

p.cargo("build").run();
let mut new = p.root();
new.pop();
Expand Down

0 comments on commit 03f084c

Please sign in to comment.