Skip to content

C0040 build-script-clock

Rule: Build.rs reads no clock and resolves git through the real git dir.

Code C0040 (stable, never reused)
Id build-script-clock
Category code
Source spirit_level
Fix by hand

Level per tier

Tier Level Reason
small warn a clock read makes the build unreproducible; a hardcoded .git path breaks in a worktree checkout
small-workspace warn a clock read makes the build unreproducible; a hardcoded .git path breaks in a worktree checkout
medium warn a clock read makes the build unreproducible; a hardcoded .git path breaks in a worktree checkout
big warn a clock read makes the build unreproducible; a hardcoded .git path breaks in a worktree checkout
ai off
embedded off
ui off
egui off
units-strict off
units-paranoid off

Parameters

Name Type Default Meaning
clock-functions list of strings ["SystemTime::now", "Utc::now", "Local::now"] qualified Type::method calls that read the wall clock
git-dir-evidence list of strings ["git-dir", "git-common-dir"] substrings anywhere in the build script that show it asked git for the real git dir first

Why

A build.rs that reads the wall clock (SystemTime::now(), chrono::Utc::now(), chrono::Local::now()) bakes a different value into every build, so cargo never sees a cached build and nothing downstream can reproduce it from the same source. A build.rs that assumes .git is a directory breaks the moment it is built from a git worktree, where .git is a file pointing at the real git dir elsewhere (.git/worktrees/<name>); the script either fails to find HEAD or watches the wrong file, and cargo:rerun-if-changed stops catching new commits.

Example

// bad: reads the clock, and assumes .git is the real git dir
let now = std::time::SystemTime::now();
println!("cargo:rerun-if-changed=.git/HEAD");
println!("cargo:rerun-if-changed=.git/index");
// good: asks git for the real dir, which works in a worktree too
let git_dir = std::process::Command::new("git")
    .args(["rev-parse", "--git-dir"])
    .output()
    .ok()
    .filter(|o| o.status.success())
    .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_owned());
if let Some(dir) = git_dir {
    println!("cargo:rerun-if-changed={dir}/HEAD");
    println!("cargo:rerun-if-changed={dir}/index");
}

How to fix

Drop the clock read; a build time nobody can reproduce is worse than none. Read the git commit time instead (through the same git rev-parse --git-dir call), or leave the field unset until something that wants a timestamp asks for one at run time.

Resolve the git dir with git rev-parse --git-dir, run from the build script's own directory (CARGO_MANIFEST_DIR), and watch HEAD and index under that resolved path. The lint is an estimate: it looks for a .git path component and flags it unless the file shows evidence (git-dir, git-common-dir) that it asked git first.

Configure

[lints.build-script-clock]
level = "deny"
clock-functions = ["SystemTime::now", "Utc::now", "Local::now"]
git-dir-evidence = ["git-dir", "git-common-dir"]
reason = "why this project wants it at this level"

Allow one finding in the code with // spirit-level: allow(build-script-clock) <reason> (waivers); the code works there too, as in [lints.C0040].