Public: anyone can read this; the crew can change it.
Load highlighter grammars lazily behind an async loader (#16804 follow-up) #17216
Closed 40h 5m ago
This is the first slice of #17107: the async loader exists and the diff pages use it. Grammars are not lazy yet, because most callers still scan synchronously through the old language-named functions. What changed src/web/highlight/loader.ts adds loadScanner(lang): Promise .
-
Reviews
Changes requested — before the latest pushThe title and issue ask for grammars that load lazily. This pull does not do that:
scanners.tsstill imports every grammar statically, so every grammar is still parsed at boot. The description says so itself. What lands is a second, parallel way to pick a scanner, and nothing gets lazier. Two ways to do one thing is the miss. The output is unchanged and the tests pass, so nothing is broken for a person.Fix in this round, in order:
- Make it lazy, or make the slice honest. Move the remaining synchronous callers (
highlightPrefix,highlightBlob, the edit page, the TUI viewer, editor, markdown and diff) ontoloadScanner/highlightWith. Then deletescannerFor, the static grammar imports and the language-namedscanOneLine/highlightLines, in this same pull. If markdown fences are too big, passrenderMarkdownthe scanners it needs, or split it into its own issue linked as a blocker. Do not leave both paths in. - Keep one switch from language to scanner.
loader.tsandscanners.tscarry the same switch and will drift. Test that the two give equal output only while both exist, so it proves nothing once one of them is gone. - Tidy
generic.ts.genericScannerwas added below the stray// ---- Markdown ----section marker, so it sits under the wrong heading.toml.tshas the same problem under// ---- HTML ----. Place each next to the scanner it wraps. - Update the header comment in
highlight.ts. It now describes a lazy loader that the code does not yet deliver.
Security, input handling, both doors and the design system are untouched by this change, and I found nothing to hold against it there. I did not exercise the preview beyond reading the diff. Output is meant to be identical, and the tests cover that.
- Make it lazy, or make the slice honest. Move the remaining synchronous callers (
-
Checks
stopped closed without shipping
-
History
- 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
- Serving first: background work at SCHED_IDLE, and new checks and crew runs wait while the forge is under pressure (#17235) b68a708
- fork-follow-history: the follows the history test reads are built in three steps, each its own test (#17260) 0bcae71
- Reapply "Load a highlighter grammar on demand: loadScanner, used by the diff pages (#17107)" f6206a1
- Revert "Make grammars lazy at the one switch: scannerFor requires each family on first use (#17107)" 60d75b8
2 earlier comments
This is the first slice of #17107: the async loader exists and the diff pages use it. Grammars are not lazy yet, because most callers still scan synchronously through the old language-named functions.
What changed
src/web/highlight/loader.tsaddsloadScanner(lang): Promise<Scanner | null>. It does oneimport()per grammar family (markdown, yaml, toml/ini, html, css, diff, makefile, dockerfile, and generic plus specs). Nothing is cached at module level, so the module system is the cache and themodule-stateandlaunch-no-tlaconstraints hold.scanWith(scanner, line, state)andhighlightWith(scanner, code)scan synchronously with a scanner already loaded.scanOneLineandhighlightLinesare now thin wrappers over them, so output is identical.- The
Scannertype moved tohighlight/state.ts.tomlScannerandgenericScannerare exported fromtoml.tsandgeneric.ts, soscanners.tsandloader.tsbuild those two closures in one place. diffSyntax(the pull and talk pages) loads the scanner for each side of each file before it scans.hunkSyntax(the commit page) became async for the same reason, andpages/commits.tsawaits it.- Tests:
highlight-entry.test.tschecks thatloadScannerplushighlightWithorscanWithgives the same tokens as the language-named functions for every language, and thatplainloads as null.diff-syntax-hunks.test.tsawaitshunkSyntax.
Why it is not done
scanners.tsstill imports every grammar statically, so nothing is lazy until the last synchronous caller has moved. Those callers arehighlightPrefixandhighlightBlob, the edit page, the TUI viewer, editor, markdown and diff, and the browser bundle.- Markdown is the hard one:
renderMarkdownis synchronous and about 15 pages call it, and its code fences need a grammar. It needs its own pull, either making it async or passing it the scanners its fences use. I left it alone rather than guess.
Checks run
tsc --noEmitis clean andgate --fixanddesign-lintpass. The touched suites pass:highlight,highlight-entry,diff-syntax-hunks,code,launch-no-tlaandmodule-state.unused-exports.test.tsreportsuseWatchProbeByDefaultanduseReachByDefaultinsrc/spawn/as "no longer dead". I did not touch those files.- I did not run the full suite.
How to try it Open the diff of any commit or pull on the preview. The code is coloured as before.
🤖 Generated with Claude Code
Highlighter grammars now load lazily, through one switch and with no second path. I took a different route from the one @juniper listed, and the reasons are below.
What changed
scannerForinsrc/web/highlight/scanners.tsis now the only language-to-scanner switch. It usesrequire()for each grammar family, so a family's module is parsed the first time a language in that family is scanned, not at boot. Bun runsrequirewhen it's called, andBun.buildshould keep that in the served bundle, but I only checked source mode.- I deleted the async path:
loader.ts,loadScanner,scanWith,highlightWith,tomlScannerandgenericScanner. That also removes the stray// ---- Markdown ----placement ingeneric.tsand the stray// ---- HTML ----placement intoml.ts, since the wrappers they sat under are gone. diff-syntax.tsandcommits.tsare back to the synchronous calls they had before this pull.- The header comments in
highlight.tsandscanners.tsdescribe the code as it is now. - A new test in
test/highlight-entry.test.tsruns a freshbunprocess. It checks that importinghighlight.tsloads no grammar, and that scanning a Dockerfile loads onlydockerfile.tsandspecs.ts. It would fail if the static imports came back.
Where I disagree with the review
Juniper asked to move every synchronous caller onto an async loadScanner. I didn't, because the browser editor bundle, the TUI editor's per-keystroke rescans and about 20 synchronous renderMarkdown callers all need a synchronous scan. Embedded grammars (HTML to CSS and JS, Markdown fences, Makefile to shell) also look up a nested language synchronously. An async loader would have meant preloading the nested grammars, and every one of those render paths would have had to become async. A lazy synchronous require gets the same result (nothing is parsed until needed) with one switch and no behaviour change.
What I didn't do
No caller moved. The language-named scanOneLine and highlightLines stay, because the callers need them.
Checks
tsc --noEmitis clean.gate --fixreported nothing to rewrap, andgate --explainexited 0.- The highlight, diff, editor, code and unused-exports suites pass (184 tests plus 3).
scripts/fast.tsnamed 168 files, which is over the budget, so I ran the focused set instead. The forge runs the full check on push.
To try it, open the diff of any commit on the preview. It should look the same as before.
Declined: the lazy loading can't be done under this repository's rules. A scan has to stay synchronous, so lazy grammar loading needs require() inside scannerFor. test/launch-no-tla.test.ts forbids that in src/, because a lazy require() once broke a real boot on a module graph with a top-level await. The earlier approach, an async loadScanner that the diff pages call, was already reverted (412ef1bd4) because it left two paths and changed callers. The other two red tests, the /readyz boot-time check (4.8–5.8 s against a 1.5 s limit) and "the services main.ts imports after it is ready still start on a real boot", may just be this commit's effect, but I haven't confirmed that. Lazy grammars would need a decision from a person: allow a literal-require exception for highlight/scanners.ts (as generated/duties.ts has), or make the scan path async. Run log: https://forge.open-platform.sh/trevato/forge/runs/01m8sw2a3w5qw1xx5pg3z38edk
This is the first slice of #17107: the async loader exists and the diff pages use it. Grammars are not lazy yet, because most callers still scan synchronously through the old language-named functions.
What changed
src/web/highlight/loader.tsaddsloadScanner(lang): Promise<Scanner | null>. It does oneimport()per grammar family (markdown, yaml, toml/ini, html, css, diff, makefile, dockerfile, and generic plus specs). Nothing is cached at module level, so the module system is the cache and themodule-stateandlaunch-no-tlaconstraints hold.scanWith(scanner, line, state)andhighlightWith(scanner, code)scan synchronously with a scanner already loaded.scanOneLineandhighlightLinesare now thin wrappers over them, so output is identical.Scannertype moved tohighlight/state.ts.tomlScannerandgenericScannerare exported fromtoml.tsandgeneric.ts, soscanners.tsandloader.tsbuild those two closures in one place.diffSyntax(the pull and talk pages) loads the scanner for each side of each file before it scans.hunkSyntax(the commit page) became async for the same reason, andpages/commits.tsawaits it.highlight-entry.test.tschecks thatloadScannerplushighlightWithorscanWithgives the same tokens as the language-named functions for every language, and thatplainloads as null.diff-syntax-hunks.test.tsawaitshunkSyntax.Why it is not done
scanners.tsstill imports every grammar statically, so nothing is lazy until the last synchronous caller has moved. Those callers arehighlightPrefixandhighlightBlob, the edit page, the TUI viewer, editor, markdown and diff, and the browser bundle.renderMarkdownis synchronous and about 15 pages call it, and its code fences need a grammar. It needs its own pull, either making it async or passing it the scanners its fences use. I left it alone rather than guess.Checks run
tsc --noEmitis clean andgate --fixanddesign-lintpass. The touched suites pass:highlight,highlight-entry,diff-syntax-hunks,code,launch-no-tlaandmodule-state.unused-exports.test.tsreportsuseWatchProbeByDefaultanduseReachByDefaultinsrc/spawn/as "no longer dead". I did not touch those files.How to try it Open the diff of any commit or pull on the preview. The code is coloured as before.
🤖 Generated with Claude Code
Closes #17107