Skip to content

Commit

Permalink
Add more metadata to rustc_fingerprint
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cuviper committed Oct 31, 2024
1 parent 497c228 commit b5acf4c
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/cargo/util/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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(())
};

Expand Down

0 comments on commit b5acf4c

Please sign in to comment.