Public: anyone can read this; the crew can change it.

Load highlighter grammars lazily behind an async loader (#16804 follow-up) #17332

opened this

Merged 38h 23m 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.

Notes

opened this

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. loadScanner(lang): Promise<Scanner | null> uses one import() per grammar family. The module system is the cache, so no module-level state is added. plain and unknown languages give null.
  • scanWith and highlightWith in highlight/scanners.ts take an already-loaded Scanner, so scans stay synchronous. scanOneLine and highlightLines still take a language name and delegate to them, so every caller not yet migrated is unchanged. Scanner is now a shared type in highlight/state.ts.
  • genericScanner and tomlScanner now return a ready scanner for one spec or dialect. The other scanners are unchanged.
  • src/web/diff-syntax.ts awaits loadScanner for each file's old and new side, then scans with the loaded scanner. src/web/pages/commits.ts is updated to match.
  • Tests: test/highlight-entry.test.ts checks that loadScanner plus scanWith and highlightWith give the same tokens as the synchronous path, and that plain gives null. test/diff-syntax-hunks.test.ts is 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 useWatchProbeByDefault and useReachByDefault are no longer dead. That is unrelated to this change.
  • bun scripts/gate.ts --fix changed nothing. Nothing new was pushed, because the branch was already up to date with origin.

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.json or 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

Closes #17107

3 earlier comments
commented

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. loadScanner(lang): Promise<Scanner | null> uses one import() per grammar family. The module system is the cache, so no module-level state is added. plain and unknown languages give null.
  • scanWith and highlightWith in highlight/scanners.ts take an already-loaded Scanner, so scans stay synchronous. scanOneLine and highlightLines still take a language name and delegate to them, so every caller not yet migrated is unchanged. Scanner is now a shared type in highlight/state.ts.
  • genericScanner and tomlScanner now return a ready scanner for one spec or dialect. The other scanners are unchanged.
  • src/web/diff-syntax.ts awaits loadScanner for each file's old and new side, then scans with the loaded scanner. src/web/pages/commits.ts is updated to match.
  • Tests: test/highlight-entry.test.ts checks that loadScanner plus scanWith and highlightWith give the same tokens as the synchronous path, and that plain gives null. test/diff-syntax-hunks.test.ts is 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 useWatchProbeByDefault and useReachByDefault are no longer dead. That is unrelated to this change.
  • bun scripts/gate.ts --fix changed nothing. Nothing new was pushed, because the branch was already up to date with origin.

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.json or 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

commented

Grammars now load lazily from one table. Pushed; the forge's check hasn't run on it yet.

What changed

  • The loadScanner loader is gone, along with scanWith, highlightWith and the Scanner and tomlScanner helpers. diff-syntax.ts and commits.ts are back to their synchronous form.
  • scannerFor in src/web/highlight/scanners.ts is the only language-to-grammar mapping. It no longer imports any grammar statically. Each family is required the first time its language is scanned, so a process that never scans Dockerfiles never loads the Dockerfile grammar.
  • Bun's require is synchronous, so no caller changes: the TUI viewer and editor, highlightPrefix, the embedded-language scanners and the browser bundle all keep calling highlightLines and scanOneLine. 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.ts and checks that no grammar module is loaded. Reverting to static imports fails it.
  • Scanning dockerfile loads only dockerfile.ts, generic.ts and specs.ts. Scanning yaml loads only yaml.ts. Scanning plain loads nothing.
  • This replaces the old test that only compared the two paths.

Checks I ran

  • tsc is clean.
  • The highlight, diff-syntax, code, blob, assets, web-editor and TUI-editor suites pass.
  • src-imports passes.
  • unused-exports fails, but only on src/spawn/watch_probe_default.ts and src/spawn/reach_default.ts. I didn't touch those files.
  • gate.ts has 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

commented

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

commented

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

Sign in to join this conversation.

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs