#!/usr/bin/env bun // Enforce, as a ratchet, that every `node:` import carries a `why:` comment on it or directly above. // // Root `CLAUDE.md`: "Bun only. No Node-specific APIs unless via `node:` and unavoidable, and then // **with a comment saying why**." Nothing read that sentence, so per axiom 3 the second half of the // rule did not exist: measured 2026-08-23, 238 of the 4,027 files under `packages/` and `scripts/` // import a `node:` builtin and the gate had no opinion about any of them. Issue #280 counted 119 of // 210 and the growth between the two counts is the argument for a rule over a sweep. // // WHY THE COMMENT IS THE RULE AND THE IMPORT IS NOT. `node:` is not banned — `writeSync` on fd 1 is // the only synchronous stdout write Bun has, `mkdtemp` is the only temp-directory API, and // `node:async_hooks` is the ALS seam every scope in the framework opens through. What is banned is // reaching for one without saying which Bun native was missing, because that sentence is the only // thing that lets the next agent delete the import when Bun ships the native. // // A TEST FILE IS SOURCE, `As of 2026-08-26`. `checkNodeImports` opened with // `if (isTestPath(file.path)) continue` at both of its walks, so the SCANNER read every test file // and the RATCHET dropped every finding: measured, 404 unexplained imports across 164 test files // under a green `bun run node-imports`, and `storage` — flagged by review on #364 — had no row in // the pin table at all. `CLAUDE.md`'s non-negotiable exempts nothing, and it records this exact // mechanism happening once before: "`checkErrorFixes` skips test files, so the rule was prose there // and 422 sites accumulated under a green gate". Issue #365. // // A LITERAL `why:`, not "a comment nearby". A token is greppable and a paragraph is not, and the // rule has to be decidable from text: `scripts/lib/log.ts` already writes the sentence this asks // for, in a doc comment ending "A node: API, and unavoidable — Bun has no synchronous stdout write // of its own", and the token is what makes that a machine-checkable claim rather than good prose. // // bun run node-imports · bun run scripts/node-imports.ts [--json] // bun run scripts/node-imports.ts --unpin [,] # shrink the ratchet import { renderFixShellArg } from '../packages/core/src/error-render'; import { maskLiterals } from '../packages/core/src/source-mask'; import type { SourceFile } from './boundaries'; import { corpus } from './lib/corpus'; import type { Finding } from './lib/log'; import { isBuiltinSpecifier } from './lib/node-builtins'; import { NODE_IMPORT_PINS, NODE_PINS_FILE } from './lib/node-import-pins'; import type { PinTarget } from './lib/pin-edit'; import { pinRaiseFix, pinRaiseOf } from './lib/pin-edit'; import { ratchetPinMain } from './lib/pin-edit-main'; import type { PinTable, RatchetGap } from './lib/ratchet'; import { ratchetGaps } from './lib/ratchet'; import type { SiteProbe } from './lib/ratchet-sites'; import { leadSite, newSitesFirst, siteList, siteTarget } from './lib/ratchet-sites'; import { isCode, lineOf } from './lib/source-scan'; const SCRIPT = 'node-imports'; /** Where this ratchet's pins live: what its over finding's raise and `--write-pin` edit. */ export const NODE_IMPORT_PIN_TARGET: PinTarget = { script: 'scripts/node-imports.ts', file: NODE_PINS_FILE, table: 'NODE_IMPORT_PINS', }; const EXPLAIN = 'bun run scripts/node-imports.ts --explain --json lists every one'; /** * Every spelling that reaches a builtin: a static `from 'node:x'`, a side-effect `import 'node:x'`, * a dynamic `await import('node:x')` — the specifier in any quote, a backtick included, so long as * it interpolates nothing — a `require('node:x')` and `process.getBuiltinModule('node:x')`, which * Bun implements and which takes no `import` keyword at all (sweep 11). Each with or without the * prefix. * The dynamic form is here because `scripts/async-context-guard.ts`'s own header names it as the * hole a static-only scan leaves. A bare candidate counts only when its root is a Node builtin * (`lib/node-builtins.ts`). * The keyword must stand alone: after `.`/`?.` it names a method (`loader.require('fs')`, * `Buffer.from('fs')`), after an identifier character it is part of one (`myrequire`). Neither * reaches a builtin, so a finding there would ask a `why:` of an import that never happens. */ const NODE_IMPORT = /(?:(? { const match = NODE_IMPORT_END.exec(line); return match !== null && isBuiltinSpecifier((match[1] ?? match[2]) as string); }; /** The line a wrapped import opens on: `} from 'node:x'` closes a statement begun by `import {`. */ const importStart = (lines: readonly string[], index: number): number => { if (!(lines[index] ?? '').trimStart().startsWith('}')) return index; for (let above = index - 1; above >= 0; above -= 1) { if (/^\s*import\b/.test(lines[above] ?? '')) return above; } return index; }; /** * Whether a `why:` sits on the import's own line, in the comment block directly above it, or * above the contiguous block of `node:` imports it belongs to. * * ONE SENTENCE PER BLOCK. `mkdtemp` and `tmpdir` are reached for together and one `why:` above the * pair says so; the second import used to count as unexplained, which made authors repeat the * sentence. What ends a block is anything that is not a `node:` import: a blank line, a statement, * an import of anything else — so a `why:` three statements up still covers nothing. A `why:` * trailing one import is that import's alone. A doc comment spanning ten lines does count, because * that is where the framework already writes these sentences. */ export function hasWhy(lines: readonly string[], index: number): boolean { if (WHY.test(lines[index] ?? '')) return true; // A blank line may sit between a comment and the import it explains, never between two imports // of one block: seen on the way up, it ends the walk at the next import. let blank = false; for (let above = importStart(lines, index) - 1; above >= 0; above -= 1) { const line = (lines[above] ?? '').trim(); if (line === '') { blank = true; continue; } if (line.startsWith('//') || line.startsWith('*') || line.startsWith('/*')) { if (WHY.test(line)) return true; continue; } if (blank || !endsNodeImport(line)) return false; above = importStart(lines, above); } return false; } /** * Every unexplained `node:` import in one file, in source order. * * INPUT vs IMPORT, and BOTH carriers are exempt. `maskLiterals` blanks comment text and string * contents alike while preserving every offset, so a match survives it exactly when the process * would really evaluate the import. A rule's own fixture spells the forbidden shape as DATA — this * file's own test does, `browser-barrel.test.ts` and `async-context-guard.test.ts` do, and * `packages/cli/src/templates/` emits app source inside template literals the CLI writes and never * runs. A COMMENT is the half a string-literal exemption misses, and missing it is not theoretical: * `async-context-guard.test.ts:106` explains the shape by quoting it. `dead-docs-host.ts` states * the same carve-out — "a comment naming the host as the thing that was removed cannot 404". * One mask closes both, and it is the one `render-modes`, `frozen-records`, `secret-compare` and * `proto-index` already read, so there is no second tokenizer here. */ export function scanNodeImports( path: string, source: string, masked: string = maskLiterals(source), ): readonly NodeImportSite[] { const lines = source.split('\n'); const out: NodeImportSite[] = []; for (const match of source.matchAll(NODE_IMPORT)) { const specifier = match[2] as string; if (!isBuiltinSpecifier(specifier)) continue; if (!isCode(masked, match.index, match[0] as string)) continue; const line = lineOf(source, match.index); if (hasWhy(lines, line - 1)) continue; out.push({ path, line, specifier }); } return out; } export type NodeImportGap = RatchetGap; export interface NodeImportInput { readonly files: readonly SourceFile[]; readonly pins: PinTable; } /** The ratchet over fixture files: a package may hold what it is pinned at, may fall, never rise. */ export const checkNodeImports = (input: NodeImportInput): readonly NodeImportGap[] => ratchetGaps( input.files.flatMap((file) => scanNodeImports(file.path, file.source)), input.pins, input.files.length > 0, ); const at = (site: NodeImportSite | undefined): string => site === undefined ? '' : `${site.path}:${String(site.line)}`; /** Every site, new ones first — the package's first is usually one its pin already allows. */ const overFinding = (gap: NodeImportGap): Finding => ({ code: 'X_NODE_IMPORT_UNEXPLAINED', cause: `${gap.pkg} imports a node: builtin without saying why in ${String(gap.found)} place(s) and is pinned at ${String(gap.pinned)} — ${siteList(gap, (site) => `${at(site)} (${site.specifier})`, EXPLAIN)} — and no comment says which Bun native was missing, so nobody can tell whether it is still unavoidable`, fix: `bun run scripts/node-imports.ts --explain --json # lists every site: above ${siteTarget(gap, at)} add a comment beginning "why:" and naming the Bun API that does not exist — e.g. // why: Bun has no synchronous stdout write, and process.stdout.write drops its queue on exit; or ${pinRaiseFix(pinRaiseOf(NODE_IMPORT_PIN_TARGET, gap))}`, at: at(leadSite(gap)), }); const staleFinding = (gap: NodeImportGap): Finding => ({ code: 'X_NODE_IMPORT_PIN_STALE', cause: `${gap.pkg} is pinned at ${String(gap.pinned)} unexplained node: import(s) and has ${String(gap.found)} — the pin is above what this tree contains, so it would let ${String(gap.pinned - gap.found)} back in`, fix: `bun run scripts/node-imports.ts --unpin ${renderFixShellArg(gap.pkg, '')}`, at: NODE_PINS_FILE, }); const unscannedFinding = (): Finding => ({ code: 'X_NODE_IMPORT_UNSCANNED', cause: 'no source file was read, so every package reports zero and the ratchet enforces nothing — a glob that matches nothing reads exactly like a tree of pure Bun', fix: "grep -n 'CORPUS_PATTERNS' scripts/lib/corpus.ts # edit it so it matches this repo layout, then: bun run scripts/node-imports.ts", at: 'scripts/lib/corpus.ts', }); /** Every unexplained import in the tree, read off the shared corpus and its cached mask. */ export const nodeImportSites = async (root: string): Promise => (await corpus(root, 'source')).flatMap((file) => scanNodeImports(file.path, file.source, file.masked), ); export const nodeImportFindingFor = (gap: NodeImportGap): Finding => gap.kind === 'over' ? overFinding(gap) : gap.kind === 'stale' ? staleFinding(gap) : unscannedFinding(); const PROBE: SiteProbe = { rescan: (path, source) => scanNodeImports(path, source), line: (site) => site.line, }; export const nodeImportGaps = async (root: string): Promise => newSitesFirst(root, ratchetGaps(await nodeImportSites(root), NODE_IMPORT_PINS, true), PROBE); /** What this rule contributes to `x verify`'s `unit` step, through `node-imports.test.ts`. */ export const nodeImportFindings = async (root: string): Promise => (await nodeImportGaps(root)).map(nodeImportFindingFor); if (import.meta.main) { await ratchetPinMain({ target: NODE_IMPORT_PIN_TARGET, script: SCRIPT, pinsFile: NODE_PINS_FILE, pins: NODE_IMPORT_PINS, sites: nodeImportSites, findingFor: nodeImportFindingFor, probe: PROBE, clean: 'every node: import above its pin says why it is unavoidable', }); }