Sub topic 12
sccache and shared target directories
This chapter assumes you can read .cargo/config.toml, know the difference between dependency crates and your own workspace crates, and have read the earlier chapters on incremental compilation and profiles.
Cargo already reuses work inside one project's target directory. Two situations defeat that reuse. The first is when you build many projects that share dependencies. The second is when you run cargo clean, switch branches, or build on a fresh CI machine. A compiler cache and a shared target directory address these cases.
How sccache works
sccache is a wrapper around rustc. Cargo calls the wrapper instead of the compiler. sccache hashes the inputs to each compiler call: the source files, the compiler version, the flags, and the relevant environment variables. If it has seen that hash before, it returns the stored output instead of compiling again.
Install it and tell Cargo to use it:
cargo install sccache --locked
export RUSTC_WRAPPER=sccache
cargo build
sccache --show-statsTo make this permanent for your user or a project, put it in the Cargo configuration instead:
# .cargo/config.toml
[build]
rustc-wrapper = "sccache"The --show-stats output reports cache hits, misses, and non-cacheable calls. Run it after a build, then run cargo clean and build again. The second build should show mostly hits.
What sccache cannot cache
- Incremental builds. sccache does not cache crates compiled with incremental compilation, because the incremental state lives in the
targetdirectory. Dev builds enable incremental compilation by default, so your own workspace crates are mostly "non-cacheable". Dependencies are not built incrementally, so they are cached. If you want your own crates cached too, setCARGO_INCREMENTAL=0, and accept slower edit-compile cycles. - Linking and proc-macro crates. Final binaries and some crate types are not cached. The link step still runs every time.
- Path-sensitive builds. Absolute paths can end up in the hash. The same project checked out in two different directories can miss the cache. Adding
--remap-path-prefixto your flags helps, but only with consistent settings everywhere.
Choosing where the cache lives
By default sccache stores data in a local directory. Set SCCACHE_DIR to move it and SCCACHE_CACHE_SIZE to cap it. Teams can instead point sccache at shared storage, such as an S3 bucket, so that developers and CI reuse each other's results. That requires credentials and a decision about who may write to the cache. Treat a writable shared cache as trusted infrastructure, since its contents end up in your binaries.
Sharing one target directory
Cargo can also place build output outside the project:
# ~/.cargo/config.toml
[build]
target-dir = "/home/me/.cache/rust-target"Setting CARGO_TARGET_DIR has the same effect. Projects that share the directory can reuse identical dependency builds, provided the versions, features, profile, and compiler match.
The costs are real:
- Cargo locks the directory, so two builds in different projects run one after the other.
- Different feature sets or versions of the same crate overwrite or add to each other's artifacts, so the directory grows and can rebuild more than you expect.
cargo cleanin any project removes everything for all of them.- Tools that expect
./target/for binaries need the new path.
| Approach | Best for | Main drawback |
|---|---|---|
| sccache | Clean builds, CI, many checkouts | Skips incremental crates and linking |
| Shared target dir | Many local projects with the same dependencies | Lock contention and mixed artifacts |
You can combine both. Measure with cargo build --timings and sccache --show-stats before keeping either.
Exercise
After enabling sccache, your dev builds show many "non-cacheable" calls for your own workspace crates, while dependencies are cached. Why, and what would change it?
Show answer
Dev builds use incremental compilation by default, and sccache cannot cache incremental crates. Dependencies are not built incrementally, so they are cached. Setting CARGO_INCREMENTAL=0 makes your own crates cacheable, but it slows the edit-compile loop, so it is usually only worth doing in CI.