Ray's Knowledge Base
Unlock

Slow cargo test on macOS: target/debug/deps fills with hundreds of thousands of old .o files

PitfallVerified 13 Oct 2026Holds anywhere
Pitfall. The symptom, what causes it, and the fix that was run and seen to work.

Symptom#

cargo build and cargo test are very slow in a Rust workspace on macOS. Even ls target/debug/deps takes over a minute. target/ was 163 GB, and target/debug/incremental was 75 GB.

Cause#

Debug builds on macOS leave .o files beside each build (the debug info lives in them), and cargo never deletes the old ones. In the rgit repo, target/debug/deps held 809,759 files, of which 793,778 were .o files. A second cause is that every file in crates//tests/ becomes its own test program that links the whole crate again. rgit-cli had 27 such files, so one cargo test did 27 big links. Each debug test binary was about 220 MB, and many old copies stayed on disk.

Fix#

  1. Run cargo clean once, when no other agent or build is using cargo. The first build after it is a full rebuild. 2. Merge the integration test files into one test program per crate (tests/it/main.rs with one module per old file), so there is one link in place of 27. 3. Or run cargo clean or cargo sweep from time to time.

Evidence#

Agent session on the rgit repo, which measured the file counts above. The plan to merge the tests was written as CHG-f546x2 (branch test/one-test-binary). The effect of the clean and of the merge had not been measured yet.

A related case, cause not proven: the first CGImageSourceCreateWithData call blocked about 190 s, with no CPU, in a cargo test binary in a huge deps folder. The same binary in another folder ran in 0.16 s. ls of that deps folder took over 200 s. ImageIO or CFBundle may read the folder of the executable.