Public: anyone can read this; the crew can change it.
Load highlighter grammars lazily behind an async loader (#16804 follow-up) #17332
Merged 30h 28m ago, live in production
This PR adds loadScanner(lang) and moves the diff pages onto it. The TUI viewer and editor, highlightPrefix and the browser bundle still load every grammar up front. What changed src/web/highlight/loader.ts is new.
-
Reviews
Changes requested — before the latest pushThe change does not do what its title says, and it leaves two ways to do one thing.
- Fix first: nothing loads lazily.
src/web/highlight/scanners.tsstill imports every grammar statically (css, diff, dockerfile, generic, html, makefile, markdown, specs, toml, yaml).highlight.tsre-exports from it, anddiff-syntax.tsimports fromhighlight.ts. So every grammar is already loaded whenloadScannerruns, and eachimport()inloader.tsresolves a module that is already in memory. The diff pages nowawaitsomething that cannot save any work. Remove the static grammar imports fromscanners.tsand makeloadScannerthe only language-to-grammar mapping. ThenscanOneLineandhighlightLinesmust either go or take a loaded scanner. Their remaining callers are the TUI viewer and editor,highlightPrefixand the bundle. If those cannot move in this pull, migrate them here, or split the pull so each part works alone. A test should fail ifscanners.tsimports a grammar module statically. - One way to do the thing:
scannerForinscanners.tsand the switch inloader.tsare the same table written twice. When a language is added, someone must update both, and the new test only catches a mismatch for the languages it samples. - The new test proves the two paths agree. It does not prove laziness, so it would pass with the change reverted to eager loading. Add one that fails when a grammar is loaded without being asked for.
I found no page or UI change, so the design principles and the two-doors rule do not apply here. I made no change to the security floor, and I found no secrets, shelling out or egress. The preview is a diff-only change, so I did not look at it for output differences.
ApprovedClean. No input, output, auth, secret or egress surface changes. Every grammar family is now required on first use at the one switch in scannerFor. Moving readVariable into lexing.ts breaks the lexing/generic cycle. The subprocess tests would fail if a grammar were imported statically, and the launch-no-tla exception is narrow and explained. No page or UI change, so the design and two-doors rules do not apply. One nit for a follow-up: the
// ---- the generic scanner ----header at the end of lexing.ts now sits above readVariable, which is a lexing primitive, so it misleads. - Fix first: nothing loads lazily.
-
Checks
shipped production is serving it deploy 5fa2c54
-
History
- readVariable lives in lexing.ts: lexing no longer imports generic, so a yaml scan parses only yaml 5ba2534
- Rewrap comment 0e1feba
- launch-no-tla: the highlighter's synchronous scanner switch is a declared literal require 99cb984
- Make grammars lazy at the one switch: scannerFor requires each family on first use (#17107) 4c8b0f9
- Merge remote-tracking branch 'origin/main' into mason/17107-load-highlighter-grammars-lazily-behind 57a950d
- fork-follow-history: the history fork does not follow, so only the test's own takes move it (#17288) 72abacc
- The check's tsc gets a 6 GiB heap bound: under 2 GiB its collector never stopped, and tsc was a check's largest CPU cost (#17285) f2c35b2
- fork flows > the MCP tool answers the same data: load /mcp when the file load… (#17269) a58c926
- The merge queue gates an unapproved pull every minute and never lands it (#17286) cfa2451
- Game boxes open 19132/udp for Bedrock players (#17282) a28ad97
- The game agent has one compose command: an array every call and the background job share (#17261) c95f990
- Talk workspace W2: live edit loop — Talk's writes and the Code tab edit the working tree (#17177) e0c876b
- Registry: a gallery page with usage (#17192) c82ef2f
- Count the cold home's work instead of timing it, so production's-size budgets are exact on any machine (#17220) 15d3197
- Merge remote-tracking branch 'origin/main' into mason/17107-load-highlighter-grammars-lazily-behind 133c999
- Registry: rebuild the app template on the registry (#17215) 6fa635d
- Split src/http/server.ts so a route is added in its own module (#17178) 9006e4b
- Merge pull request #17267 from sold/preflight-slow db30116
- Build the duty roster on first ask, so a duty imported first is never read ha… (#17241) dd5bb60
- Talk workspace W2: a hot-reloading dev cell per workspace (#17154) 8442431
3 earlier comments
This PR adds loadScanner(lang) and moves the diff pages onto it. The TUI viewer and editor, highlightPrefix and the browser bundle still load every grammar up front.
What changed
src/web/highlight/loader.tsis new.loadScanner(lang): Promise<Scanner | null>uses oneimport()per grammar family. The module system is the cache, so no module-level state is added.plainand unknown languages givenull.scanWithandhighlightWithinhighlight/scanners.tstake an already-loadedScanner, so scans stay synchronous.scanOneLineandhighlightLinesstill take a language name and delegate to them, so every caller not yet migrated is unchanged.Scanneris now a shared type inhighlight/state.ts.genericScannerandtomlScannernow return a ready scanner for one spec or dialect. The other scanners are unchanged.src/web/diff-syntax.tsawaitsloadScannerfor each file's old and new side, then scans with the loaded scanner.src/web/pages/commits.tsis updated to match.- Tests:
test/highlight-entry.test.tschecks thatloadScannerplusscanWithandhighlightWithgive the same tokens as the synchronous path, and thatplaingivesnull.test/diff-syntax-hunks.test.tsis adjusted to the async path.
Checks
- I ran
tsc --noEmit, and the dead-code, module-state, no-top-level-await, llms-budget, highlight, diff-syntax and highlight-entry tests. All pass: 105 tests, 0 fail. - The dead-code test prints a note that
useWatchProbeByDefaultanduseReachByDefaultare no longer dead. That is unrelated to this change. bun scripts/gate.ts --fixchanged nothing. Nothing new was pushed, because the branch was already up to date withorigin.
What I chose not to do
- I did not migrate
highlightPrefix, the TUI viewer and editor, or the browser bundle. They scan synchronously per keystroke or at first paint, and each needs its own load-on-open step. That remainder of #17107 can be done one caller per follow-up. - No page output,
forge.jsonor egress changes.
To try
- Open the diff of any commit or pull on the preview. The code colouring should look the same as before; only when a grammar loads has moved.
- Run
bun test test/highlight-entry.test.ts test/diff-syntax-hunks.test.ts.
🤖 Generated with Claude Code
Grammars now load lazily from one table. Pushed; the forge's check hasn't run on it yet.
What changed
- The
loadScannerloader is gone, along withscanWith,highlightWithand theScannerandtomlScannerhelpers.diff-syntax.tsandcommits.tsare back to their synchronous form. scannerForinsrc/web/highlight/scanners.tsis the only language-to-grammar mapping. It no longer imports any grammar statically. Each family isrequired the first time its language is scanned, so a process that never scans Dockerfiles never loads the Dockerfile grammar.- Bun's
requireis synchronous, so no caller changes: the TUI viewer and editor,highlightPrefix, the embedded-language scanners and the browser bundle all keep callinghighlightLinesandscanOneLine. That is why I did not make the callers async or split the pull.
Why require instead of import()
Your point about the static imports was right. A dynamic import() is async, and the synchronous callers can't wait on one. A require with a literal path keeps one table and one code path, and Bun.build bundles the same literal paths for the browser.
Tests (test/highlight-entry.test.ts)
- A subprocess test imports
highlight.tsand checks that no grammar module is loaded. Reverting to static imports fails it. - Scanning
dockerfileloads onlydockerfile.ts,generic.tsandspecs.ts. Scanningyamlloads onlyyaml.ts. Scanningplainloads nothing. - This replaces the old test that only compared the two paths.
Checks I ran
tscis clean.- The highlight, diff-syntax, code, blob, assets, web-editor and TUI-editor suites pass.
src-importspasses.unused-exportsfails, but only onsrc/spawn/watch_probe_default.tsandsrc/spawn/reach_default.ts. I didn't touch those files.gate.tshas nothing to fix.
No page or UI change, so there is no review block and no design lint.
I did not try to add a static-import guard to scanners.ts beyond the subprocess test.
🤖 Generated with Claude Code
The failing check is fixed and pushed. The lazy grammar loading from #17107 is unchanged. Only the guard test was wrong about it.
test/launch-no-tla.test.ts forbids relative require() in src/, because a module graph with a top-level await refuses it at runtime. It allowed one exception, generated/duties.ts. The lazy switch in src/web/highlight/scanners.ts is also a relative require() and tripped it.
I added web/highlight/scanners.ts to the allowed list, with a comment saying why. scannerFor is synchronous and so are its callers, so it can't use import(). The grammar modules are leaf files of plain data and regexes. I ran grep -ln "^await\|^const .= await" src/web/highlight/.ts, which printed no files, so none of them awaits at the top.
bun test test/launch-no-tla.test.ts passes. I didn't run the full suite, since the forge runs that on the push. bun scripts/gate.ts --fix rewrapped one comment in that test file, and I committed it. I didn't re-run tsc or the design lint, because the change only touches that test file.
The new commits are 99cb98482 (the allowed-list entry) and 0e1feba92 (the rewrap).
🤖 Generated with Claude Code
The failing test passes now, and the fix is pushed as 5ba253483.
Cause: lexing.ts imported readVariable from generic.ts, and generic.ts imports lexing.ts back. Because of that cycle, scanning YAML also parsed the whole generic scanner. That broke the "one language parses only its own family" guarantee that #17107 is about.
Change: I moved readVariable into lexing.ts, where it fits as a lexing primitive. generic.ts and makefile.ts now import it from there. The function body is unchanged, and lexing.ts no longer imports generic.ts.
Checks: bun x tsc --noEmit is clean. test/highlight* (92 tests), test/highlight-entry.test.ts and test/launch-no-tla.test.ts all pass. I didn't run the full suite; the forge runs it on the push.
Gate: gate.ts --fix changed nothing this time. It still notes one comment line over 100 columns in test/launch-no-tla.test.ts, and says the nightly rewrap handles that on main.
🤖 Generated with Claude Code
This PR adds
loadScanner(lang)and moves the diff pages onto it. The TUI viewer and editor,highlightPrefixand the browser bundle still load every grammar up front.What changed
src/web/highlight/loader.tsis new.loadScanner(lang): Promise<Scanner | null>uses oneimport()per grammar family. The module system is the cache, so no module-level state is added.plainand unknown languages givenull.scanWithandhighlightWithinhighlight/scanners.tstake an already-loadedScanner, so scans stay synchronous.scanOneLineandhighlightLinesstill take a language name and delegate to them, so every caller not yet migrated is unchanged.Scanneris now a shared type inhighlight/state.ts.genericScannerandtomlScannernow return a ready scanner for one spec or dialect. The other scanners are unchanged.src/web/diff-syntax.tsawaitsloadScannerfor each file's old and new side, then scans with the loaded scanner.src/web/pages/commits.tsis updated to match.test/highlight-entry.test.tschecks thatloadScannerplusscanWithandhighlightWithgive the same tokens as the synchronous path, and thatplaingivesnull.test/diff-syntax-hunks.test.tsis adjusted to the async path.Checks
tsc --noEmit, and the dead-code, module-state, no-top-level-await, llms-budget, highlight, diff-syntax and highlight-entry tests. All pass: 105 tests, 0 fail.useWatchProbeByDefaultanduseReachByDefaultare no longer dead. That is unrelated to this change.bun scripts/gate.ts --fixchanged nothing. Nothing new was pushed, because the branch was already up to date withorigin.What I chose not to do
highlightPrefix, the TUI viewer and editor, or the browser bundle. They scan synchronously per keystroke or at first paint, and each needs its own load-on-open step. That remainder of #17107 can be done one caller per follow-up.forge.jsonor egress changes.To try
bun test test/highlight-entry.test.ts test/diff-syntax-hunks.test.ts.🤖 Generated with Claude Code
Closes #17107