Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.
TypeScript ai-agentsanthropicclaude-codeclicode-graphcodexcontext-engineeringcursordeveloper-toolsgeminiknowledge-graphllmmcpmcp-serveropen-sourceopenaitree-sitter
fix(generic): surface a throwing grammar as a build error instead of swallowing it (#140)
Matic Korošecebb5fe4a90c7
diff --git a/src/graph/generic.ts b/src/graph/generic.tsindex 85583974..b599deb7 100644--- a/src/graph/generic.ts+++ b/src/graph/generic.ts@@ -85,7 +85,7 @@ const KIND: Record<string, Kind> = {// Loaded grammars + compiled tags queries, keyed by graft lang name. Populated by// warmGenericGrammars; read synchronously by extractGeneric.-interface Loaded { language: unknown; query: unknown | null }+export interface Loaded { language: unknown; query: unknown | null }const loaded = new Map<string, Loaded>();let tsMod: typeof import("web-tree-sitter") | null = null;let initPromise: Promise<void> | null = null;@@ -153,6 +153,18 @@ export function isWarm(langName: string): boolean {return loaded.has(langName);}+/** Test seam: swap in (or, with null, remove) a grammar entry, returning the+ * previous one so the caller can restore it. The parse-failure path in+ * extractGeneric needs a grammar that throws deterministically; the real crash+ * that motivated it (tree-sitter-wasm PHP on heredocs, #139) is heap-state+ * dependent, so tests inject a fake here instead of depending on it. */+export function swapGrammarForTest(name: string, entry: Loaded | null): Loaded | null {+ const prev = loaded.get(name) ?? null;+ if (entry) loaded.set(name, entry);+ else loaded.delete(name);+ return prev;+}+/** Load one grammar from the tree-sitter-wasms bundle, initialising* web-tree-sitter on first call. Null when the wasm is missing or won't* instantiate — never throws, so a caller degrades instead of failing the build.@@ -218,8 +230,13 @@ export function extractGeneric(rel: string, source: string, langName: string): Econst parser = new tsMod.Parser();parser.setLanguage(entry.language as never);tree = parser.parse((i: number) => source.slice(i, i + PARSE_CHUNK));- } catch {- return { nodes, rawEdges };+ } catch (err) {+ // A grammar that throws (e.g. a WASM "memory access out of bounds" from an+ // external scanner) is not a per-file syntax problem: swallowing it here left+ // the file with only its file node and the build reporting success. Rethrow so+ // build.ts records it in `errors`, the CLI prints it, and the extract cache+ // remembers the failure instead of caching an empty result as clean.+ throw new Error(`${langName} grammar threw: ${err instanceof Error ? err.message : String(err)}`);}if (!tree) return { nodes, rawEdges };diff --git a/test/generic-extract.test.ts b/test/generic-extract.test.tsindex 5f85c96e..aa749826 100644--- a/test/generic-extract.test.ts+++ b/test/generic-extract.test.ts@@ -16,6 +16,7 @@ import {isWarm,loadWasmLanguage,parseWasm,+ swapGrammarForTest,type TsNode,} from "../src/graph/generic.js";import { resolveEdges } from "../src/graph/resolve.js";@@ -412,3 +413,61 @@ test("PHP wasm grammar extracts class + methods from a heredoc file (#139)", asytest("PHP wasm grammar extracts class + methods from a nowdoc file (#139)", async () => {await assertPhpWasmExtractsClassAndMethods(PHP_NOWDOC, "WithNowdoc", "nowdoc");});++/**+ * A grammar whose parse throws (the tree-sitter-wasm PHP grammar did, on+ * heredocs — #139) must surface as a per-file build error, not be swallowed+ * into a silently symbol-less file that the extract cache then replays as a+ * clean parse. The real crash is heap-state dependent, so a deterministically+ * throwing fake grammar is swapped in via the test seam instead.+ */+const THROWING_GRAMMAR = {+ // web-tree-sitter's setLanguage() inspects the language object, so any+ // property access blowing up makes extractGeneric's try block throw exactly+ // like a crashing wasm scanner does.+ language: new Proxy({}, { get(): never { throw new Error("memory access out of bounds (fake)"); } }),+ query: null,+};++test("extractGeneric rethrows a throwing grammar with the language named (#139)", async () => {+ await warmGenericGrammars(["rust"]); // initialises web-tree-sitter+ const prev = swapGrammarForTest("rust", THROWING_GRAMMAR);+ try {+ assert.throws(+ () => extractGeneric("src/lib.rs", "pub fn f() {}\n", "rust"),+ (err: unknown): boolean => {+ assert.ok(err instanceof Error, "throws an Error");+ assert.match(err.message, /^rust grammar threw: /, "names the language");+ assert.match(err.message, /memory access out of bounds/, "keeps the original message");+ return true;+ },+ );+ } finally {+ swapGrammarForTest("rust", prev);+ }+});++test("a throwing grammar is a per-file build error, cached as a failure (#139)", async () => {+ const dir = mkdtempSync(join(tmpdir(), "graft-throwing-grammar-"));+ writeFileSync(join(dir, "lib.rs"), "pub fn f() {}\n");+ await warmGenericGrammars(["rust"]); // so buildGraph's own warm call is a no-op+ const prev = swapGrammarForTest("rust", THROWING_GRAMMAR);+ try {+ const first = await buildGraph(dir, { reuse: false });+ assert.equal(first.errors.length, 1, `one build error (got: ${first.errors.join("; ")})`);+ assert.match(first.errors[0], /lib\.rs: parse failed/);+ assert.match(first.errors[0], /rust grammar threw: memory access out of bounds/);+ const g = readGraph(wiringPath(contextDirFor(dir)));+ assert.ok(g, "graph built");+ assert.ok(!g!.nodes.some((n) => n.path === "lib.rs"), "failed file has no file node");++ // The extract cache must remember the failure, not an empty success: an+ // incremental rebuild of the unchanged file replays the error.+ const second = await buildGraph(dir, { reuse: true });+ assert.equal(second.parsed, 0, "unchanged file is not re-parsed");+ assert.equal(second.errors.length, 1, `error replayed (got: ${second.errors.join("; ")})`);+ assert.match(second.errors[0], /rust grammar threw/);+ } finally {+ swapGrammarForTest("rust", prev);+ }+});