#!/usr/bin/env bash # check-doc-snippets.sh # # Doc-example guard: proves the ```swift examples in the DocC catalog and the /// doc # comments still compile against the real API. # # Nothing else does this. `swift build` never sees a code fence, and check-docc.sh only # validates symbol LINKS. So examples rot silently, and have: # - `engine.add(...)` became throwing; 11 doc examples kept calling it without `try`. # - `LyrebirdFloat` was deleted; 75 references outlived it, including # `@UIParam var amplitude: LyrebirdFloat = 0.5`, which cannot compile. # Both are type errors, so typechecking the fences catches them. # # Each fence is generated into a throwaway .swift under $(mktemp -d) — nothing is written # to the working tree — wrapped in `@MainActor func _fence_N()` and preceded by # `#sourceLocation`, so a failure points at the ORIGINAL markdown file and line. # # The set of checked fences is recorded in scripts/doc-snippets-manifest.txt. It is # produced by asking the COMPILER which fences compile (--classify), not by hand-annotating # 900 fences. Fences that compile under no preamble self-exclude — that is how iOS-only # examples, Package.swift manifest snippets, and pseudo-code drop out with no skip list. # # Usage: # bash scripts/check-doc-snippets.sh # CI/default: typecheck the recorded set # bash scripts/check-doc-snippets.sh --classify # re-record which fences are checkable # # Run manually or from CI. Requires a prior `swift build` (it typechecks against the built # module). Deliberately NOT in .githooks/pre-commit — that hook is fast by design. # # Exit 0 = every recorded example still compiles. set -euo pipefail ROOT="$(cd "$(dirname "$0")/.." && pwd)" MODE="check" SHRINK="" case "${1:-}" in --classify) MODE="classify" if [[ "${2:-}" == "--allow-shrink" ]]; then SHRINK="--allow-shrink" elif [[ $# -gt 1 ]]; then echo "usage: $0 --classify [--allow-shrink]" >&2; exit 2; fi ;; "") if [[ $# -gt 0 ]]; then echo "usage: $0 [--classify [--allow-shrink]]" >&2; exit 2; fi ;; *) echo "usage: $0 [--classify [--allow-shrink]]" >&2; exit 2 ;; esac WORK="$(mktemp -d)" trap 'rm -rf "$WORK"' EXIT echo "[1/2] Building the module the examples typecheck against..." (cd "$ROOT" && swift build) > /dev/null if [[ "$MODE" == "classify" ]]; then echo "[2/2] Asking the compiler which fences compile (slow — tries each preamble)..." python3 "$ROOT/scripts/doc_snippets.py" classify "$ROOT" "$WORK" $SHRINK echo " Commit scripts/doc-snippets-manifest.txt." else echo "[2/2] Typechecking the recorded doc examples..." python3 "$ROOT/scripts/doc_snippets.py" check "$ROOT" "$WORK" fi