Skip to content

Preserve U+FEFF when Wtf8Decoder reads payload text - #65579

Closed
5 (tydavid5) wants to merge 2 commits into
microsoft:mainfrom
tydavid5:fix/wtf8decoder-preserve-feff-65458
Closed

5 (tydavid5) wants to merge 2 commits into
microsoft:mainfrom
tydavid5:fix/wtf8decoder-preserve-feff-65458

Conversation

@tydavid5

Copy link
Copy Markdown

Fixes #65458

Wtf8Decoder extends TextDecoder without ignoreBOM, so the underlying decoder drops U+FEFF at the start of every segment it decodes. A string or template value that starts with U+FEFF, or that has U+FEFF immediately after a lone surrogate, came back from the API without that character, while a U+FEFF in the middle of a segment survived. The constructor now defaults ignoreBOM to true, so every payload decoder built from this class keeps the character, and an explicit ignoreBOM: false still opts out. The WASM transport's explicit option is now redundant, so it just constructs the decoder with no arguments.

This cannot change how a real file-level BOM is handled. The Go filesystem reader already strips a UTF-8 BOM from source text before the compiler sees the file, and the client decoder only ever sees payload strings from the server's string table, never raw file bytes. The new tests cover the reported cases plus a trailing U+FEFF and the explicit opt-out.

Verification: the issue repro gave leading [78], only [], afterSurrogate [d800] and template [74, 61, 69, 6c] before the fix, and all five give the expected code points after. The new tests pass, and with the fix reverted they fail with actual [120], expected [65279, 120]. Fifteen edge cases were checked by hand, including U+FEFF after a lone surrogate, a trailing U+FEFF, streaming decode, and the explicit opt-out.

Fixes microsoft#65458

Wtf8Decoder extends TextDecoder without ignoreBOM, so the underlying decoder drops U+FEFF at the start of every segment it decodes. A string or template value that starts with U+FEFF, or that has U+FEFF immediately after a lone surrogate, came back from the API without that character, while a U+FEFF in the middle of a segment survived. The constructor now defaults ignoreBOM to true, so every payload decoder built from this class keeps the character, and an explicit ignoreBOM: false still opts out. The WASM transport's explicit option is now redundant, so it just constructs the decoder with no arguments.

This cannot change how a real file-level BOM is handled. The Go filesystem reader already strips a UTF-8 BOM from source text before the compiler sees the file, and the client decoder only ever sees payload strings from the server's string table, never raw file bytes. The new tests cover the reported cases plus a trailing U+FEFF and the explicit opt-out.

Verification: the issue repro gave leading [78], only [], afterSurrogate [d800] and template [74, 61, 69, 6c] before the fix, and all five give the expected code points after. The new tests pass, and with the fix reverted they fail with actual [120], expected [65279, 120]. Fifteen edge cases were checked by hand, including U+FEFF after a lone surrogate, a trailing U+FEFF, streaming decode, and the explicit opt-out.
Copilot AI balanced review requested due to automatic review settings November 19, 2026 11:56
@github-project-automation github-project-automation Bot moved this to Not started in PR Backlog Nov 19, 2026
@typescript-automation typescript-automation Bot added the For Backlog Bug PRs that fix a backlog bug label Nov 19, 2026

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.

🟡 Changes recommended

The new signature references the unavailable global TextDecoderOptions, causing type-checking to fail under the package’s ES2022 and Node-only configuration.

1 open issue
What changed in this PR

Addresses #65458 by preserving U+FEFF in API payload strings, including literal and template values and text following lone surrogates.

Changes:

  • Defaults Wtf8Decoder to preserve U+FEFF while retaining explicit ignoreBOM: false behavior.
  • Simplifies the WASM transport to use the new default.
  • Adds regression tests for U+FEFF positions and explicit opt-out.
File Description
packages/​typescript/​test/​wtf8.test.ts Tests U+FEFF preservation and opt-out behavior.
packages/​typescript/​src/​wasm/​transport.ts Removes the now-redundant decoder option.
packages/​typescript/​src/​api/​node/​wtf8.ts Adds constructor defaults that preserve payload U+FEFF.

Review effort: Balanced

}

export class Wtf8Decoder extends TextDecoder {
constructor(label: string | undefined = "utf-8", options?: TextDecoderOptions) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed. The constructor no longer names the TextDecoderOptions global. It now takes ConstructorParameters[1], which resolves under the package's ES2022 and Node-only configuration and keeps the same accepted options, including the explicit ignoreBOM: false opt-out. Verified by running the package type-check: it fails on the old signature with TS2552 Cannot find name TextDecoderOptions and passes with this change. The wtf8 tests still pass, 9 of 9.

The constructor named the TextDecoderOptions global, which does not exist under this package's ES2022 and Node-only configuration, so type-checking the package failed with Cannot find name TextDecoderOptions. The options type is now taken from the constructor itself, which keeps the same accepted options and the explicit ignoreBOM false opt-out.

Fixes the Copilot review on microsoft#65579. Verified by running the package type-check: it fails on the old signature with TS2552 and passes with this change. The wtf8 tests still pass, 9 of 9.
@github-project-automation github-project-automation Bot moved this from Not started to Done in PR Backlog Nov 19, 2026
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: Done

Development

Successfully merging this pull request may close these issues.

Native API: Wtf8Decoder drops U+FEFF from string literals and template text

3 participants