You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Move build-dir out of the workspace to cargo-cache-home by default #16147
As a follow up to #14125, we've talked about changing the default for build-dir after a sufficient time of it being stabilized so people have a chance to test their builds and tools with it to prepare.
I may be being dumb, so apologies, but it is unclear whether this is for cargo script or cargo as a whole. If it is only for cargo script clearly the following does not apply.
Someone will want to at minimum call out very loudly that those of us who are moving our projects to non-system drives need to tweak this if it is changed. Specifically on Windows it is not uncommon to reserve C for the system/installed programs and to do work on other drives. In most programming languages (C++, etc) it is sufficient to just move the repository because the intermediate artifacts are all relative to it.
I also suspect that a lot of people probably do CI setups where they assume their repo is the only thing that needs to be performant, because again that's a pretty normal-ish thing. I know we did at one of my previous jobs.
If I was a new user trying to optimize my builds and my system, this would be a nasty surprise. SO would potentially large directories on a system drive.
I think changing the default is fine, I'm just highlighting that it's a tad unusual and surprising and that use cases which assume that things are under the repository are probably pretty common. Unlike many other changes this doesn't error or anything: it just mysteriously gets slow, and if you are sufficiently unlucky, takes out the system.
This proposed change is for all workspaces; cargo scripts already does this.
In a way, this is to help with those situations. Especially with #1734, all cargo caches would be movable with one environment variable to whatever drive works best for them.
I'm pointing out that this creates a "once you know, you know" situation. I would be surprised by it if I was in the shoes of a new user coming from either C/C++ or Python. It seems worth it to me to at least voice it, especially since mostly the people who would be affected aren't here. I'm neutral on the actual setting: if you don't change it I'm fine, if you do then I can (presumably) reconfigure it to point at the right drive.
changed the title [-]Move `build-dir` out of the workspace to `cache-cache-home` by default[/-][+]Move `build-dir` out of the workspace to `cargo-cache-home` by default[/+]on Nov 30, 2025
From my perspective I think moving build-dir of the workspace's target dir by default might actually help newcomers to Rust.
Currently there is a lot of "stuff" in target which could be a overwhelming to a newcomer that just wants their program they compiled. When they are already busy trying to learn the language this noise can add additional cognitive load.
Moving build-dir to a different location would greatly reduce the amount of files that are added to target.
Am I reading correctly that, unlike #15010, this does change the path of final artifacts? That’s going to break a lot of code, and force a bunch of logic that’s currently static to become dynamic. It’s not clear to me what anyone is getting out of it.
This changes intermediate artifacts only (build-dir). Final artifacts (target-dir) are left alone. Final artifacts are part of our stability guarantees which is causing frustration with other efforts :).
Another motivation for moving the default build-dir location is fine grain locking (#4282)
Currently in Cargo we lock on .cargo-lock in <root>/[<platform>]/<profile> in both build-dir and artifact-dir.
When -Zfine-grain-locking is enabled, for cargo build we take an exclusive lock on artifact-dir and shared lock on build-dir.
The shared lock on build-dir allows concurrent cargo build and cargo check.
However, when build-dir and artifact-dir are the same (which is the default <workspace-root>/target) the locking file is the same, thus we are forced to be conservative and take an exclusive lock.
While there are other ways to solve this, the simplest solution (and the one I'd like to see) is to simply change the default build-dir location.
For fine grain locking, we split out the build-dir lock into a dedicate file (.cargo-build-lock) in #16708 so it is no longer blocked by moving this issue.
Though I still would like to see this issue happen eventually :D
Some numbers from a setup that has been doing this a while, in case they are useful for the "what should the new default be" question.
Mine keys target-dir per git worktree, not build-dir per workspace, so it is not the same thing. The shape matches though: one global root, one hashed subdir per checkout. Currently 51 directories, 179 GiB.
On the mnemonic. My keys are <checkout-dir-name>-<sha256(abs_path)[:12]>. The prefix costs 10-25 characters on top of a path that already carries a 12-char hash and a cache root, and I have not hit a length limit on Linux or Windows. It means du -sh * on the root is readable. One workspace holds 104 GiB of my 179 GiB; with bare hashes I would see that number and have no idea whose it was.
On lifecycle. Once build-dir leaves the workspace, deleting a checkout strands its build-dir, and cargo clean has no workspace left to run from. The RFC cross-linked above (heiervang-technologies/clanker-code#74) runs into this and proposes deleting a hash directory once its owning worktree is confirmed gone, discovered by walking git worktree list --porcelain.
That confirmation step is the weak part, and I have a number for it. With a one-way hash the only method available is to re-hash every live checkout and diff against the store. Doing that on my machine flagged 9 directories. Two were wrong: live checkouts outside the tree I had picked to scan, one nested deeper than my search went, one under ~/Documents. Real count was 6, holding 7.1 GiB.
22% false positives, on a scheme where I wrote the key derivation myself. Enumeration only finds checkouts you already know to ask about, which is how both of us ended up guessing at a scan root.
A file inside each build-dir naming the source manifest path would turn that into a lookup. The layout is already documented as internal and subject to change, so there is room for one.
Activity
build(deps): update rust to v1.91.0
rust191: add a package for rust 1.91.0.
ahicks92 commentedon Nov 25, 2025
I may be being dumb, so apologies, but it is unclear whether this is for cargo script or cargo as a whole. If it is only for cargo script clearly the following does not apply.
Someone will want to at minimum call out very loudly that those of us who are moving our projects to non-system drives need to tweak this if it is changed. Specifically on Windows it is not uncommon to reserve C for the system/installed programs and to do work on other drives. In most programming languages (C++, etc) it is sufficient to just move the repository because the intermediate artifacts are all relative to it.
I also suspect that a lot of people probably do CI setups where they assume their repo is the only thing that needs to be performant, because again that's a pretty normal-ish thing. I know we did at one of my previous jobs.
If I was a new user trying to optimize my builds and my system, this would be a nasty surprise. SO would potentially large directories on a system drive.
I think changing the default is fine, I'm just highlighting that it's a tad unusual and surprising and that use cases which assume that things are under the repository are probably pretty common. Unlike many other changes this doesn't error or anything: it just mysteriously gets slow, and if you are sufficiently unlucky, takes out the system.
epage commentedon Nov 25, 2025
This proposed change is for all workspaces; cargo scripts already does this.
In a way, this is to help with those situations. Especially with #1734, all cargo caches would be movable with one environment variable to whatever drive works best for them.
ahicks92 commentedon Nov 25, 2025
I'm pointing out that this creates a "once you know, you know" situation. I would be surprised by it if I was in the shoes of a new user coming from either C/C++ or Python. It seems worth it to me to at least voice it, especially since mostly the people who would be affected aren't here. I'm neutral on the actual setting: if you don't change it I'm fine, if you do then I can (presumably) reconfigure it to point at the right drive.
T-256 commentedon Nov 30, 2025
[-]Move `build-dir` out of the workspace to `cache-cache-home` by default[/-][+]Move `build-dir` out of the workspace to `cargo-cache-home` by default[/+]ranger-ross commentedon Dec 4, 2025
From my perspective I think moving
build-dirof the workspace'stargetdir by default might actually help newcomers to Rust.Currently there is a lot of "stuff" in
targetwhich could be a overwhelming to a newcomer that just wants their program they compiled. When they are already busy trying to learn the language this noise can add additional cognitive load.Moving
build-dirto a different location would greatly reduce the amount of files that are added totarget.HerringtonDarkholme commentedon Dec 18, 2025
Hi, if your project is affected by the deprecation of
you can apply the following codemod with ast-grep (which itself is impacted as well):
Remember to import the
cargo_binmacro into scope.For a concrete example of this change, see: ast-grep/ast-grep#2399
jyn514 commentedon Dec 18, 2025
Am I reading correctly that, unlike #15010, this does change the path of final artifacts? That’s going to break a lot of code, and force a bunch of logic that’s currently static to become dynamic. It’s not clear to me what anyone is getting out of it.
epage commentedon Dec 18, 2025
This changes intermediate artifacts only (
build-dir). Final artifacts (target-dir) are left alone. Final artifacts are part of our stability guarantees which is causing frustration with other efforts :).refactor(test): migrate from cargo_bin to escargot
fix: migrate tests from cargo_bin to escargot (#49)
Replace deprecated cargo_bin with cargo_bin! macro (#579)
epage commentedon Jan 20, 2026
#13663 is likely a blocker for this as there wouldn't be a way to workaround the lack of it anymore, like what is done in Rust-SDL2/rust-sdl2#1520
ranger-ross commentedon Mar 1, 2026
Another motivation for moving the default
build-dirlocation is fine grain locking (#4282)Currently in Cargo we lock on
.cargo-lockin<root>/[<platform>]/<profile>in bothbuild-dirandartifact-dir.When
-Zfine-grain-lockingis enabled, forcargo buildwe take an exclusive lock onartifact-dirand shared lock onbuild-dir.The shared lock on
build-dirallows concurrentcargo buildandcargo check.However, when
build-dirandartifact-dirare the same (which is the default<workspace-root>/target) the locking file is the same, thus we are forced to be conservative and take an exclusive lock.While there are other ways to solve this, the simplest solution (and the one I'd like to see) is to simply change the default
build-dirlocation.epage commentedon Mar 2, 2026
I think the simplest is for us to use separate locks which might also simplify things.
ranger-ross commentedon Mar 6, 2026
For fine grain locking, we split out the
build-dirlock into a dedicate file (.cargo-build-lock) in #16708 so it is no longer blocked by moving this issue.Though I still would like to see this issue happen eventually :D
fix: use robust binary lookup in CLI tests
fix: use assert_cmd cargo_bin_cmd! macro in tests (#101) (#115)
CheckPickerUpper commentedon Aug 24, 2026
Some numbers from a setup that has been doing this a while, in case they are useful for the "what should the new default be" question.
Mine keys
target-dirper git worktree, notbuild-dirper workspace, so it is not the same thing. The shape matches though: one global root, one hashed subdir per checkout. Currently 51 directories, 179 GiB.On the mnemonic. My keys are
<checkout-dir-name>-<sha256(abs_path)[:12]>. The prefix costs 10-25 characters on top of a path that already carries a 12-char hash and a cache root, and I have not hit a length limit on Linux or Windows. It meansdu -sh *on the root is readable. One workspace holds 104 GiB of my 179 GiB; with bare hashes I would see that number and have no idea whose it was.On lifecycle. Once build-dir leaves the workspace, deleting a checkout strands its build-dir, and
cargo cleanhas no workspace left to run from. The RFC cross-linked above (heiervang-technologies/clanker-code#74) runs into this and proposes deleting a hash directory once its owning worktree is confirmed gone, discovered by walkinggit worktree list --porcelain.That confirmation step is the weak part, and I have a number for it. With a one-way hash the only method available is to re-hash every live checkout and diff against the store. Doing that on my machine flagged 9 directories. Two were wrong: live checkouts outside the tree I had picked to scan, one nested deeper than my search went, one under
~/Documents. Real count was 6, holding 7.1 GiB.22% false positives, on a scheme where I wrote the key derivation myself. Enumeration only finds checkouts you already know to ask about, which is how both of us ended up guessing at a scan root.
A file inside each build-dir naming the source manifest path would turn that into a lookup. The layout is already documented as internal and subject to change, so there is room for one.
epage commentedon Aug 24, 2026
We have #13136 for gc-ing build-dirs that are no longer in use.
stdbuf: build.rs: give the nested cargo build its own build-dir