#!/usr/bin/env bash # ONE derived-data root for this project, and every xcodebuild names it. # # source scripts/lib/derived-data.sh; dir="$(cotton_derived_data)" # bash scripts/lib/derived-data.sh # the same answer, for a non-shell caller # # WHY. An xcodebuild that passes no `-derivedDataPath` writes to # `~/Library/Developer/Xcode/DerivedData/Cotton-`, and # one that passes a path writes there instead. Until this change some of this # repo's invocations did one and some the other, so a Mac carried TWO derived # trees full of the same intermediates for the same project: on one Mac, # 2026-09-22, 22 GB under `~/.cache/cottondb/derived` and another 22 GB under # `~/Library/Developer/Xcode/DerivedData`. Neither is warmer for the other # existing, and only the first one is swept. # # The answer is resolved in ONE place rather than repeated at each call site, # because the duplication is exactly what let the two roots drift apart: # # 1. `COTTON_DERIVED_DATA`, when the caller set it. CI does, per head ref, and # that variable is what the `ios` job's sweep keeps. # 2. Otherwise `/`, named by `scripts/ci/cargo-target-dir.sh` — # the same sanitiser the per-branch cargo targets use, so `feat/thing` # becomes `feat-thing` here too and `scripts/ci/sweep-cargo-targets.sh` # bounds the root by the rules it already knows. The branch is READ FROM # THE ENVIRONMENT FIRST (`COTTON_HEAD_REF`, then the two the runner sets), # because `actions/checkout` leaves a DETACHED HEAD: asking git on a CI Mac # answers "HEAD", every job lands in the same directory, and the per-branch # keying this file exists for is silently off. Measured on run 83's `swift` # job, which resolved to `/local`. # 3. With no branch to read — a DETACHED checkout (`git worktree add # --detach`, as `scripts/prod-cut.sh` makes two of at once) or a tree that # is not a git checkout — `/detached--`: # named by the CHECKOUT, so two detached trees never answer one directory. # It was `/local` for all of them, and on 2026-10-10 prod-cut's Mac # and iPhone lanes, two detached worktrees building in parallel, both wrote # `/local`: the Mac lane died on "accessing build database # .../XCBuildData/build.db: disk I/O error" and the iPhone lane stalled. # Never Xcode's default: a path nothing sweeps is the failure this file # exists to end. # # `COTTON_DERIVED_ROOT` moves the root; the default is `~/.cache/cottondb/derived`. # # PER BRANCH, and per checkout where there is no branch: git checks a branch out # in one worktree at a time, so either key names one checkout, and two # concurrent xcodebuilds sharing one derived directory is a corruption rather # than a slowdown. Never assume the build lock keeps two checkouts apart — a # caller holding it can run two builds under it (prod-cut does). # # Guard: the ios-derived-data test. # The directory of THIS checkout, for when no branch names it. cotton_derived_checkout() { local root="$1" here="$2" top name top="$(git -C "$here/.." rev-parse --show-toplevel 2>/dev/null || (cd "$here/.." && pwd -P))" name="$(printf '%s' "$(basename "$top")" | tr -c 'A-Za-z0-9._-' '-')" printf '%s\n' "$root/detached-${name}-$(printf '%s' "$top" | cksum | cut -d' ' -f1)" } cotton_derived_data() { local root here branch dir if [ -n "${COTTON_DERIVED_DATA:-}" ]; then printf '%s\n' "$COTTON_DERIVED_DATA" return 0 fi root="${COTTON_DERIVED_ROOT:-$HOME/.cache/cottondb/derived}" here="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" branch="${COTTON_HEAD_REF:-${GITHUB_HEAD_REF:-${GITHUB_REF_NAME:-}}}" if [ -z "$branch" ]; then branch="$(git -C "$here/.." rev-parse --abbrev-ref HEAD 2>/dev/null || true)" fi if [ -z "$branch" ] || [ "$branch" = HEAD ]; then cotton_derived_checkout "$root" "$here" return 0 fi dir="$(bash "$here/ci/cargo-target-dir.sh" --per-ref "$branch" "$root" 2>/dev/null || true)" if [ -n "$dir" ]; then printf '%s\n' "$dir"; else cotton_derived_checkout "$root" "$here"; fi } # Runnable as well as sourceable, so a caller in another language asks for the # path the same way a shell script does rather than spelling the rule twice. if [ "${BASH_SOURCE[0]}" = "${0}" ]; then cotton_derived_data "$@" fi