fix(reference): report malformed UTF-8 before import parsing

Signed-off-by: Rudy Celekli <47457359+rudycelekli@users.noreply.github.com>
This commit is contained in:
Rudy Celekli
2026-10-10 01:45:00 +08:00
committed by morluto
parent 9055bd695b
commit f166a2ae15
3 changed files with 100 additions and 1 deletions
+19 -1
View File
@@ -331,7 +331,25 @@ export const parseReferenceSourceImports = (
if (language !== null && !codeSourceLanguages.has(language))
return { relationships: [], parse_failures: [] };
const source = new TextDecoder("utf-8", { fatal: false }).decode(bytes);
let source: string;
try {
source = new TextDecoder("utf-8", { fatal: true }).decode(bytes);
} catch (cause: unknown) {
// The import still records the original bytes; lossy text cannot supply
// observed module names or relationships.
void cause;
return {
relationships: [],
parse_failures: [
{
path,
parser: "utf-8",
reason:
"Source bytes are not valid UTF-8; import targets were not parsed.",
},
],
};
}
const { ast, reasons } = parseWithBabel(path, source, language);
const parseFailures = reasons.map((reason) => ({
path,