Skip to content

Commit 324bedd

Browse files
committed
Add tip about cargo::core::compile::fingerprint
1 parent 2ddb1eb commit 324bedd

1 file changed

Lines changed: 49 additions & 1 deletion

File tree

  • content/blog/tips-for-faster-rust-compile-times

‎content/blog/tips-for-faster-rust-compile-times/index.md‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
+++
22
title = "Tips For Faster Rust Compile Times"
33
date = 2024-01-12
4-
updated = 2025-07-01
4+
updated = 2025-09-03
55
draft = false
66
template = "article.html"
77
[extra]
@@ -44,6 +44,7 @@ Click here to expand the table of contents.
4444
- [Split Big Crates Into Smaller Ones Using Workspaces](#split-big-crates-into-smaller-ones-using-workspaces)
4545
- [Disable Unused Features Of Crate Dependencies](#disable-unused-features-of-crate-dependencies)
4646
- [Add Features For Expensive Code](#add-features-for-expensive-code)
47+
- [Find The Root Cause For Rebuilds](#find-the-root-cause-for-rebuilds)
4748
- [Cache Dependencies With sccache](#cache-dependencies-with-sccache)
4849
- [Cranelift: The Alternative Rust Compiler](#cranelift-the-alternative-rust-compiler)
4950
- [Switch To A Faster Linker](#switch-to-a-faster-linker)
@@ -336,6 +337,53 @@ You can do the same in your code. In the above example, the `json` feature
336337
in your `Cargo.toml` enables JSON support while the `complex_feature` feature
337338
enables another expensive code path.
338339

340+
### Find The Root Cause For Rebuilds
341+
342+
Sometimes many crates rebuild even when no code has changed, often due to environment variable differences between build processes (like your Makefile builds vs rust-analyzer vs CI builds).
343+
344+
Use cargo's fingerprint logging to identify exactly why rebuilds are triggered:
345+
346+
```bash
347+
export CARGO_LOG="cargo::core::compiler::fingerprint=info"
348+
export RUST_LOG=trace
349+
cargo build -vv
350+
```
351+
352+
Look for lines showing what caused the rebuild.
353+
For example:
354+
355+
```
356+
INFO prepare_target: cargo::core::compiler::fingerprint: dirty: EnvVarChanged { name: "VIRTUAL_ENV", old_value: None, new_value: Some("/path/to/.venv") }
357+
358+
Dirty pyo3-build-config v0.24.1: the env variable VIRTUAL_ENV changed
359+
```
360+
361+
See that "Dirty" line? It tells you exactly what caused the rebuild.
362+
You can grep for that line to find all rebuild causes.
363+
364+
Common culprits include:
365+
- Environment variables (CC, CXX, VIRTUAL_ENV, PATH changes)
366+
- Feature flag mismatches between different build tools
367+
- Different cargo profiles being used
368+
- Timestamp differences in generated files
369+
370+
Once you identify the cause, ensure consistency across all your build processes. For rust-analyzer in VS Code, you can configure matching environment variables in `.vscode/settings.json`:
371+
372+
```json
373+
{
374+
"rust-analyzer.check.extraEnv": {
375+
"CC": "clang",
376+
"CXX": "clang++",
377+
"VIRTUAL_ENV": "/path/to/your/.venv"
378+
},
379+
"rust-analyzer.cargo.features": "all"
380+
}
381+
```
382+
383+
This debugging approach can dramatically reduce unnecessary rebuilds when different tools use inconsistent environments.
384+
385+
Credit: This technique was mentioned in [this write-up](https://github.com/twitu/twitu/blob/54a8915eac80562a15824988bac629583e6befd1/fixing-frequent-full-rust-builds-with-cargo-fingerprints.md) by [Ishan Bhanuka](https://github.com/twitu).
386+
339387
### Cache Dependencies With sccache
340388

341389
Another neat project is [sccache](https://github.com/mozilla/sccache) by

0 commit comments

Comments
 (0)