Files
CatoandClaude Opus 5 ed17d502fe fix(verify): the gate was red on an unmodified main, twice over (#259)
`pnpm run verify` — the command this repo's definition of done names — exits
non-zero on a clean checkout of main, for two independent reasons that have
nothing to do with any change. CI cannot see either one.

**1. Generated output from tooling that was removed.**
#254 replaced contentlayer with bip-kit and deleted the `.contentlayer` line
from frontend/.prettierignore. Correct — the tool is gone. But the directory it
generated survives in every checkout that had built the blog before then, and
losing the ignore entry moved it from "invisible" to "in prettier's scope".
Prettier cannot parse its import assertions, so `format:check`, the FIRST gate,
died with a SyntaxError naming a generated file nobody wrote. A fresh clone has
no such directory, which is exactly why CI is green.

Two checkouts here had it, including the worktree of a session running right
now, whose verify is red for this reason as I write it. So preflight grows a
fourth check — same shape as the stale-node_modules one, and covering every
worktree rather than just the current one:

    ! generated output from removed tooling: frontend/.contentlayer
      .claude/worktrees/complete-fetch/frontend/.contentlayer

`--fix` deletes them. Proved by recreating the exact stale state, watching the
read-only run flag both paths, then watching --fix clear them.

**2. A required artifact that no step in the gate produced.**
`typecheck` ran `tsc` against a Prisma client that only ever got generated by
`@prisma/client`'s own postinstall hook, or by `build` — which runs *after*
typecheck. The hook fires in CI; it did not fire here, because pnpm gates
dependency build scripts and caches their side effects. So a clean
`pnpm install --frozen-lockfile` left the client ungenerated and verify died:

    src/lib/db.ts(1,10): error TS2305: Module '"@prisma/client"'
      has no exported member 'PrismaClient'.

Reproduced from an empty node_modules twice before changing anything. The fix
is not to re-approve a hook: a gate should not depend on an install side
effect it does not control. `typecheck` now runs `prisma generate` itself
(~300ms) and the artifact exists because the step that needs it made it.

Both fixes are the same principle — `verify` must depend on the repo, not on
what a previous install or a previous version of the tooling happened to leave
behind. The CLAUDE.md build section is corrected alongside: it still described
contentlayer as the blog pipeline and told the reader to run
`pnpm exec contentlayer build`, which would have recreated failure #1.

verify, from a wiped frontend/node_modules: prettier ok, lint 0 errors / 152
warnings, tsc clean, 96/96 tests (76 frontend + 20 backend), build ok.


Claude-Session: https://claude.ai/code/session_01LKTTAr8jsTvoZN6HneFtK9

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-08 04:16:33 +00:00

146 lines
6.3 KiB
Bash
Executable File

#!/bin/bash
# Preflight: make the working copy match reality before any work starts.
#
# Why this exists: session after session burned part of a run repairing the
# same class of entry-time drift, each time discovering it the hard way —
# 2026-08-29 local main held a commit origin never got; `git pull --ff-only`
# failed for every run entering the repo.
# 2026-08-31 local main was 2 commits behind origin (#232, #233).
# 2026-09-02 local main was 3 commits behind (#234-#236), two merged
# worktrees were still checked out, and `frontend/node_modules`
# predated the prettier devDependency added in #233 — so the very
# first gate of `npm run verify` died on "prettier: not found",
# which looks like a repo bug and is not one.
# 2026-09-04 #243 migrated npm -> pnpm (package-lock.json -> pnpm-lock.yaml)
# but this check still watched package-lock.json; once that file
# was deleted, `[[ -f "$lock" ]] || continue` made the freshness
# check a silent no-op for both packages, so a checkout still
# holding the old npm-flat node_modules — five commits and a
# package-manager migration stale — reported "clean".
# 2026-09-08 #254 removed contentlayer and its ignore entry; the generated
# directory it left behind was suddenly in prettier's scope, and
# `pnpm run verify` exited 2 on an unmodified main.
# A bare `git status` reports "clean" in all these cases. This closes the class.
#
# Usage: pnpm run preflight (read-only by default)
# pnpm run preflight --fix (fast-forward, prune, reinstall)
set -euo pipefail
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
cd "$PROJECT_ROOT"
FIX=0
[[ "${1:-}" == "--fix" ]] && FIX=1
RED=$'\033[0;31m'; YELLOW=$'\033[1;33m'; GREEN=$'\033[0;32m'; NC=$'\033[0m'
drift=0
note() { printf '%s\n' " $1"; }
warn() { printf '%s\n' "${YELLOW}!${NC} $1"; drift=1; }
ok() { printf '%s\n' "${GREEN}✓${NC} $1"; }
# 1. Divergence from origin. `git status` alone cannot see this without a fetch.
git fetch --quiet --prune origin
BRANCH="$(git rev-parse --abbrev-ref HEAD)"
if git rev-parse --verify --quiet "origin/$BRANCH" >/dev/null; then
behind="$(git rev-list --count "HEAD..origin/$BRANCH")"
ahead="$(git rev-list --count "origin/$BRANCH..HEAD")"
if [[ "$behind" != 0 || "$ahead" != 0 ]]; then
warn "$BRANCH is ${ahead} ahead / ${behind} behind origin/$BRANCH"
if [[ $FIX == 1 && "$ahead" == 0 ]]; then
git merge --ff-only "origin/$BRANCH" >/dev/null && note "fast-forwarded to origin/$BRANCH"
elif [[ "$ahead" != 0 ]]; then
note "${RED}ahead of origin — a prior run may have left unpushed commits; inspect before pushing${NC}"
fi
else
ok "$BRANCH in sync with origin"
fi
else
note "$BRANCH has no upstream yet — divergence check skipped"
fi
# 2. Worktrees and branches whose commits already landed upstream (squash-merged
# PRs leave these behind; `git branch --merged` misses them because the squash
# is a different commit — `git cherry` compares patch ids instead).
stale=()
while read -r _ _ ref; do
[[ "$ref" =~ ^\[(.+)\]$ ]] || continue
b="${BASH_REMATCH[1]}"
[[ "$b" == "$BRANCH" ]] && continue
if [[ -z "$(git cherry "$BRANCH" "$b" 2>/dev/null | grep '^+' || true)" ]]; then
stale+=("$b")
fi
done < <(git worktree list)
if ((${#stale[@]})); then
warn "merged worktree branches still checked out: ${stale[*]}"
if [[ $FIX == 1 ]]; then
for b in "${stale[@]}"; do
wt="$(git worktree list --porcelain | grep -B2 "branch refs/heads/$b\$" | head -1 | cut -d' ' -f2-)"
if [[ -n "$(git -C "$wt" status --porcelain)" ]]; then
note "${RED}$b has uncommitted changes — left alone${NC}"
continue
fi
git worktree remove "$wt" && git branch -D "$b" >/dev/null && note "removed $b"
done
git worktree prune
fi
else
ok "no stale worktrees"
fi
# 3. Installed dependencies older than the lockfile. This is what breaks
# `pnpm run verify` in a way that reads as a repo bug (see #233/prettier).
for pkg in frontend backend; do
lock="$pkg/pnpm-lock.yaml"
stamp="$pkg/node_modules/.modules.yaml"
[[ -f "$lock" ]] || continue
if [[ ! -d "$pkg/node_modules" || ! -f "$stamp" || "$lock" -nt "$stamp" ]]; then
warn "$pkg/node_modules is stale or missing (older than $lock)"
if [[ $FIX == 1 ]]; then
# A leftover npm-flat node_modules (from before the #243 pnpm migration)
# doesn't get cleanly overwritten by pnpm's symlinked layout — wipe first.
rm -rf "$pkg/node_modules"
(cd "$pkg" && pnpm install --frozen-lockfile) && note "reinstalled $pkg"
fi
else
ok "$pkg/node_modules matches lockfile"
fi
done
# 4. Generated output belonging to tooling the repo no longer has. When a tool
# is removed, its ignore entries go with it — so the directory it left behind
# stops being ignored and starts being *linted and formatted*. `git status`
# calls it untracked, `verify` dies at its first gate, and the error names
# the generated file rather than the removal that stranded it.
# 2026-09-08 #254 replaced contentlayer with bip-kit and deleted the
# `.contentlayer` line from frontend/.prettierignore. Every
# checkout that had ever built the blog still held
# frontend/.contentlayer, whose import assertions prettier
# cannot parse — `verify` exited 2 on main with nothing wrong
# in the repo. Two checkouts here had it, including a worktree.
# Worktrees are checked too: each has its own copy, and its own red verify.
dead_output=()
while read -r wt _; do
[[ -d "$wt/frontend/.contentlayer" ]] && dead_output+=("$wt/frontend/.contentlayer")
done < <(git worktree list)
if ((${#dead_output[@]})); then
warn "generated output from removed tooling: ${dead_output[*]#"$PROJECT_ROOT"/}"
if [[ $FIX == 1 ]]; then
for d in "${dead_output[@]}"; do
rm -rf "$d" && note "removed ${d#"$PROJECT_ROOT"/}"
done
fi
else
ok "no output from removed tooling"
fi
if [[ $drift == 1 && $FIX == 0 ]]; then
printf '\n%s\n' "${YELLOW}Drift found. Re-run with:${NC} pnpm run preflight --fix"
exit 1
fi
printf '\n%s\n' "${GREEN}Preflight clean.${NC}"