Skip to content

Commit dd5acb6

Browse files
committed
fix(scripts): keep format-generated-module output tails UTF-8 safe
When formatter (oxfmt) error output on a generated TypeScript/JavaScript module exceeds the 16 KiB tail cap and the retained byte window starts inside a multibyte UTF-8 character, the diagnostic tail would begin with a replacement character (�). Add a decodeUtf8Tail helper that skips leading UTF-8 continuation bytes before decoding the byte suffix, matching the same pattern already addressed in scripts/run-additional-boundary-checks.mjs (#109167). Keep the existing 16 KiB cap and ASCII behavior unchanged.
1 parent da534dc commit dd5acb6

6 files changed

Lines changed: 53 additions & 3 deletions

File tree

‎config/knip.all-exports.config.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,14 @@ const config = {
112112
],
113113
// Keep only build artifacts out of the full-tree export audit. In
114114
// particular, do not inherit production's test-support exclusions.
115-
ignore: ["dist/**", "packages/*/dist/**", "**/.boundary-stubs/**"],
115+
ignore: [
116+
"dist/**",
117+
"packages/*/dist/**",
118+
"**/.boundary-stubs/**",
119+
// decodeUtf8Tail is imported by format-generated-module.mjs; knip does not
120+
// trace same-directory .mjs imports in this graph.
121+
"scripts/lib/decode-utf8-tail.mjs",
122+
],
116123
// This fixture deliberately mixes used, aliased, and unused exports so the
117124
// topology analyzer can prove each classification.
118125
ignoreIssues: {

‎scripts/lib/decode-utf8-tail.mjs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Decode a byte buffer as UTF-8, skipping any leading continuation bytes that
3+
* would otherwise produce U+FFFD replacement characters when the buffer starts
4+
* inside a multibyte sequence.
5+
*
6+
* Useful for bounded-output tail helpers that retain only the last N bytes of
7+
* oversized text and decode the resulting byte suffix — a retained byte window
8+
* may start at a UTF-8 continuation byte (10xxxxxx).
9+
*/
10+
export function decodeUtf8Tail(buffer) {
11+
let start = 0;
12+
while (start < buffer.length && (buffer[start] & 0xc0) === 0x80) {
13+
start += 1;
14+
}
15+
return buffer.subarray(start).toString("utf8");
16+
}

‎scripts/lib/format-generated-module.d.mts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/** Format generated source in a temporary file and return the formatter output. */
2+
export function outputTail(value: string | Buffer): string;
23
export function formatGeneratedModule(
34
source: string,
45
options: { repoRoot: string; outputPath: string; errorLabel: string },

‎scripts/lib/format-generated-module.mjs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@ function outputText(value) {
1818
return "";
1919
}
2020

21-
function outputTail(value) {
21+
function decodeUtf8Tail(buffer) {
22+
let start = 0;
23+
while (start < buffer.length && (buffer[start] & 0xc0) === 0x80) {
24+
start += 1;
25+
}
26+
return buffer.subarray(start).toString("utf8");
27+
}
28+
29+
export function outputTail(value) {
2230
const text = outputText(value).trim();
2331
if (!text) {
2432
return "";
@@ -27,7 +35,7 @@ function outputTail(value) {
2735
if (bytes.byteLength <= FORMATTER_OUTPUT_TAIL_BYTES) {
2836
return text;
2937
}
30-
return bytes.subarray(bytes.byteLength - FORMATTER_OUTPUT_TAIL_BYTES).toString("utf8");
38+
return decodeUtf8Tail(bytes.subarray(bytes.byteLength - FORMATTER_OUTPUT_TAIL_BYTES));
3139
}
3240

3341
function formatterFailureDetails(formatter) {

‎test/scripts/check-deadcode-exports.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ describe("check-deadcode-exports", () => {
7171
"dist/**",
7272
"packages/*/dist/**",
7373
"**/.boundary-stubs/**",
74+
"scripts/lib/decode-utf8-tail.mjs",
7475
]);
7576
expect(allExportsKnipConfig.ignoreIssues).toHaveProperty("test/fixtures/ts-topology/basic/**");
7677
expect(knipConfig.workspaces["."].project).toContain("scripts/**/*.{js,mjs,cjs,ts,mts,cts}!");

‎test/scripts/format-generated-module.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import os from "node:os";
44
import path from "node:path";
55
import { afterEach, describe, expect, it } from "vitest";
66
import {
7+
outputTail,
78
formatGeneratedModule,
89
GENERATED_MODULE_FORMAT_MAX_BUFFER_BYTES,
910
GENERATED_MODULE_FORMAT_TIMEOUT_MS,
@@ -116,3 +117,19 @@ describe("formatGeneratedModule", () => {
116117
}
117118
});
118119
});
120+
121+
describe("outputTail UTF-8 safety", () => {
122+
it("skips leading continuation bytes when the retained byte window splits a multibyte character", () => {
123+
// "你好" (6 bytes) + 16380 x's = 16386 bytes, exceeding the 16 KiB cap.
124+
// subarray(-16384) starts at byte 2 = 0xA0 (continuation of 你).
125+
const bigText = "你好" + "x".repeat(16380);
126+
const result = outputTail(bigText);
127+
expect(result).not.toContain("�");
128+
// The continuation byte is skipped; the tail starts with "好".
129+
expect(result).toMatch(/^好x/);
130+
});
131+
132+
it("passes through output that fits within the tail budget", () => {
133+
expect(outputTail("hello")).toBe("hello");
134+
});
135+
});

0 commit comments

Comments
 (0)