diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index bfb3295db2..06d83acdb7 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -101,36 +101,11 @@ jobs: # Optional (v2.3145): Maps JavaScript API browser key for the Dashboard jobs map; unset → OpenStreetMap. VITE_GOOGLE_MAPS_BROWSER_KEY: ${{ secrets.VITE_GOOGLE_MAPS_BROWSER_KEY }} + # What a built page must hold is scripts/lib/builtPage.mjs, with a test for each refusal (punch list #480): + # .nojekyll, index.html and 404.html at the top, a built asset named in the page, and no source file named + # in it. Until 2026-12-02 the rules were shell in this step and nothing ran them before a merge. - name: Verify build output - run: | - echo "Verifying build output..." - echo "Contents of dist directory:" - ls -la dist/ || true - echo "" - if [ ! -f dist/.nojekyll ]; then - echo "::error::.nojekyll file is missing from dist directory" - exit 1 - fi - if [ ! -f dist/index.html ]; then - echo "::error::index.html is missing from dist directory" - exit 1 - fi - if [ ! -f dist/404.html ]; then - echo "::error::404.html is missing — vite copy-404 plugin should copy index.html for GitHub Pages SPA paths" - exit 1 - fi - echo "Contents of dist/index.html:" - cat dist/index.html - echo "" - if ! grep -q "assets/index-" dist/index.html; then - echo "::error::index.html does not reference built assets correctly" - exit 1 - fi - if grep -q "main.tsx" dist/index.html; then - echo "::error::index.html still references source file main.tsx" - exit 1 - fi - echo "✓ Build output verified: .nojekyll present, index.html references built assets" + run: node scripts/check-built-page.mjs dist # One release of asset overlap: a tab still running the just-superseded build can # keep fetching its hashed route chunks instead of 404ing (which triggers the diff --git a/docs/recent-features/v2.9211.md b/docs/recent-features/v2.9211.md new file mode 100644 index 0000000000..d54ef9fad3 --- /dev/null +++ b/docs/recent-features/v2.9211.md @@ -0,0 +1,25 @@ +# v2.9211 — The check a new build passes before it goes live is one tested script (2026-12-02) + +Punch list #480, step 2. A workflow, a script and its test. No screen changes. Nothing owed on prod. + +## What was wrong + +`deploy.yml` builds the page and then checks it in its **Verify build output** step. The rules of that step were shell written into the workflow. They ran only on a push to `main`, and no test read them. On 2026-12-02 a comment in `index.html` named the page's source file, the last rule refused the built page, and every deploy of `main` failed for almost two hours. + +## What changed + +- `scripts/lib/builtPage.mjs` holds the rules as data: `.nojekyll`, `index.html` and `404.html` at the top of the built folder, a built asset named in the page (`assets/index-`), and no `main.tsx` named in it. `builtPageRefusals` answers every rule a page breaks, where the shell stopped at the first. +- `scripts/check-built-page.mjs [folder]` reads a built folder (`dist` when none is named), prints what is at its top and its `index.html` as the step always did, and exits 1 with one line a refusal. On a runner each line is an `::error::` annotation. `npm run check:built-page` runs it. +- `deploy.yml`'s **Verify build output** step is now `node scripts/check-built-page.mjs dist`. It sits where it sat, after the build and before the asset overlap and the upload. The words of each refusal are the shell's own. + +## How it is held + +`src/lib/builtPage.test.ts`: each refusal on a made-up page, a comment that names the source file as on 2026-12-02, the order of several refusals, the script's exit on made-up folders, and that `deploy.yml` runs the script after the build and keeps no copy of the rules in shell. + +Proven by hand before the merge: a `vite build` in the worktree, the script green on its `dist`, and red on the same `dist` with the source file's name put in a comment. + +## Not done here + +- The check still runs only after a merge. Step 3 of the card runs the build and this script on the merge queue's build. +- A failed deploy still tells nobody (step 4), and nothing on the live site names its commit (step 5). +- The first deploy of `main` after this merge is the proof on a runner: its **Verify build output** step must print the page and the verified line. diff --git a/package.json b/package.json index 27bf8acc30..8f1ddd4023 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "check:timezone": "node scripts/check-app-calendar-tz.mjs", "check:css-tokens": "node scripts/check-css-tokens.mjs", "check:edge-drift": "node scripts/check-edge-function-drift.mjs", + "check:built-page": "node scripts/check-built-page.mjs", "check:edge-bundles": "node scripts/check-edge-bundles.mjs", "check:edge-archived-guard": "node scripts/check-edge-archived-guard.mjs", "check:workflow-parity": "node scripts/check-workflow-parity.mjs", diff --git a/scripts/check-built-page.mjs b/scripts/check-built-page.mjs new file mode 100644 index 0000000000..57c13daeba --- /dev/null +++ b/scripts/check-built-page.mjs @@ -0,0 +1,35 @@ +#!/usr/bin/env node +/** + * The check a built page passes before it is deployed (punch list #480, step 2). It reads the built folder, prints + * what is at its top and the page's index.html as the deploy's step always did, and fails with every rule the page + * breaks. The rules and their words are `lib/builtPage.mjs`; deploy.yml's "Verify build output" step runs this. + * + * node scripts/check-built-page.mjs [built-folder] (default dist) + * + * Run it after `npm run build`. It reads files alone, so it takes under a second. + */ + +import { existsSync, readdirSync, readFileSync } from 'node:fs' +import { join, resolve } from 'node:path' +import { builtPageRefusals, builtPageReport } from './lib/builtPage.mjs' + +const dir = resolve(process.argv[2] ?? 'dist') +const annotate = process.env.GITHUB_ACTIONS === 'true' + +console.log(`Verifying build output in ${dir}...`) + +const files = existsSync(dir) ? readdirSync(dir).sort() : [] +console.log(files.length > 0 ? `At its top: ${files.join(' ')}` : 'The folder is missing or empty.') + +const indexPath = join(dir, 'index.html') +const indexHtml = existsSync(indexPath) ? readFileSync(indexPath, 'utf8') : null +if (indexHtml !== null) { + console.log('') + console.log('Contents of index.html:') + console.log(indexHtml) +} + +const refusals = builtPageRefusals({ files, indexHtml }) +console.log('') +for (const line of builtPageReport(refusals, { annotate })) console.log(line) +process.exit(refusals.length === 0 ? 0 : 1) diff --git a/scripts/lib/builtPage.mjs b/scripts/lib/builtPage.mjs new file mode 100644 index 0000000000..3161c9360d --- /dev/null +++ b/scripts/lib/builtPage.mjs @@ -0,0 +1,66 @@ +/** + * What a built page must hold before it is deployed (punch list #480, step 2). Until this file the rules were shell + * inside deploy.yml's "Verify build output" step, so they ran only after a merge and nothing tested them: on + * 2026-12-02 a comment in index.html that named the page's source file tripped the last rule, and every deploy of + * main failed for two hours. Here each rule is a row with its own words, and `builtPageRefusals` answers every rule + * the page breaks, not the first alone. + * + * Plain JS with no imports: deploy.yml runs `scripts/check-built-page.mjs` on bare Node right after the build. + */ + +/** The files GitHub Pages needs at the top of the built folder, each with why. */ +export const BUILT_PAGE_FILES = [ + { name: '.nojekyll', words: '.nojekyll file is missing from dist directory' }, + { name: 'index.html', words: 'index.html is missing from dist directory' }, + { + name: '404.html', + words: '404.html is missing — vite copy-404 plugin should copy index.html for GitHub Pages SPA paths', + }, +] + +/** A built page names its entry script by its hashed file under assets/. */ +export const BUILT_ASSET_MARK = 'assets/index-' + +/** The source file a built page must no longer name: the build rewrites the module tag to a hashed asset. */ +export const SOURCE_FILE_MARK = 'main.tsx' + +/** + * Every rule the built page breaks, in the order the deploy's step checked them. An empty list is a page that may + * be deployed. + * + * @param {{ files: Iterable, indexHtml: string | null }} page `files` are the names at the top of the built + * folder; `indexHtml` is the text of its index.html, or null when there is none. + * @returns {{ code: string, words: string }[]} + */ +export function builtPageRefusals(page) { + const files = new Set(page.files) + const refusals = [] + for (const file of BUILT_PAGE_FILES) { + if (!files.has(file.name)) refusals.push({ code: `missing:${file.name}`, words: file.words }) + } + const html = files.has('index.html') ? page.indexHtml : null + if (typeof html === 'string') { + if (!html.includes(BUILT_ASSET_MARK)) { + refusals.push({ code: 'no-built-asset', words: 'index.html does not reference built assets correctly' }) + } + if (html.includes(SOURCE_FILE_MARK)) { + refusals.push({ code: 'names-source-file', words: 'index.html still references source file main.tsx' }) + } + } + return refusals +} + +/** + * The lines the script prints for a list of refusals. On a GitHub runner each is an `::error::` line, which the run + * draws as an annotation; anywhere else it is a plain line. + * + * @param {{ code: string, words: string }[]} refusals + * @param {{ annotate?: boolean }} [opts] + * @returns {string[]} + */ +export function builtPageReport(refusals, opts = {}) { + if (refusals.length === 0) { + return ['✓ Build output verified: .nojekyll present, index.html references built assets'] + } + return refusals.map((r) => (opts.annotate ? `::error::${r.words}` : `✗ ${r.words}`)) +} diff --git a/src/content/releaseNotes/v2.9211.ts b/src/content/releaseNotes/v2.9211.ts new file mode 100644 index 0000000000..84b4439ed5 --- /dev/null +++ b/src/content/releaseNotes/v2.9211.ts @@ -0,0 +1,15 @@ +import type { ReleaseNote } from '../../lib/releaseNotes' + +const note: ReleaseNote = { + version: 'v2.9211', + date: '2026-12-02', + title: 'The check a new build passes before it goes live is one tested script', + kind: 'infra', + highlights: [ + 'Before a new build of the app goes live, a check reads the built page. It wants the page\'s files in place and the page pointing at its built code.', + 'That check was a few lines only the release step ran, and nothing tested them. On 2026-12-02 one of them stopped every release for two hours.', + 'The check is now one script with a test for each thing it refuses. It reads the same things as before, so nothing changes in the app.', + ], +} + +export default note diff --git a/src/lib/builtPage.test.ts b/src/lib/builtPage.test.ts new file mode 100644 index 0000000000..1c84cb6936 --- /dev/null +++ b/src/lib/builtPage.test.ts @@ -0,0 +1,162 @@ +import { describe, expect, it } from 'vitest' +import { execFileSync } from 'node:child_process' +import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join } from 'node:path' +// @ts-expect-error — deploy.yml runs the kernel on bare Node right after the build, so it is plain JS with no types +import * as kernel from '../../scripts/lib/builtPage.mjs' + +/** + * scripts/lib/builtPage.mjs holds what a built page must pass before it is deployed (punch list #480, step 2). + * These pin each refusal on a made-up page, the script's exit on a made-up folder, and that deploy.yml runs the + * script and no copy of the rules. + */ +type Refusal = { code: string; words: string } +const k = kernel as { + BUILT_PAGE_FILES: { name: string; words: string }[] + BUILT_ASSET_MARK: string + SOURCE_FILE_MARK: string + builtPageRefusals: (page: { files: Iterable; indexHtml: string | null }) => Refusal[] + builtPageReport: (refusals: Refusal[], opts?: { annotate?: boolean }) => string[] +} +const ROOT = join(__dirname, '..', '..') +const SCRIPT = join(ROOT, 'scripts', 'check-built-page.mjs') + +const GOOD_HTML = '
' +const GOOD_FILES = ['.nojekyll', '404.html', 'assets', 'index.html', 'sw.js'] +const codes = (page: { files: Iterable; indexHtml: string | null }) => k.builtPageRefusals(page).map((r) => r.code) + +describe('builtPageRefusals', () => { + it('passes a page with its three files, a built asset and no source file named', () => { + expect(k.builtPageRefusals({ files: GOOD_FILES, indexHtml: GOOD_HTML })).toEqual([]) + }) + + it('refuses a folder with no .nojekyll', () => { + expect(codes({ files: GOOD_FILES.filter((f) => f !== '.nojekyll'), indexHtml: GOOD_HTML })).toEqual(['missing:.nojekyll']) + }) + + it('refuses a folder with no index.html, and reads no page there', () => { + expect(codes({ files: GOOD_FILES.filter((f) => f !== 'index.html'), indexHtml: null })).toEqual(['missing:index.html']) + // Text handed over for a file that is not there is not read: the missing file is the one reason. + expect(codes({ files: GOOD_FILES.filter((f) => f !== 'index.html'), indexHtml: 'src/main.tsx' })).toEqual(['missing:index.html']) + }) + + it('refuses a folder with no 404.html', () => { + expect(codes({ files: GOOD_FILES.filter((f) => f !== '404.html'), indexHtml: GOOD_HTML })).toEqual(['missing:404.html']) + }) + + it('refuses a page that names no built asset', () => { + const html = GOOD_HTML.replace('/assets/index-Ab12Cd34.js', '/app.js') + expect(codes({ files: GOOD_FILES, indexHtml: html })).toEqual(['no-built-asset']) + }) + + it('refuses a page that still names its source file, in a tag or in a comment', () => { + const tag = GOOD_HTML.replace('', '') + expect(codes({ files: GOOD_FILES, indexHtml: tag })).toEqual(['names-source-file']) + // The night of 2026-12-02: the words sat in a comment, and the build keeps a comment. + const comment = GOOD_HTML.replace('', '') + expect(codes({ files: GOOD_FILES, indexHtml: comment })).toEqual(['names-source-file']) + }) + + it('answers every rule a page breaks, in the order the deploy checked them', () => { + expect(codes({ files: [], indexHtml: null })).toEqual(['missing:.nojekyll', 'missing:index.html', 'missing:404.html']) + expect(codes({ files: ['index.html'], indexHtml: '' })).toEqual([ + 'missing:.nojekyll', + 'missing:404.html', + 'no-built-asset', + 'names-source-file', + ]) + }) + + it('gives every refusal words of its own', () => { + const all = [ + ...k.builtPageRefusals({ files: [], indexHtml: null }), + ...k.builtPageRefusals({ files: GOOD_FILES, indexHtml: 'src/main.tsx' }), + ] + expect(all).toHaveLength(5) + expect(new Set(all.map((r) => r.words)).size).toBe(5) + for (const r of all) expect(r.words.length).toBeGreaterThan(20) + }) +}) + +describe('builtPageReport', () => { + it('says the page is verified when nothing is refused', () => { + expect(k.builtPageReport([])).toEqual(['✓ Build output verified: .nojekyll present, index.html references built assets']) + }) + + it('writes a refusal as an error line on a runner and a plain line anywhere else', () => { + const refusals = k.builtPageRefusals({ files: GOOD_FILES, indexHtml: 'nothing built' }) + expect(k.builtPageReport(refusals, { annotate: true })).toEqual(['::error::index.html does not reference built assets correctly']) + expect(k.builtPageReport(refusals)).toEqual(['✗ index.html does not reference built assets correctly']) + }) +}) + +describe('scripts/check-built-page.mjs', () => { + const run = (dir: string, env: Record = {}) => { + try { + const out = execFileSync(process.execPath, [SCRIPT, dir], { encoding: 'utf8', env: { ...process.env, GITHUB_ACTIONS: '', ...env } }) + return { code: 0, out } + } catch (e) { + const err = e as { status: number; stdout: string } + return { code: err.status, out: err.stdout } + } + } + const folder = (files: Record) => { + const dir = mkdtempSync(join(tmpdir(), 'built-page-')) + for (const [name, text] of Object.entries(files)) { + mkdirSync(join(dir, name, '..'), { recursive: true }) + writeFileSync(join(dir, name), text) + } + return dir + } + + it('passes a made-up built folder and prints its page', () => { + const dir = folder({ '.nojekyll': '', 'index.html': GOOD_HTML, '404.html': GOOD_HTML }) + try { + const r = run(dir) + expect(r.code).toBe(0) + expect(r.out).toContain('At its top: .nojekyll 404.html index.html') + expect(r.out).toContain(GOOD_HTML) + expect(r.out).toContain('✓ Build output verified') + } finally { + rmSync(dir, { recursive: true, force: true }) + } + }) + + it('fails a folder whose page names its source file, with an error line on a runner', () => { + const html = GOOD_HTML.replace('', '') + const dir = folder({ '.nojekyll': '', 'index.html': html, '404.html': html }) + try { + const r = run(dir, { GITHUB_ACTIONS: 'true' }) + expect(r.code).toBe(1) + expect(r.out).toContain('::error::index.html still references source file main.tsx') + } finally { + rmSync(dir, { recursive: true, force: true }) + } + }) + + it('fails a folder that is not there, naming each missing file', () => { + const r = run(join(tmpdir(), 'built-page-never-made')) + expect(r.code).toBe(1) + expect(r.out).toContain('The folder is missing or empty.') + for (const file of k.BUILT_PAGE_FILES) expect(r.out).toContain(file.words) + }) +}) + +describe('deploy.yml', () => { + const deploy = readFileSync(join(ROOT, '.github', 'workflows', 'deploy.yml'), 'utf8') + + it('runs the script after the build, on the folder it uploads', () => { + const build = deploy.indexOf('run: npm run build') + const verify = deploy.indexOf('run: node scripts/check-built-page.mjs dist') + const upload = deploy.indexOf("path: './dist'") + expect(build).toBeGreaterThan(-1) + expect(verify).toBeGreaterThan(build) + expect(upload).toBeGreaterThan(verify) + }) + + it('keeps no copy of the rules in shell', () => { + expect(deploy).not.toContain('grep -q "main.tsx"') + expect(deploy).not.toContain('dist/.nojekyll') + }) +}) diff --git a/src/lib/firstLook/showEntry.test.ts b/src/lib/firstLook/showEntry.test.ts index 56aad24856..0c59ba1465 100644 --- a/src/lib/firstLook/showEntry.test.ts +++ b/src/lib/firstLook/showEntry.test.ts @@ -44,9 +44,11 @@ describe('a First look’s page as an entry of its own (punch list #319 PR 4)', }) it('the build makes it, and the service worker leaves it to the network', () => { - // The app's entry must be keyed `index`: its script is then `assets/index-.js`, which the deploy's own check greps for. + // The app's entry must be keyed `index`: its script is then `assets/index-.js`, which the deploy's own check + // of the built page looks for (scripts/lib/builtPage.mjs, run by deploy.yml). expect(read('vite.config.ts')).toMatch(/input: \{ index: resolve\(__dirname, 'index\.html'\), show: resolve\(__dirname, 'show\.html'\) \}/) - expect(read('.github/workflows/deploy.yml')).toContain('grep -q "assets/index-" dist/index.html') + expect(read('scripts/lib/builtPage.mjs')).toContain("export const BUILT_ASSET_MARK = 'assets/index-'") + expect(read('.github/workflows/deploy.yml')).toContain('run: node scripts/check-built-page.mjs dist') expect(read('src/sw.ts')).toContain('/^\\/show\\.html$/') }) })