dcreager.net

Shared target directory

2024-01-30

Rust stores all compiler outputs (including compiled dependencies and intermediate files) in the “target” directory. By default, each workspace (typically, the cloned repo of a collection of related crates) has its own target directory, called target and stored as a direct child of the workspace root. (This is why you typically see ‘/target/’ as an entry in a Rust repo's .gitignore file.)

Target directories can get very large, because there is no garbage collection applied to it by default. This can become especially problematic if you work on several workspaces that have shared dependencies, since each target directory will include its own copy of those dependencies.

There is a config option that lets you use a shared target directory instead. Put this in your ‘~/.cargo/config.toml’ file:

[build]
target-dir = ".cache/cargo/target"

(Note that relative paths are relative to the parent of the directory containing the config file — i.e., your home directory for your global ‘~/.cargo/config.toml’.)

Build cache [Cargo book]

‘build.target-dir’ [Cargo book]

Languages » Rust