Skip to content

Move build-dir out of the workspace to cargo-cache-home by default #16147

Open

Description

@epage

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.

Blockers:

Open questions:

Activity

  1. added
    A-cachingArea: caching of dependencies, repositories, and build artifacts
    on Oct 24, 2025
  2. added a commit that references this issue on Nov 4, 2025
  3. ahicks92 commented on Nov 25, 2025

    @ahicks92

    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.

  4. epage commented on Nov 25, 2025

    @epage
    ContributorAuthor

    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.

  5. ahicks92 commented on Nov 25, 2025

    @ahicks92

    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.

  6. T-256 commented on Nov 30, 2025

    @T-256
  7. 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
  8. ranger-ross commented on Dec 4, 2025

    @ranger-ross
    Member

    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.

  9. HerringtonDarkholme commented on Dec 18, 2025

    @HerringtonDarkholme

    Hi, if your project is affected by the deprecation of

    Command::cargo_bin("your-cmd")?

    you can apply the following codemod with ast-grep (which itself is impacted as well):

    ast-grep -p 'Command::cargo_bin($A)?' -r 'Command::new(cargo_bin!())' -U

    Remember to import the cargo_bin macro into scope.

    For a concrete example of this change, see: ast-grep/ast-grep#2399

  10. jyn514 commented on Dec 18, 2025

    @jyn514
    Member

    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.

  11. epage commented on Dec 18, 2025

    @epage
    ContributorAuthor

    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 :).

  12. epage commented on Jan 20, 2026

    @epage
    ContributorAuthor

    #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

  13. ranger-ross commented on Mar 1, 2026

    @ranger-ross
    Member

    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.

  14. epage commented on Mar 2, 2026

    @epage
    ContributorAuthor

    I think the simplest is for us to use separate locks which might also simplify things.

  15. ranger-ross commented on Mar 6, 2026

    @ranger-ross
    Member

    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

  16. CheckPickerUpper commented on Aug 24, 2026

    @CheckPickerUpper

    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.

  17. epage commented on Aug 24, 2026

    @epage
    ContributorAuthor

    We have #13136 for gc-ing build-dirs that are no longer in use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-cachingArea: caching of dependencies, repositories, and build artifactsC-enhancementCategory: enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions