DerivedData 并发锁与冷重建
现象与根因
多个编程 Agent 或脚本同时在共享 DerivedData 目录下调用 xcodebuild 时,常因 build.db 锁冲突报错;即便在外层套上串行脚本锁,实际墙钟耗时仍会大幅波动(在我们原有的串行 xcodebuild 脚本基线中为 32–233s,包含队列锁等待与缓存失效)。若给每个 git worktree 单独指定 -derivedDataPath,虽然避开了锁冲突,但 Xcode 将绝对目录和 DerivedData 路径编入了依赖键,每个新 worktree 都会退化为全量冷编。
诊断与修复命令
排查当前占用 DerivedData 与 build.db 的进程:
# 查看正在运行的 xcodebuild 与 swift-frontend 进程
ps aux | grep -E "xcodebuild|swift-frontend" | grep -v grep
# 定向检查持有 build.db 文件锁的进程(避免全目录扫描整个 DerivedData)
find ~/Library/Developer/Xcode/DerivedData -maxdepth 3 -name build.db -exec lsof {} + 2>/dev/null在原生 xcodebuild 环境下,临时按 worktree 隔离 DerivedData 并锁定包解析版本:
# 按 worktree 路径隔离 DerivedData(可避免锁冲突,但首次仍需冷编)
WORKTREE_HASH=$(pwd | shasum -a 256 | cut -c1-12)
xcodebuild build \
-scheme MyApp \
-destination "generic/platform=iOS Simulator" \
-derivedDataPath "/tmp/dd-${WORKTREE_HASH}" \
-onlyUsePackageVersionsFromResolvedFile定位 xcodebuild(及回退路径)增量重编或跨 worktree 缓存未命中的确切原因(--explain-cache 需在不带 --json 时执行;直连引擎会直接输出每个 target 的 cas_hits / cas_misses):
# 诊断 xcodebuild(及回退路径)由路径、编译参数或输入变更引起的缓存未命中
xcloom build --local --engine xcodebuild --explain-cache
xcloom cache-why MyApp --repo . --jsonXCLoom 如何防止复发
XCLoom 在两条构建路径上分别提供跨 git worktree 缓存:xcodebuild worktree 缓存路径采用本地 Unix-socket LLVM CAS 插件与路径无关的 worktree 和 DerivedData 前缀重映射;--engine direct 路径采用基于文件的 APFS clonefile(2) 直连 CAS(~/.cache/xcloom/direct-cas)与相对内容摘要,并支持可选的自有存储桶 L2 远程 CAS(--remote-cas,支持 S3、Cloudflare R2 或共享文件系统),在多台 Mac 与 CI 间共享编译产物而无需第三方账号。同时,XCLoom 复用按 Package.resolved 索引的 SwiftPM 检出,并用 .xcloom-gen-stamp 保留 project.pbxproj 时间戳避免工程生成器触发无谓重算。
在本地 Debug 的 build、run、hot、test 默认的 --engine auto 下,直连引擎通过第一层原生编译器和第二层按 target 混合委托覆盖 .xcodeproj、.xcworkspace、CocoaPods 和 XcodeGen(project.yml)应用;独立 Package.swift 应用与未支持的规则会回退到 xcodebuild 并输出 engine_fallback_reason。本地 Release 构建与 xcloom archive --local 在 --engine auto 下默认走 xcodebuild,同时显式支持 --engine direct(如 xcloom archive --local --engine direct)。
实测跨 worktree 与增量构建数据:
- 真实上线 iOS + watchOS 应用(77.7 MB
.ipa,7 个嵌套 bundle):全新 worktree 构建耗时 1.32s(124/0CAS),单文件修改耗时 2.41s(118/2CAS)。 - 6 仓库开源统一基准(
v0.10.0发布前构建修订3c39d3c,记录于docs/claims-ledger.md,2026-11-24):6 个仓库全部走direct引擎(fallback_reason=-),5 个稳定仓库的新建 worktree 加速达 3.96x–51.5x(apple/sample-food-truck23.16s -> 0.45s[51.5x]、Dimillian/IceCubesApp45.97s -> 1.00s[46.0x]、jellyfin/Swiftfin58.70s -> 3.03s[19.4x]、signalapp/Signal-iOS74.10s -> 5.15s[14.4x]、wikimedia/wikipedia-ios47.36s -> 11.95s[3.96x])。
常见问题
不能。隔离目录只解决 build.db 并发锁报错,新 worktree 的缓存为空,仍会承受完整的冷编译耗时。
编译参数本身是缓存键的组成部分。在真实 TestFlight 记录里,Package.swift 新增 .treatWarning(..., as: .error) 后导致缓存命中率跌至 0/3419(全量 3,419 项失效),归档时间从 128–155s 升至 284s。在 xcodebuild 与回退路径上,执行 xcloom cache-why 可直接定位到该参数变更;直连引擎则直接报告每个 target 的 cas_hits / cas_misses。
本地 Debug 默认的 --engine auto 通过第一层原生编译器和第二层按 target 混合委托编译 .xcodeproj、.xcworkspace、CocoaPods 和 XcodeGen(project.yml)应用;独立 Package.swift 应用与未支持的自定义规则会自动回退到 xcodebuild,并在 --json 结果中返回 engine_fallback_reason 字段说明原因,不会静默跳过任何构建步骤。Release / archive 在 --engine auto 下默认走 xcodebuild,同时显式支持 --engine direct(如 xcloom archive --local --engine direct)。
相关链接

# 通过 Homebrew 安装(或 curl -fsSL https://xcloom.dev/install.sh | sh,默认装入 ~/.local/bin)
brew install xcloom/tap/xcloom
# 只读环境与模拟器检查(不改动任何工程配置)
xcloom doctor
xcloom sim status
# 接入编程 Agent 的 MCP 与 Skill(无需 Node;默认当前仓库,加 --global 为用户全局安装)
xcloom mcp install --claude --codex --cursor
xcloom skill install --claude --codex --cursor也可以直接把下面一句话发给 Claude Code、Codex 或 Cursor,让 Agent 自动完成 CLI 安装、MCP 与 Skill 注册、环境校验和首次本地构建,并按需开启 git 合并驱动或运行当前仓库的构建对比测速:
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).需要 macOS 及本地已安装的 Xcode SDK 与编译器。无需迁移工程格式,直接支持现有的 .xcodeproj、.xcworkspace、CocoaPods 与 XcodeGen 工程;独立 Package.swift 应用与暂不支持的构建规则会自动回退至 xcodebuild。