fix: load WASM grammars sequentially to avoid Node 20+ race condition

web-tree-sitter has a known race condition when loading multiple WASM
grammars concurrently on Node.js 19+ (V8 10.8+). External scanner
symbols from one grammar can overwrite another's GOT entries, causing
"bad export type" errors for TypeScript, TSX, and other languages.

Replace Promise.allSettled(entries.map(...)) with a sequential for...of
loop so each grammar fully initializes before the next one starts.

Ref: https://github.com/tree-sitter/tree-sitter/issues/2338
This commit is contained in:
Rafael Vescovi
2026-02-17 10:33:39 -06:00
parent a9148e674e
commit 267c9e1379
+13 -14
View File
@@ -86,21 +86,20 @@ export async function initGrammars(): Promise<void> {
await Parser.init();
// Load all grammars in parallel
// Load grammars sequentially to avoid web-tree-sitter WASM race condition on Node 20+
// See: https://github.com/tree-sitter/tree-sitter/issues/2338
const entries = Object.entries(WASM_GRAMMAR_FILES) as [GrammarLanguage, string][];
await Promise.allSettled(
entries.map(async ([lang, wasmFile]) => {
try {
const wasmPath = require.resolve(`tree-sitter-wasms/out/${wasmFile}`);
const language = await WasmLanguage.load(wasmPath);
languageCache.set(lang, language);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.warn(`[CodeGraph] Failed to load ${lang} grammar — parsing will be unavailable: ${message}`);
unavailableGrammarErrors.set(lang, message);
}
})
);
for (const [lang, wasmFile] of entries) {
try {
const wasmPath = require.resolve(`tree-sitter-wasms/out/${wasmFile}`);
const language = await WasmLanguage.load(wasmPath);
languageCache.set(lang, language);
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.warn(`[CodeGraph] Failed to load ${lang} grammar — parsing will be unavailable: ${message}`);
unavailableGrammarErrors.set(lang, message);
}
}
grammarsInitialized = true;
}