Repository navigation
Decode malformed UTF-8 before parsing source files - #65721
benjibun (benjibunbun) wants to merge 1 commit into
Conversation
An agent-assisted search of what? |
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the UTF-8 decoding and source-position alignment changes.
No open issues 🎉
What changed in this PR
Fixes #65637 by decoding malformed UTF-8 before parsing, so AST positions align with the source text returned by the JavaScript API.
Changes:
- Adds JavaScript-compatible replacement decoding while preserving valid UTF-8 and existing BOM/UTF-16 handling.
- Adds filesystem and sync/async API regression tests for decoding and source positions.
- Updates regex baselines to contain decoded replacement characters.
| File | Description |
|---|---|
tsc/testdata/baselines/reference/compiler/regexInvalidUtf8WithUnicodeFlag.types |
Updates decoded text in the type baseline. |
tsc/testdata/baselines/reference/compiler/regexInvalidUtf8WithUnicodeFlag.symbols |
Updates decoded text in the symbol baseline. |
tsc/testdata/baselines/reference/compiler/regexInvalidUtf8WithUnicodeFlag.js |
Updates source and emitted JavaScript baseline text. |
tsc/internal/vfs/vfstest/vfstest_test.go |
Tests malformed UTF-8 boundaries and Unicode/BOM handling. |
tsc/internal/vfs/internal/internal.go |
Decodes malformed UTF-8 during file reads. |
packages/typescript/test/sync/api.test.ts |
Tests synchronous API positions and escaped surrogate preservation. |
packages/typescript/test/async/api.test.ts |
Adds equivalent asynchronous API regression coverage. |
Review effort: Balanced
|
Oops sorry I meant a search through open GitHub issues across projects. That only explained the interaction that led me to this issue. But honestly I chose it simply because I wanted to take on a more challenging contribution where I could make a useful improvement and properly verify the fix. This issue seemed like a good fit because it had a concrete correctness problem and a clear way to test it. I've updated the PR description to make that clearer. |
Fixes #65637
Codex (GPT-6, reasoning setting unavailable) assisted with implementation, testing and review, and helped co-author this PR.
Motivation & Guidance
Motivation: I wanted a more challenging contribution with a clear way to test the fix. Codex helped me search open GitHub issues across projects and I chose this one.
Guidance: Fix the underlying cause and keep the change small, with tests for both the reported failure and behaviour that should stay the same.
Analysis
The native file reader passes malformed UTF-8 bytes through to the parser. When the API returns the source text to JavaScript, incomplete sequences are decoded as replacement characters. The position calculation counts those invalid bytes differently, so the node positions no longer line up with the returned text.
In the reported example, the
+is at position 23 in the source text, but its token reports position 25. CallinggetText()then returns"2"instead of"+".Fix
Decode malformed UTF-8 when reading the file, before it reaches the parser. This gives the parser and JavaScript API the same source text to work with.
The fix uses the existing
golang.org/x/textdependency, whose decoder handles incomplete sequences the same way as JavaScript. Replacing each invalid byte separately would still produce the wrong number of replacement characters. Valid UTF-8 keeps the original string without an extra copy, and the existing BOM and UTF-16 handling is preserved.The new sync and async tests cover both
createProgramand direct-file loading. They reproduce the original position mismatch before the fix and pass afterward. Additional cases cover malformed sequence boundaries, valid Unicode, BOM/UTF-16 input and escaped lone-surrogate values. Three existing regex baselines now contain decoded replacement characters, matching TypeScript 6.0.3.Validation passed:
npx hereby validate --allnpx hereby tidynpx hereby check:formatThere was no generated or dependency drift. Focused race tests also passed, and a separate check matched VFS output against Node's UTF-8 decoding for 81,600 inputs.
These checks ran on macOS arm64, with existing test skips retained. The full run used
GOFLAGS="-gcflags=all=-dwarf=false -p=2"to reduce debug metadata and concurrent compilation under disk pressure. Hosted cross-platform CI has not run.The added UTF-8 scan has a measurable cost. Paired benchmarks for a roughly 3 MB file showed about 0.12 ms (14%) more time for an in-memory read, with unchanged allocations. Disk-read timings were noisy.