Skip to content

Decode malformed UTF-8 before parsing source files - #65721

Open
benjibun (benjibunbun) wants to merge 1 commit into
microsoft:mainfrom
benjibunbun:fix/65637-malformed-utf8
Open

benjibun (benjibunbun) wants to merge 1 commit into
microsoft:mainfrom
benjibunbun:fix/65637-malformed-utf8

Conversation

@benjibunbun

@benjibunbun benjibun (benjibunbun) commented Nov 28, 2026 •

Copy link
Copy Markdown

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. Calling getText() 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/text dependency, 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 createProgram and 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 --all
  • npx hereby tidy
  • npx hereby check:format

There 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.

Copilot AI balanced review requested due to automatic review settings November 28, 2026 18:49
@typescript-automation typescript-automation Bot added the For Backlog Bug PRs that fix a backlog bug label Nov 28, 2026
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Nov 28, 2026
@jakebailey

Copy link
Copy Markdown
Member

an agent-assisted search

An agent-assisted search of what?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@benjibunbun

Copy link
Copy Markdown
Author

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

For Backlog Bug PRs that fix a backlog bug

Projects

Status: Not started

Development

Successfully merging this pull request may close these issues.

Malformed UTF-8 causes indices to be out of sync with the source text

3 participants