Skip to content

fix: only skip mtime check on ~/.cargo/{git,registry} #12369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/cargo/core/compiler/fingerprint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
62 changes: 61 additions & 1 deletion tests/testsuite/freshness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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();
}