From b5acf4ce4763ff90fd8bd92494490184a37fa24e Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Thu, 31 Oct 2024 15:10:54 -0700 Subject: [PATCH] Add more metadata to `rustc_fingerprint` Previously, `rustc_fingerprint` was only using the path and mtime for each executable, but this is not always sufficient. For example, Fedora will [clamp mtimes] to help reproducible builds, so Rust 1.82 looks like `/usr/bin/rustc` and `2024-10-17 00:00:00` across all builds in Fedora 39 through 42 (rawhide), even though they are different in their full version strings, LLVM, etc. [clamp mtimes]: https://fedoraproject.org/wiki/Changes/ReproducibleBuildsClampMtimes If the target directory (including `.rustc_info.json`) is shared across such systems, the fingerprint wouldn't notice the difference, and cargo would try to use artifacts that aren't actually compatible, like: ``` error[E0514]: found crate `autocfg` compiled by an incompatible version of rustc --> build.rs:2:14 | 2 | let ac = autocfg::new(); | ^^^^^^^ | = note: the following crate versions were found: crate `autocfg` compiled by rustc 1.82.0 (f6e511eec 2024-10-15) (Fedora 1.82.0-1.fc42): [...]/target/debug/deps/libautocfg-589c41db1eea6297.rlib = help: please recompile that crate using this compiler (rustc 1.82.0 (f6e511eec 2024-10-15) (Fedora 1.82.0-1.fc40)) (consider running `cargo clean` first) ``` We can improve this situation by adding the file length, although that could also happen to be the same, and the creation date that will match when the file was installed, though not all filesystems support that. All of this comes from a single metadata call, so it shouldn't have any noticeable slowdown that would hurt the caching effort. --- src/cargo/util/rustc.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/cargo/util/rustc.rs b/src/cargo/util/rustc.rs index 2b80136dc05..c9a5ee82109 100644 --- a/src/cargo/util/rustc.rs +++ b/src/cargo/util/rustc.rs @@ -6,6 +6,7 @@ use std::sync::Mutex; use anyhow::Context as _; use cargo_util::{paths, ProcessBuilder, ProcessError}; +use filetime::FileTime; use serde::{Deserialize, Serialize}; use tracing::{debug, info, warn}; @@ -329,7 +330,13 @@ fn rustc_fingerprint( let path = paths::resolve_executable(path)?; path.hash(hasher); - paths::mtime(&path)?.hash(hasher); + let meta = paths::metadata(&path)?; + meta.len().hash(hasher); + + // Often created and modified are the same, but not all filesystems support the former, + // and distro reproducible builds may clamp the latter, so we try to use both. + FileTime::from_creation_time(&meta).hash(hasher); + FileTime::from_last_modification_time(&meta).hash(hasher); Ok(()) };