#!/usr/bin/env bash # check-docc.sh # # DocC documentation guard: blocks NEW documentation warnings (broken symbol links, # ambiguous references, article/symbol collisions) from landing. # # DocC symbol links pin a symbol's FULL signature — `Score/toMusicXMLString(divisions:)` # stops resolving the moment a parameter is added. Nothing in `swift build` or `swift test` # checks this, so links rot silently: a sweep in 2026-07 found ~84 accumulated warnings, # including a link broken by the change that added `maxEventsPerBlock` to # `SynthesisEngine.init` in the same cycle it shipped. # # This is a RATCHET: a baseline is committed at scripts/docc-warnings-baseline.txt and the # check fails only on warnings that are NOT in it. The backlog has since been cleared, so the # baseline is EMPTY and any new warning fails. The baseline may only shrink — prefer fixing a # link over re-baselining it. # # Usage: # bash scripts/check-docc.sh # CI/default: fail on new warnings # bash scripts/check-docc.sh --update-baseline # accept current warnings as the baseline # bash scripts/check-docc.sh --strict # require ZERO warnings (goal state) # # Covers every consumer-facing catalog: LyrebirdSynth and LyrebirdUI. # # Run manually or from CI. Takes ~60s (DocC extracts symbol graphs). Deliberately NOT in # .githooks/pre-commit — that hook is fast by design. # # Exit 0 = no new warnings. set -euo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)" BASELINE="$ROOT/scripts/docc-warnings-baseline.txt" # Every catalog that ships to consumers. LyrebirdUI was unguarded until phase 238 — # check-docc.sh only built LyrebirdSynth, so its warnings were invisible. # The LiveCompile sub-package is deliberately excluded: it is a dev-only package with # no swift-docc-plugin dependency, so its one catalog file cannot be built from here. TARGETS=("LyrebirdSynth" "LyrebirdUI") if [[ $# -gt 1 ]]; then echo "usage: $0 [--update-baseline|--strict] (one option at a time)" >&2 exit 2 fi MODE="check" case "${1:-}" in --update-baseline) MODE="update" ;; --strict) MODE="strict" ;; "") ;; *) echo "usage: $0 [--update-baseline|--strict]" >&2; exit 2 ;; esac CURRENT="$(mktemp)" RAW="$(mktemp)" CURRENT_RAW="$(mktemp)" trap 'rm -f "$CURRENT" "$RAW" "$CURRENT_RAW"' EXIT normalise() { LC_ALL=C awk ' function flush() { if (pending) { print msg " :: (no source location)"; pending = 0 } } !docc && /Building documentation for/ { docc = 1; next } !docc { next } # Flush first: two consecutive `warning:` lines would otherwise drop the earlier one. /^warning:/ { flush(); msg = $0; sub(/^warning: /, "", msg); pending = 1; next } pending && /^[[:space:]]*--> / { loc = $0 sub(/^[[:space:]]*--> /, "", loc) sub(/:[0-9]+:[0-9]+(-[0-9]+:[0-9]+)?$/, "", loc) print msg " :: " loc pending = 0 next } pending { flush() } END { flush() } ' "$1" } echo "[1/2] Building DocC documentation for ${TARGETS[*]} (~60s)..." fail_build() { echo "ERROR: DocC build failed for '$2' ($1)." >&2 echo " Re-run manually to see it:" >&2 echo " swift package generate-documentation --target $2" >&2 tail -25 "$RAW" >&2 exit 1 } # Warnings do not make the plugin exit non-zero, so we must not treat a clean exit as # "no problems" — we diff the warning set ourselves below. But we do keep the real exit # code rather than discarding it, so a hard failure can't masquerade as a pass. : > "$RAW" for TARGET in "${TARGETS[@]}"; do ONE="$(mktemp)" DOCC_EXIT=0 (cd "$ROOT" && swift package generate-documentation --target "$TARGET" 2>&1) > "$ONE" || DOCC_EXIT=$? # NB: `cmd && fail_build` would abort under `set -e` whenever the test is false, so each # check is an explicit `if`. if [[ "$DOCC_EXIT" -ne 0 ]]; then cat "$ONE" >> "$RAW"; rm -f "$ONE"; fail_build "exit code $DOCC_EXIT" "$TARGET" fi if ! grep -q "Finished extracting symbol information" "$ONE"; then cat "$ONE" >> "$RAW"; rm -f "$ONE"; fail_build "symbol extraction never finished" "$TARGET" fi # A hard diagnostic can be emitted *after* extraction succeeds (e.g. a convert-stage # failure), in which case the marker is present and the exit code may still be 0. if grep -qE "^error:" "$ONE"; then cat "$ONE" >> "$RAW"; rm -f "$ONE"; fail_build "DocC emitted error: diagnostics" "$TARGET" fi # Normalise THIS target's log immediately. The awk below latches on the first # "Building documentation for" banner and never resets, so normalising a concatenation # of both targets would scan the second target's pre-banner SwiftPM build warnings as # if they were DocC diagnostics — the exact thing the awk comment says must not happen. normalise "$ONE" >> "$CURRENT_RAW" rm -f "$ONE" done LC_ALL=C sort -u "$CURRENT_RAW" > "$CURRENT" COUNT=$(wc -l < "$CURRENT" | tr -d ' ') echo "[2/2] Comparing $COUNT distinct warning(s) against the baseline..." if [[ "$MODE" == "update" ]]; then # Enforce the shrink-only invariant rather than merely asserting it: refuse to record # a warning the current baseline doesn't already contain. if [[ -f "$BASELINE" ]]; then ADDED="$(LC_ALL=C comm -23 "$CURRENT" "$BASELINE")" if [[ -n "$ADDED" ]]; then echo "REFUSED: --update-baseline would ADD $(printf '%s\n' "$ADDED" | wc -l | tr -d ' ') warning(s):" >&2 printf '%s\n' "$ADDED" | sed 's/^/ + /' >&2 echo >&2 echo "The baseline may only shrink. Fix these, or if the addition is genuinely" >&2 echo "intended, delete the baseline first and re-run to re-record from scratch." >&2 exit 1 fi fi cp "$CURRENT" "$BASELINE" echo "OK: baseline updated — $COUNT warning(s) recorded at scripts/docc-warnings-baseline.txt" echo " Commit it." exit 0 fi if [[ "$MODE" == "strict" ]]; then if [[ "$COUNT" -eq 0 ]]; then echo "OK: zero DocC warnings." exit 0 fi echo "FAIL (--strict): $COUNT DocC warning(s):" >&2 sed 's/^/ /' "$CURRENT" >&2 exit 1 fi if [[ ! -f "$BASELINE" ]]; then echo "ERROR: no baseline at $BASELINE" >&2 echo " Create it with: bash scripts/check-docc.sh --update-baseline" >&2 exit 1 fi # No `|| true`: a comm failure (e.g. a baseline sorted under a different collation) must # surface rather than silently produce bogus NEW/FIXED sets. NEW="$(LC_ALL=C comm -23 "$CURRENT" "$BASELINE")" FIXED="$(LC_ALL=C comm -13 "$CURRENT" "$BASELINE")" if [[ -n "$FIXED" ]]; then echo echo "Nice — $(printf '%s\n' "$FIXED" | wc -l | tr -d ' ') baselined warning(s) are now fixed:" printf '%s\n' "$FIXED" | sed 's/^/ - /' echo "Tighten the ratchet: bash scripts/check-docc.sh --update-baseline" fi if [[ -n "$NEW" ]]; then echo >&2 echo "FAIL: $(printf '%s\n' "$NEW" | wc -l | tr -d ' ') NEW DocC warning(s) not in the baseline:" >&2 printf '%s\n' "$NEW" | sed 's/^/ + /' >&2 echo >&2 echo "Most likely cause: a symbol link pins a signature that just changed — DocC links" >&2 echo "carry the FULL parameter list, so adding one parameter breaks every link to it." >&2 echo "Fix the link (preferred), or, if intentional:" >&2 echo " bash scripts/check-docc.sh --update-baseline" >&2 exit 1 fi echo "OK: no new DocC warnings ($COUNT baselined, 0 new)."