# DerivedData Locks in Git Worktrees

## Symptom & Root Cause

Running concurrent `xcodebuild` invocations against a shared `DerivedData` directory fails with `database is locked` on `build.db`, and serializing them behind a wrapper lock still yields wide wall-clock swings (`32–233s` in our legacy serialized `xcodebuild` script baseline, including queue lock contention and cache invalidation). Splitting `-derivedDataPath` per `git worktree` avoids lock errors, yet triggers a full cold rebuild because Xcode keys compilation artifacts and dependency records by absolute worktree and `DerivedData` paths.

## Exact Diagnostic & Fix Commands

Inspect active `xcodebuild` processes and `build.db` locks on the host:

```sh
# List running xcodebuild and swift-frontend processes
ps aux | grep -E "xcodebuild|swift-frontend" | grep -v grep

# Check which process holds a lock on DerivedData build.db (targeted lookup avoids slow full-tree scans)
find ~/Library/Developer/Xcode/DerivedData -maxdepth 3 -name build.db -exec lsof {} + 2>/dev/null
```

Work around immediate lock contention in plain `xcodebuild` by isolating `DerivedData` per worktree and disabling implicit package resolution churn:

```sh
# Isolate DerivedData per worktree (avoids lock contention, still cold on first run)
WORKTREE_HASH=$(pwd | shasum -a 256 | cut -c1-12)
xcodebuild build \
  -scheme MyApp \
  -destination "generic/platform=iOS Simulator" \
  -derivedDataPath "/tmp/dd-${WORKTREE_HASH}" \
  -onlyUsePackageVersionsFromResolvedFile
```

Audit why an `xcodebuild` (or fallback) incremental build or worktree handoff missed cache (`--explain-cache` runs without `--json`; direct-engine builds report per-target `cas_hits` / `cas_misses` directly):

```sh
# Attribute xcodebuild (and fallback) cache misses to path, compiler flag, or input changes
xcloom build --local --engine xcodebuild --explain-cache
xcloom cache-why MyApp --repo . --json
```

## How XCLoom Prevents Recurrence

XCLoom separates worktree caching across its two build engines: the `xcodebuild` worktree-cache path uses a local Unix-socket LLVM CAS plugin with path-normalized worktree and `DerivedData` prefixes, while `--engine direct` uses a file-backed APFS `clonefile(2)` direct CAS (`~/.cache/xcloom/direct-cas`) with relative content digests and optional BYO-bucket L2 Remote CAS (`--remote-cas` pointing to S3, Cloudflare R2, or a shared filesystem) to share compiled artifacts across Macs and CI without a vendor account. It also shares `Package.resolved`-keyed SwiftPM checkouts and preserves `project.pbxproj` timestamps with `.xcloom-gen-stamp` so project generators do not force graph replanning.

Under `--engine auto` (the local Debug default for `build`, `run`, `hot`, and `test`), direct compilation covers `.xcodeproj`, `.xcworkspace`, CocoaPods, and XcodeGen (`project.yml`) apps via Layer 1 native compilers and Layer 2 per-target hybrid delegation; standalone `Package.swift` apps and unsupported rules fall back to `xcodebuild` with `engine_fallback_reason`. Local Release builds and `xcloom archive --local` default to `xcodebuild` under `--engine auto` and support explicit `--engine direct` (`xcloom archive --local --engine direct`).

Measured worktree and incremental timings:

- Production iOS + watchOS app (`77.7 MB` `.ipa`, 7 nested bundles): fresh worktree in `1.32s` (`124/0` CAS) and 1-file Swift edit in `2.41s` (`118/2` CAS).
- 6-repo unified open-source benchmark (`v0.10.0` pre-release revision `3c39d3c`, logged in `docs/claims-ledger.md`, `2026-11-24`): all 6 repos compile via `direct` (`fallback_reason=-`), with fresh worktrees speeding up `3.96x–51.5x` across the 5 stable repos (`apple/sample-food-truck` `23.16s -> 0.45s` [`51.5x`], `Dimillian/IceCubesApp` `45.97s -> 1.00s` [`46.0x`], `jellyfin/Swiftfin` `58.70s -> 3.03s` [`19.4x`], `signalapp/Signal-iOS` `74.10s -> 5.15s` [`14.4x`], `wikimedia/wikipedia-ios` `47.36s -> 11.95s` [`3.96x`]).

## Frequently Asked Questions

### Does giving each git worktree its own DerivedData fix build speed?

No. Separate `DerivedData` directories eliminate `build.db` lock errors, but every new worktree starts with an empty cache and pays full cold compile time.

### Why did adding a Swift compiler flag invalidate every cached module?

Compiler flags are part of the compilation digest. In real TestFlight logs, adding `.treatWarning(..., as: .error)` in `Package.swift` dropped cache hits to `0/3419` (invalidating all `3,419` cached tasks) and raised archive time from `128–155s` to `284s`; `xcloom cache-why` flags this explicitly on `xcodebuild` and fallback builds, while direct-engine builds report per-target `cas_hits` / `cas_misses` directly.

### What happens if a target uses custom Xcode build phases XCLoom cannot compile directly?

With the default `--engine auto` for local Debug builds, XCLoom compiles `.xcodeproj`, `.xcworkspace`, CocoaPods, and XcodeGen (`project.yml`) apps via Layer 1 native compilers and Layer 2 per-target hybrid delegation; standalone `Package.swift` apps and unsupported custom rules fall back to `xcodebuild` and print an explicit `engine_fallback_reason` in `--json` output rather than silently skipping build phases. `Release` / `archive` defaults to `xcodebuild` under `--engine auto` and explicitly supports `--engine direct` (`xcloom archive --local --engine direct`).

## Related Links

- [Why Claude Code Spent 70% of My iOS Sessions Waiting on Xcode](/blog/70-percent-xcode-wait-claude-code-ios/)
- [iOS Simulator OOM Under Parallel Agents](/errors/ios-simulator-oom-parallel-agents/)
- [XCLoom Benchmarks](/benchmarks/)
- [All iOS Build & Simulator Errors](/errors/)
