|
1 | 1 | +++ |
2 | 2 | title = "Tips For Faster Rust Compile Times" |
3 | 3 | date = 2024-01-12 |
4 | | -updated = 2025-07-01 |
| 4 | +updated = 2025-09-03 |
5 | 5 | draft = false |
6 | 6 | template = "article.html" |
7 | 7 | [extra] |
@@ -44,6 +44,7 @@ Click here to expand the table of contents. |
44 | 44 | - [Split Big Crates Into Smaller Ones Using Workspaces](#split-big-crates-into-smaller-ones-using-workspaces) |
45 | 45 | - [Disable Unused Features Of Crate Dependencies](#disable-unused-features-of-crate-dependencies) |
46 | 46 | - [Add Features For Expensive Code](#add-features-for-expensive-code) |
| 47 | + - [Find The Root Cause For Rebuilds](#find-the-root-cause-for-rebuilds) |
47 | 48 | - [Cache Dependencies With sccache](#cache-dependencies-with-sccache) |
48 | 49 | - [Cranelift: The Alternative Rust Compiler](#cranelift-the-alternative-rust-compiler) |
49 | 50 | - [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 |
336 | 337 | in your `Cargo.toml` enables JSON support while the `complex_feature` feature |
337 | 338 | enables another expensive code path. |
338 | 339 |
|
| 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 | + |
339 | 387 | ### Cache Dependencies With sccache |
340 | 388 |
|
341 | 389 | Another neat project is [sccache](https://github.com/mozilla/sccache) by |
|
0 commit comments