Skip to content

Commit 9e01c8b

Browse files
committed
skip mtime checks for paths in $CARGO_HOME
1 parent 48e0c53 commit 9e01c8b

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

src/cargo/core/compiler/fingerprint.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1785,6 +1785,13 @@ where
17851785

17861786
for path in paths {
17871787
let path = path.as_ref();
1788+
1789+
// Assuming anything in cargo_home is immutable (see also #9455 about marking it readonly)
1790+
// which avoids rebuilds when CI caches $CARGO_HOME/registry/{index, cache} and
1791+
// $CARGO_HOME/git/db across runs, keeping the content the same but changing the mtime.
1792+
if let Ok(true) = home::cargo_home().map(|home| path.starts_with(home)) {
1793+
continue;
1794+
}
17881795
let path_mtime = match mtime_cache.entry(path.to_path_buf()) {
17891796
Entry::Occupied(o) => *o.get(),
17901797
Entry::Vacant(v) => {

tests/testsuite/build_script.rs

+79
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Tests for build.rs scripts.
22
33
use cargo_test_support::compare::assert_match_exact;
4+
use cargo_test_support::install::cargo_home;
45
use cargo_test_support::paths::CargoPathExt;
56
use cargo_test_support::registry::Package;
67
use cargo_test_support::tools;
@@ -4803,6 +4804,84 @@ fn rerun_if_directory() {
48034804
fresh();
48044805
}
48054806

4807+
#[cargo_test]
4808+
fn rerun_if_published_directory() {
4809+
// build script of a dependency contains a `rerun-if-changed` pointing to a directory
4810+
Package::new("mylib-sys", "1.0.0")
4811+
.file("mylib/balrog.c", "")
4812+
.file("src/lib.rs", "")
4813+
.file(
4814+
"build.rs",
4815+
r#"
4816+
fn main() {
4817+
// Changing to mylib/balrog.c will not trigger a rebuild
4818+
println!("cargo:rerun-if-changed=mylib");
4819+
}
4820+
"#,
4821+
)
4822+
.publish();
4823+
4824+
let p = project()
4825+
.file(
4826+
"Cargo.toml",
4827+
r#"
4828+
[package]
4829+
name = "foo"
4830+
version = "0.0.1"
4831+
4832+
[dependencies]
4833+
mylib-sys = "1.0.0"
4834+
"#,
4835+
)
4836+
.file("src/main.rs", "fn main() {}")
4837+
.build();
4838+
4839+
p.cargo("check").run();
4840+
4841+
// Delete regitry src to make directories being recreated with the latest timestamp.
4842+
cargo_home().join("registry/src").rm_rf();
4843+
4844+
p.cargo("check --verbose")
4845+
.with_stderr(
4846+
"\
4847+
[FRESH] mylib-sys v1.0.0
4848+
[FRESH] foo v0.0.1 ([CWD])
4849+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
4850+
",
4851+
)
4852+
.run();
4853+
4854+
// Upgrade of a package should still trigger a rebuild
4855+
Package::new("mylib-sys", "1.0.1")
4856+
.file("mylib/balrog.c", "")
4857+
.file("mylib/balrog.h", "")
4858+
.file("src/lib.rs", "")
4859+
.file(
4860+
"build.rs",
4861+
r#"
4862+
fn main() {
4863+
println!("cargo:rerun-if-changed=mylib");
4864+
}
4865+
"#,
4866+
)
4867+
.publish();
4868+
p.cargo("update").run();
4869+
p.cargo("fetch").run();
4870+
4871+
p.cargo("check -v")
4872+
.with_stderr(format!(
4873+
"\
4874+
[COMPILING] mylib-sys [..]
4875+
[RUNNING] `rustc --crate-name build_script_build [..]
4876+
[RUNNING] `[..]build-script-build[..]`
4877+
[RUNNING] `rustc --crate-name mylib_sys [..]
4878+
[CHECKING] foo [..]
4879+
[RUNNING] `rustc --crate-name foo [..]
4880+
[FINISHED] [..]",
4881+
))
4882+
.run();
4883+
}
4884+
48064885
#[cargo_test]
48074886
fn test_with_dep_metadata() {
48084887
let p = project()

0 commit comments

Comments
 (0)