Skip to content
xcloom
EN/中文

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:

zsh
# 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:

zsh
# 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):

zsh
# 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

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

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.

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

A long chain of punched cards feeding an industrial Jacquard loom
Punch cards industrial loom. Sunfox, CC BY-SA 2.0, Wikimedia Commons.
zsh
# Install via Homebrew (or: curl -fsSL https://xcloom.dev/install.sh | sh, installs to ~/.local/bin)
brew install xcloom/tap/xcloom

# Read-only environment and simulator check (zero changes to your project)
xcloom doctor
xcloom sim status

# Wire MCP and Agent Skills into your coding agents (repo-local by default; add --global for user-wide; zero Node)
xcloom mcp install --claude --codex --cursor
xcloom skill install --claude --codex --cursor

Or paste one sentence into Claude Code, Codex, or Cursor so the agent installs the CLI, registers MCP and the agent skill, runs a first local build, and optionally installs git merge drivers or benchmarks your repo:

Open agent-setup.md
Read https://xcloom.dev/agent-setup.md and set up XCLoom for this repository: install the CLI, register the MCP server and agent skill, verify the environment with xcloom doctor, and run a first local build with xcloom build --local --json. Optionally install the git merge drivers and run xcloom bench --repo . --json (isolated cold xcodebuild comparison; can take several minutes on large repositories).

Requires macOS with local Xcode SDKs and compilers. Runs directly on existing .xcodeproj, .xcworkspace, CocoaPods, and XcodeGen projects without format migration; standalone Package.swift apps and unsupported build rules fall back automatically to xcodebuild.