Skip to content

Reduce size of target directory #66348

Description

@Diggsey

The size of the target directory is becoming a severe problem for me. I use rust at work, and we have many services written in rust. The target directory for each service is typically 2-3 GB for a fresh build, and increases without bound due to the lack of GC, so that after a short period you're looking at 4 GB per service.

For reference, the final size of the musl-based statically compiled binaries we deploy is about 100 MB each, or 40x smaller than the intermediate artefacts, and the majority of that is debug info (once we separate out debug info it's down to maybe 30 MB).

I have a decently sized SSD (500GB), but even so, this very quickly gets out of hand. For various reasons I can only really afford to give 10 GB of space dedicated to the rust services, which means I can only do local development on two at a time (the other services are run by pulling prebuilt docker images with no possibility for local changes) and this is assuming I regularly nuke my target directories.

If I compress a 4 GB target directory with 7-zip, it compresses to about 700 MB, so even if all of the data is required, there's quite a bit of scope for reducing the disk usage. Also, maybe some intermediate artefacts can be deleted entirely and just recreated as needed?

I've seen elsewhere the suggestion to share the target directory between services, but:

  1. I don't think that will actually help much here - the versions of dependencies often differ slightly between services and even a single service can reach the 10 GB limit on its own if left unchecked.
  2. Due to the lack of GC, it will become impossible to nuke the target directory for a single service, so the only option will be to nuke everything and then rebuild it all from scratch.

Activity

  1. added
    T-bootstrapRelevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    T-cargoRelevant to the cargo team, which will review and decide on the PR/issue.
    on Nov 13, 2019
  2. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    and removed
    T-bootstrapRelevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)
    on Nov 13, 2019
  3. Diggsey commented on Nov 15, 2019

    @Diggsey
    ContributorAuthor

    For an example service, the breakdown by file type is:

    Extension    Size (MB)
    ----------------------
    .rlib             1384
    .rmeta             935
    .pdb               826
    .bin               611
    .exe               225
    .o                 109
    .dll                61
    .json               28
    ...
    

    Another thing that stands out to me is that a large proportion of space is taken by the executables and PDB files of build scripts and that these build scripts are all duplicated: once for RLS and once for normal builds. I don't see a reason why RLS couldn't reuse the same build script artefacts as the main build?

  4. memoryruins commented on Nov 15, 2019

    @memoryruins
    Contributor

    For rls, rust-lang/rls#753 (comment) described the issue as

    We basically can't share data between RLS and non-RLS because the RLS builds are done with a custom compiler and with different flags.

  5. bugproof commented on Jun 18, 2021

    @bugproof

    This is a major problem because it eats your disk space quickly even with very small console programs. Couldn't it be stored in some global place so it could be re-used by multiple crates...?

  6. memoryruins commented on Jun 18, 2021

    @memoryruins
    Contributor

    @bugproof cargo has a tracking issue for ways to improve its cache usage rust-lang/cargo#7150 . In the "Reusing shared dependencies" section, it lists a couple options that can be used today.

  7. memoryruins commented on Aug 4, 2021

    @memoryruins
    Contributor

    #87405 is tracking plans to add artifact size profiling, which I suspect will help keep an eye on some contributing factors of this issue.

  8. jabberwock commented on Apr 18, 2026

    @jabberwock

    How is this still open?

  9. Chris00 commented on Apr 26, 2026

    @Chris00
  10. chances commented on May 12, 2026

    @chances

    This issue is a major problem for us resource-constrained developers! Not everyone has "infinite" disk space.

  11. Diggsey commented on Aug 3, 2026

    @Diggsey
    ContributorAuthor
    Image
  12. Kobzol commented on Aug 3, 2026

    @Kobzol
    Member

    rust-lang/cargo#17267 might help a bit with the target directory size.

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

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-cargoRelevant to the cargo team, which will review and decide on the PR/issue.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions