From 839069f03fc9775ec16a119f3d6b21be1c9facf3 Mon Sep 17 00:00:00 2001 From: Joakim Holm Date: Mon, 17 Jul 2023 23:45:20 +0200 Subject: [PATCH] only skip mtime check on $CARGO_HOME/{git,registry} Closes #12090 Commit Fix formatting Fix tests Fix formatting Fix formatting Fix formatting --- src/cargo/core/compiler/fingerprint/mod.rs | 23 ++++++-- tests/testsuite/freshness.rs | 62 +++++++++++++++++++++- 2 files changed, 79 insertions(+), 6 deletions(-) diff --git a/src/cargo/core/compiler/fingerprint/mod.rs b/src/cargo/core/compiler/fingerprint/mod.rs index aa8be50f758..7bb852e79e5 100644 --- a/src/cargo/core/compiler/fingerprint/mod.rs +++ b/src/cargo/core/compiler/fingerprint/mod.rs @@ -1857,14 +1857,27 @@ where Err(..) => return Some(StaleItem::MissingFile(reference.to_path_buf())), }; + let skipable_dirs = if let Ok(cargo_home) = home::cargo_home() { + let skipable_dirs: Vec<_> = ["git", "registry"] + .into_iter() + .map(|subfolder| cargo_home.join(subfolder)) + .collect(); + Some(skipable_dirs) + } else { + None + }; + for path in paths { let path = path.as_ref(); - // Assuming anything in cargo_home is immutable (see also #9455 about marking it readonly) - // which avoids rebuilds when CI caches $CARGO_HOME/registry/{index, cache} and - // $CARGO_HOME/git/db across runs, keeping the content the same but changing the mtime. - if let Ok(true) = home::cargo_home().map(|home| path.starts_with(home)) { - continue; + // Assuming anything in cargo_home/{git, registry} is immutable + // (see also #9455 about marking the src directory readonly) which avoids rebuilds when CI + // caches $CARGO_HOME/registry/{index, cache} and $CARGO_HOME/git/db across runs, keeping + // the content the same but changing the mtime. + if let Some(ref skipable_dirs) = skipable_dirs { + if skipable_dirs.iter().any(|dir| path.starts_with(dir)) { + continue; + } } let path_mtime = match mtime_cache.entry(path.to_path_buf()) { Entry::Occupied(o) => *o.get(), diff --git a/tests/testsuite/freshness.rs b/tests/testsuite/freshness.rs index 86b186af847..f28f1ff4686 100644 --- a/tests/testsuite/freshness.rs +++ b/tests/testsuite/freshness.rs @@ -14,7 +14,8 @@ use super::death; use cargo_test_support::paths::{self, CargoPathExt}; use cargo_test_support::registry::Package; use cargo_test_support::{ - basic_manifest, is_coarse_mtime, project, rustc_host, rustc_host_env, sleep_ms, + basic_lib_manifest, basic_manifest, is_coarse_mtime, project, rustc_host, rustc_host_env, + sleep_ms, }; #[cargo_test] @@ -2814,3 +2815,62 @@ directory sources are not [..] ) .run(); } + +#[cargo_test] +fn skip_mtime_check_in_selected_cargo_home_subdirs() { + let p = project() + .at("cargo_home/registry/foo") + .file("Cargo.toml", &basic_lib_manifest("foo")) + .file("src/lib.rs", "") + .build(); + let project_root = p.root(); + let cargo_home = project_root.parent().unwrap().parent().unwrap(); + p.cargo("check -v") + .env("CARGO_HOME", &cargo_home) + .with_stderr( + "\ +[CHECKING] foo v0.5.0 ([CWD]) +[RUNNING] `rustc --crate-name foo src/lib.rs [..] +[FINISHED] dev [..]", + ) + .run(); + p.change_file("src/lib.rs", "illegal syntax"); + p.cargo("check -v") + .env("CARGO_HOME", &cargo_home) + .with_stderr( + "\ +[FRESH] foo v0.5.0 ([CWD]) +[FINISHED] dev [..]", + ) + .run(); +} + +#[cargo_test] +fn use_mtime_cache_in_cargo_home() { + let p = project() + .at("cargo_home/foo") + .file("Cargo.toml", &basic_lib_manifest("foo")) + .file("src/lib.rs", "") + .build(); + let project_root = p.root(); + let cargo_home = project_root.parent().unwrap(); + p.cargo("check -v") + .env("CARGO_HOME", &cargo_home) + .with_stderr( + "\ +[CHECKING] foo v0.5.0 ([CWD]) +[RUNNING] `rustc --crate-name foo src/lib.rs [..] +[FINISHED] dev [..]", + ) + .run(); + p.change_file("src/lib.rs", "illegal syntax"); + p.cargo("check -v") + .env("CARGO_HOME", &cargo_home) + .with_stderr( + "\ +[DIRTY] foo v0.5.0 ([CWD]): [..] +[CHECKING] foo v0.5.0 ([CWD]) +[RUNNING] `rustc --crate-name foo src/lib.rs [..]", + ) + .run_expect_error(); +}