Why rust does not use global target dir?

Does rust enable a global build cache just like golang?


I know the sccache would use global cache for compiling. But the build output are still inside the project target/ dir.

I also know that we can configure cargo to enable global target-dir. But how it handles project conflicts? For example, two different projects but with the same name; Same dependency lib with the different feature settings.

What's stopping you from adding export CARGO_TARGET_DIR=/tmp/ to your bashrc?

The compiler will handle multiple versions of the same crate just fine because their artifacts are placed in different locations, but using different inputs (RUSTFLAGS, cargo features, etc.) for the same version of the same crate will trigger a recompile for that crate.


config target-dir works the same as CARGO_TARGET_DIR, right?

I mean the different compile because of different compile flags would be no conflict inside the global target dir?

BTW, since the sccache contains build outputs, why the cargo build still place its build outputs to its own target dir?

This is because Cargo conflates the concepts of final build products (like executables and crate's own libraries) and intermediate build artifacts (dependencies, temp files) and uses a single directory for all of them.

Because Cargo places project-specific build products without any namespacing in the in target/ dir, having literally a single global target dir would make different projects overwrite each other's executables. This is already an issue for workspaces.


How about sccache? Since it could improve compilation speed via a central cache, why cargo still place dep objs in target/ dir per project?

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.