fix(scripts): derive the docs example files from the filesystem

Merged
DE
2mo ago
+111-9

What and why

scripts/check-docs-examples.ts guards one invariant: every file under docs/examples/ is shown verbatim on a docs page or in skills/e2e/, so the copy on a page cannot drift from the file it came from. It only guarded half of it. The check walked a hand-kept EXAMPLES map of file to page, which caught a page drifting from its example but never noticed an example that no page showed at all: the file was not a key in the map, so nothing read it and the script passed. AGENTS.md states the invariant as "docs/examples/ is typechecked and shown verbatim on docs pages and in skills/e2e/", and nothing else enforces it. tsc typechecks an example whether or not a page shows it, so an unshown example could land and stay.

This is the same shape as the hand-kept list scripts/check-error-codes.ts used to carry for its source roots. There the literal array had already drifted: packages/eas/src and packages/smol/src raised codes the reference was supposed to document, and neither was ever scanned, so the check stayed green while the reference went stale. This map has not drifted yet, since every example added so far also edited it, but it is the same manual step, and the open engine pull requests each add their new example to it by hand. One forgotten line and the check is silent again.

The fix reads the file list from the filesystem and reports any example the map does not carry. The map stays, because it is the only place that knows which page shows an example; the filesystem decides whether an example exists at all. That is the split error-codes.ts already uses, deriving the roots and leaving the checker to read them.

// scripts/docs-examples.ts
export function exampleFiles(): string[] {
  const files: string[] = [];
  const walk = (dir: string): void => {
    for (const entry of readdirSync(join(REPO_ROOT, dir), { withFileTypes: true })) {
      const path = `${dir}/${entry.name}`;
      if (entry.isDirectory()) walk(path);
      else if (entry.name.endsWith('.ts')) files.push(path);
    }
  };
  walk('docs/examples');
  return files.toSorted();
}

export function unmappedExamples(mapped: Iterable<string>): string[] {
  const known = new Set(mapped);
  return exampleFiles().filter((file) => !known.has(file));
}

check-docs-examples.ts reports a forgotten file before it checks the mapped ones, so one run lists both a drifted page and an unshown example:

docs/examples/web/orphan-example.ts: no page shows this example; add it to EXAMPLES and to the page that should
docs/browser.mdx: no code block matches docs/examples/web/browser-provider.ts

scripts/docs-examples.test.ts covers the walk, an empty map, a complete map, and the forgotten file. scripts/docs-examples.ts holds the two functions on their own so the test imports them without the checker's main() running, the same install-scripts.ts / check-install-scripts.ts split already in scripts/.

No changeset: this is a repo-side check under scripts/, not a published package.

mainthis branch
examples the check readsthe 14 keys in the mapevery .ts under docs/examples/
an example added with no pagesilently passes, exit 0fails, exit 1
a page drifting from its examplefails, exit 1fails, exit 1
scripts/**/*.test.ts17 pass22 pass

A note for the reviewer: a few open pull requests add a new engine's example to this map by hand. This change does not conflict with them, and it does not remove the line they add. It only makes forgetting that line fail the check instead of passing quietly.

Verified

Ran it locally: yes

  • Added docs/examples/web/orphan-example.ts and ran node scripts/check-docs-examples.ts. On main it printed the success line and exited 0. On this branch it exits 1 with docs/examples/web/orphan-example.ts: no page shows this example; add it to EXAMPLES and to the page that should. Removing the file returns both to exit 0.
  • A page drifting from its example still fails as before: appending a line to docs/examples/web/browser-provider.ts makes the checker exit 1 with docs/browser.mdx: no code block matches docs/examples/web/browser-provider.ts on both branches.
  • node --test "scripts/**/*.test.ts" passes, 22 tests, the 5 new ones included. Against main the new scripts/docs-examples.test.ts cannot import the module at all, which is the red state the test was written from.
  • sh node_modules/.bin/tsc -p scripts exits 0.
  • sh node_modules/.bin/oxlint scripts/docs-examples.ts scripts/docs-examples.test.ts scripts/check-docs-examples.ts reports 0 warnings and 0 errors.
  • fallow dead-code reports the same two pre-existing files it reports on main (scripts/check-dist.ts, scripts/stamp-dist.ts) and nothing new; neither module shows up as an unused file or export.
  • node scripts/check-docs-examples.ts and node scripts/check-docs-package-managers.ts exit 0 on the current tree: docs examples: 14 files shown verbatim in ....

Not run here: mint validate and mint broken-links (the docs site build). Both fail on this Windows machine loading sharp with Could not load the "sharp" module using the win32-x64 runtime, and fail the same way on an unmodified main, so it is a local baseline rather than this change. The two docs:check script steps this touches pass, and mint runs on Linux in CI.

Checklist

  • Changeset added (pnpm changeset) for any change to a published package, or the change is testbed, docs, or CI only. (Not needed: scripts/ only.)
  • Breaking change: title carries !, the changeset body names what breaks and what replaces it, and the deprecation warning landed a window earlier (stability policy in CONTRIBUTING.md). (Not applicable.)
  • Docs updated in the same PR: the docs/**/*.mdx page for the behavior, and skills/e2e/ if the skill describes it. (Not applicable: no documented behavior changes.)
  • Contract change: emitted .d.ts reviewed, tests/types/sdk-types.ts updated; wire change edits the schema and both fixtures. (Not applicable.)
  • Engine contract change (e2e/engine): changesets for e2e, @e2e-dev/web, and @e2e-dev/mobile. (Not applicable.)