Setting global `CARGO_TARGET_DIR` still results in the crate being built multiple times

⚓ Rust    📅 2025-09-11    👤 surdeus    👁️ 10      

surdeus

Warning

This post was published 62 days ago. The information described in this article may have changed.

I have multiple workspaces, one for the crate and others for examples which use that crate.

I tried speeding up the CI by setting CARGO_TARGET_DIR to a common folder, so that the main crate is not rebuilt multiple times. However, while it is now build only once for examples, it is still being build the second time when the crate itself is being built and I don't understand why.

Essentially I have this structure:

+---dependency
|   |   Cargo.toml
|   \---src
|           lib.rs
|
\---user
    |   Cargo.toml # `dependency = { path = "../depencency" }` 
    \---src
            main.rs

Then I set CARGO_TARGET_DIR to a common folder like C:/temp and then run cargo build in both crates, which results in dependency being built twice. Why?

As a note, I know that putting the library and binary inside a single workspace will solve the issue, but I cannot do this in my case.

2 posts - 2 participants

Read full topic

🏷️ Rust_feed