-
Notifications
You must be signed in to change notification settings - Fork 94
fix(graph): stop graph-on-stop hook crashing when tree-sitter is absent #321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+215
−11
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { describe, it, expect } from "vitest"; | ||
| import { existsSync, readFileSync, readdirSync } from "node:fs"; | ||
| import { join } from "node:path"; | ||
|
|
||
| /** | ||
| * Bundle-level guard for the graph-on-stop tree-sitter regression. | ||
| * | ||
| * The bug: graph-on-stop's build path statically imports `tree-sitter` (a | ||
| * native optionalDependency). When esbuild bundles that chain into the entry, | ||
| * it hoists `import ... from "tree-sitter"` to the TOP of the entry file, so | ||
| * Node resolves it at MODULE LOAD — before main()'s try/catch runs. On an | ||
| * install without the grammar, the Stop hook exits 1 (ERR_MODULE_NOT_FOUND). | ||
| * | ||
| * The fix builds graph-on-stop as its own `splitting: true` bundle so the | ||
| * `await import("../commands/graph.js")` stays a runtime chunk load and the | ||
| * tree-sitter statics live only in graph-chunks/. These assertions FAIL if | ||
| * splitting is dropped or the tree-sitter chain leaks back into the entry — | ||
| * the exact regression the unit test (which injects runBuildCommand) can't see. | ||
| * | ||
| * Covers the four harnesses that register graph-on-stop as a Stop/SessionEnd | ||
| * hook. OpenClaw ships graph-on-stop via a separate graph-worker build with | ||
| * its own env-rewrite handling and is out of scope here. | ||
| */ | ||
| const REPO_ROOT = join(__dirname, "..", "..", ".."); | ||
| const HARNESSES = ["claude-code", "codex", "cursor", "hermes"]; | ||
|
|
||
| describe("graph-on-stop shipped bundle (tree-sitter isolation)", () => { | ||
| const built = HARNESSES.map((h) => ({ | ||
| harness: h, | ||
| entry: join(REPO_ROOT, "harnesses", h, "bundle", "graph-on-stop.js"), | ||
| chunkDir: join(REPO_ROOT, "harnesses", h, "bundle", "graph-chunks"), | ||
| })).filter((b) => existsSync(b.entry)); | ||
|
|
||
| it("has every harness bundle built (none silently missing)", () => { | ||
| // Require ALL four harnesses, not just one: a filtered subset would let a | ||
| // per-harness packaging regression (or a build that skipped a harness) | ||
| // pass unnoticed. Asserting the exact set also guards against the glob | ||
| // matching nothing after a path drift. Run `npm run build` first if this | ||
| // trips — bundles are gitignored. | ||
| expect(built.map((b) => b.harness)).toEqual(HARNESSES); | ||
| }); | ||
|
|
||
| for (const b of built) { | ||
| describe(b.harness, () => { | ||
| const entrySrc = readFileSync(b.entry, "utf8"); | ||
|
|
||
| it("entry does NOT statically import tree-sitter", () => { | ||
| // Any `from "tree-sitter..."` in the entry means the native import was | ||
| // hoisted to module top → the exact load-time crash we fixed. | ||
| expect(entrySrc).not.toMatch(/from\s*["']tree-sitter/); | ||
| }); | ||
|
|
||
| it("entry loads the build path via a lazy graph-chunks/ import", () => { | ||
| expect(entrySrc).toMatch(/import\(\s*["']\.\/graph-chunks\/graph-[^"']+\.js["']\s*\)/); | ||
| }); | ||
|
|
||
| it("tree-sitter lives only in the lazy graph chunk", () => { | ||
| expect(existsSync(b.chunkDir)).toBe(true); | ||
| const chunks = readdirSync(b.chunkDir).filter((f) => f.endsWith(".js")); | ||
| const withTreeSitter = chunks.filter((f) => | ||
| /from\s*["']tree-sitter/.test(readFileSync(join(b.chunkDir, f), "utf8")), | ||
| ); | ||
| // At least one chunk must carry the grammar imports (proves they were | ||
| // split out, not dropped), and the entry proved they're not inline. | ||
| expect(withTreeSitter.length).toBeGreaterThan(0); | ||
| }); | ||
| }); | ||
| } | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Detect side-effect static imports too.
The regex misses
import "tree-sitter"syntax, allowing the original module-load failure to regress while this test passes.Proposed fix
Also applies to: 57-62
🤖 Prompt for AI Agents