Skip to content

Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.

fix(claude): resolve graft CLI/scripts from the module, not the project dir

Shrish Dwivedi326c71fe47cb
diff --git a/package.json b/package.json
index e3f9f98f..43d611a4 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "@nanonets/graft",
- "version": "0.3.0",
+ "version": "0.3.1",
"description": "Build a repo's context graph as a folder of linked markdown files that live in the repo and stay in sync with the code through git.",
"keywords": [
"ai",
diff --git a/src/claude/hooks.ts b/src/claude/hooks.ts
index ab800382..df868058 100644
--- a/src/claude/hooks.ts
+++ b/src/claude/hooks.ts
@@ -4,6 +4,7 @@ import { join, basename } from 'node:path';
import { readWiring } from './stats.js';
import { formatBlastRadius, formatRetrieval, formatOrientation } from './format.js';
import { patchStats, readStats, acquireLock, readSession, writeSession } from './state.js';
+import { graftCliPath, claudeScriptPath } from './paths.js';
function readStdin(): any {
const seam = process.env.GRAFT_TEST_STDIN;
@@ -21,7 +22,7 @@ export function underGraft(dir: string, file: string): boolean {
}
function graftJson(dir: string, args: string[]): any | null {
try {
- const out = execFileSync(process.execPath, [join(dir, 'dist', 'cli.js'), ...args],
+ const out = execFileSync(process.execPath, [graftCliPath(), ...args],
{ cwd: dir, encoding: 'utf8', timeout: 8000, stdio: ['ignore', 'pipe', 'ignore'] });
return JSON.parse(out);
} catch (e: any) {
@@ -67,7 +68,7 @@ export async function main(event: string): Promise<void> {
const stats = readStats(dir);
if (stats?.dirty && acquireLock(dir)) {
patchStats(dir, { syncing: true });
- const child = spawn(process.execPath, [join(dir, 'dist', 'claude', 'sync-run.js'), dir],
+ const child = spawn(process.execPath, [claudeScriptPath('sync-run.js'), dir],
{ detached: true, stdio: 'ignore' });
child.unref();
}
diff --git a/src/claude/paths.ts b/src/claude/paths.ts
new file mode 100644
index 00000000..e9f73a9f
--- /dev/null
+++ b/src/claude/paths.ts
@@ -0,0 +1,18 @@
+import { fileURLToPath } from 'node:url';
+import { dirname, join } from 'node:path';
+
+// This module ships inside the package at <pkgRoot>/dist/claude/paths.js. Resolving graft's
+// own CLI and sibling scripts relative to THIS file — not the project dir — is what makes the
+// hooks work when graft is installed as a dependency in someone else's repo (where
+// <projectDir>/dist/ does not exist). In graft's own repo it resolves to ./dist all the same.
+const CLAUDE_DIR = dirname(fileURLToPath(import.meta.url));
+
+/** Absolute path to the graft CLI (`<pkgRoot>/dist/cli.js`), resolved from this module. */
+export function graftCliPath(): string {
+ return join(CLAUDE_DIR, '..', 'cli.js');
+}
+
+/** Absolute path to a sibling script in the claude dir (e.g. `sync-run.js`). */
+export function claudeScriptPath(name: string): string {
+ return join(CLAUDE_DIR, name);
+}
diff --git a/src/claude/sync-run.ts b/src/claude/sync-run.ts
index 831ff52b..8c0e6262 100644
--- a/src/claude/sync-run.ts
+++ b/src/claude/sync-run.ts
@@ -1,12 +1,12 @@
import { execFileSync } from 'node:child_process';
-import { join } from 'node:path';
import { pathToFileURL } from 'node:url';
import { readWiring, computeStats } from './stats.js';
import { patchStats, releaseLock } from './state.js';
+import { graftCliPath } from './paths.js';
/** MONEY GUARD: plain `graft build` only — structural, $0, offline. Never --deep. */
function realBuild(dir: string): void {
- execFileSync(process.execPath, [join(dir, 'dist', 'cli.js'), 'build', '.'],
+ execFileSync(process.execPath, [graftCliPath(), 'build', '.'],
{ cwd: dir, stdio: 'ignore', timeout: 120000 });
}
diff --git a/test/claude-paths.test.ts b/test/claude-paths.test.ts
new file mode 100644
index 00000000..9dbe46f9
--- /dev/null
+++ b/test/claude-paths.test.ts
@@ -0,0 +1,23 @@
+import { test } from 'node:test';
+import assert from 'node:assert/strict';
+import { basename, dirname } from 'node:path';
+import { graftCliPath, claudeScriptPath } from '../src/claude/paths.js';
+
+test('graftCliPath is module-relative (cwd-independent) and points at a sibling cli.js', () => {
+ const a = graftCliPath();
+ const orig = process.cwd();
+ try { process.chdir('/'); assert.equal(graftCliPath(), a, 'must not depend on cwd / project dir'); }
+ finally { process.chdir(orig); }
+ assert.match(a, /[/\\]cli\.js$/);
+ // cli.js sits one level above the claude module dir → its parent dir is src (tsx) or dist (built)
+ assert.match(basename(dirname(a)), /^(src|dist)$/);
+});
+
+test('claudeScriptPath resolves a sibling script in the claude dir (cwd-independent)', () => {
+ const a = claudeScriptPath('sync-run.js');
+ const orig = process.cwd();
+ try { process.chdir('/'); assert.equal(claudeScriptPath('sync-run.js'), a); }
+ finally { process.chdir(orig); }
+ assert.match(a, /[/\\]sync-run\.js$/);
+ assert.equal(basename(dirname(a)), 'claude');
+});