fix(web): load file grammar before enabling edits #9947
main fromrefs/pull/9947/head·merged by juliusmarminge View on GitHubOpening a worker-highlighted TSX file could leave the editor's main-thread highlighter without its grammar. The first edit then threw Grammar for language "tsx" not loaded, after emitting the changed contents but before completing the incremental render.
Move the existing file-language assignment before the worker branch in FileRenderer.hydrate and FileRenderer.renderFile. The editor already awaits highlighter initialization before enabling edits; that initialization now loads the actual file language. Read-only worker highlighting still does not load editor grammars on the main thread.
Native before and after#
Both runs used a fresh Linux Chromium client, the same 8,323-line TSX fixture, and the app at f47a3fe9. The after run changed only this grammar patch. The existing stale-highlight fix was present in both; neither run included the separate viewport candidate. Read-only inspection captured actual editor state, without replacing methods or mocking the highlighter.
| At the first native Enter | Before | After |
|---|---|---|
| Worker ready | true | true |
| Renderer language | text | tsx |
| Main-thread loaded languages | [] | ["tsx"] |
| New page errors | 1, missing TSX grammar | 0 |
The after run also completed native undo/redo without new exceptions. A typed marker persisted on disk after closing the file and remained visible on reopening it.
After save and reopen, showing persistence rather than a layout fix:

The first-edit screenshots look alike because the separate viewport jump remains. The runtime observations above and the regression tests establish the grammar repair; the screenshot and clip establish the saved-file result.
Verification#
vp test run apps/web/src/components/files/fileEditorLanguageReadiness.test.ts apps/web/src/components/files/fileEditorHighlight.test.ts apps/web/src/components/files/fileContentRevision.test.ts apps/web/src/components/files/FilePreviewPanel.test.ts --maxWorkers=2
The tests use the installed Pierre worker and actual renderer, tokenizer and document classes. Worker-highlighted cases await the completed highlight, then apply the first edit without waiting for the tokenizer's delayed prebuild. On the unpatched main implementation at c7dc3cbd, six readiness cases fail with the exact missing-grammar exception and plain text passes. After the fix, all seven readiness cases pass, covering both rendering entry points, explicit language overrides, language switches and non-worker hydration.
All 26 focused tests, targeted lint and web typecheck pass on 61d8322a, refreshed onto 7a089b2b. The intervening main changes do not touch the editor. Independent review and reruns of the unchanged fix also pass.
This addresses a separate cold-language failure found while investigating #7907. It does not include the wrapped-row height-cache change or fix intermittent caret positioning on redo. Keep that report open.
GPT 6 Astra via Codex in T3 Code.
Note
Medium Risk Changes vendored renderer/highlighter lifecycle and worker cache eviction on edit; behavior is covered by new integration tests but touches core editing and highlighting paths.
Overview
Fixes a crash on the first edit of worker-highlighted files (e.g. TSX) where the main-thread highlighter had not loaded the file’s grammar (Grammar for language "tsx" not loaded), even though read-only highlighting already ran in a worker.
The @pierre/diffs patch moves computedLang (explicit file.lang or inferred from the filename) to run before the worker vs main-thread branches in FileRenderer.hydrate and renderFile, so initializeHighlighter() (already awaited before edits) loads the correct language instead of staying on plain text. Read-only worker AST highlighting still does not pull editor grammars onto the main thread until the editor initializes. Incremental tokenization also cleans worker tasks and evicts the file highlight cache when applying dirty lines, and default editor options in the patch drop forced gutter/selection overrides while keeping useTokenTransformer: true.
Adds fileEditorLanguageReadiness.test.ts, which drives the real Pierre worker (via a Node worker transport), FileRenderer, tokenizer, and a synchronous “first Enter” edit to lock in hydrate/render paths, explicit language overrides, language switches, non-worker hydration, and plain-text files. pnpm-lock.yaml updates the patched @pierre/diffs hash.
Note
Compute file language before worker branch in FileRenderer.hydrate and renderFile#
- Moves the explicit-or-filename-inferred language computation ahead of the worker-manager branch in both
FileRenderer.hydrateandFileRenderer.renderFile, so worker-backed rendering records the correct language before highlighting - Previously the language was only set in the local-highlighter path, so the first edit on a worker-backed file could lack the right grammar
- Adds a Node
WorkerTransportadapter and a suite of readiness tests in fileEditorLanguageReadiness.test.ts covering worker-backed hydrate/renderFile, explicit language, renderer reuse, and plain-text edits - Risk:
renderFilenow updatescomputedLangfor every rendered file rather than only local-highlighted files; consumers relying oncomputedLangbeing unset for worker-backed renders should verify behavior
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
Approvability#
Verdict: Approved at
61d8322Macroscope's review found this PR approvable — This is a narrowly scoped renderer bug fix that ensures worker-highlighted files initialize the correct grammar before editing. The runtime change is localized and backed by integration coverage for the affected rendering paths and language cases.
Notes:
- No code objects were reviewed. Approvability was decided on eligibility alone.
juliusmarminge merged this2fa5ef4
juliusmarminge closed this
Sign in to join this conversation.
Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Baseline:
7a089b2· PR result:61d8322· Source CI: successScenario and decoded snapshot size
10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.
Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.