| summary | Share one Cargo target dir across all worktrees to skip redundant rebuilds (76s -> 5s) | |||
|---|---|---|---|---|
| read_when |
|
Each git worktree gets its own target/ directory. Every first build in a new worktree recompiles all dependencies from scratch (~76s). With many worktrees this also eats massive disk space.
A shared target directory lets Cargo reuse compiled artifacts across all worktrees. Measured improvement: 76s -> 5s for a fresh worktree build.
.cargo/config.toml is already gitignored, so this is local to your machine.
-
Create
.cargo/config.tomlin the repo root with an absolute path to a shared target dir:[build] target-dir = "/absolute/path/to/pika/.shared-target"
Replace the path with wherever your pika repo lives. The path must be absolute because Cargo resolves relative paths from the working directory, not the config file.
-
Done. All worktrees under
pika/worktrees/andpika/.worktrees/are subdirectories of the repo root, so Cargo's parent-directory walk finds the same config automatically. -
Symlink
targetto.shared-targetso recipes that hardcodetarget/still work:rm -rf target && ln -s .shared-target target -
(Optional) Delete old per-worktree
target/dirs to reclaim disk space:rm -rf worktrees/*/target .worktrees/*/target
- Cargo walks parent directories to find
.cargo/config.toml - All worktrees share one
target/directory via the config - Cargo's incremental compilation handles the rest: deps compiled by any worktree are reused
-
No concurrent builds: Cargo holds a lock on the target dir. If two worktrees build at the same time, one blocks until the other finishes.
-
Absolute path: Machine-specific, but since
.cargo/config.tomlis gitignored that's fine. -
Branch switching thrash: If two worktrees have very different deps, Cargo may recompile more. In practice most worktrees share 99% of deps.
-
Stale
target/directory: If you previously built without the shared target config, atarget/directory with old artifacts may still exist in the repo root. The justfile'sios-gen-swiftand other recipes referencetarget/$PROFILE/, so a staletarget/will shadow the correct.shared-target/output, causing uniffi to generate bindings from an outdated dylib. After enabling the shared target, delete or replace the oldtarget/directory:rm -rf target && ln -s .shared-target targetThe symlink ensures recipes that hardcode
target/still find the correct artifacts.
As a complementary optimization, install sccache to cache individual rustc invocations by input hash:
brew install sccacheIn ~/.cargo/config.toml (your home dir, not the repo):
[build]
rustc-wrapper = "/opt/homebrew/bin/sccache"This helps when the shared target dir has a cache miss. On its own it only cuts ~76s to ~45s, so it's a nice-to-have on top of the shared target, not a replacement.