Fix PreToolUse hook CLI path resolution after gitnexus setup#109
Fix PreToolUse hook CLI path resolution after gitnexus setup#109atul-trycoral wants to merge 1 commit into
gitnexus setup#109Conversation
…setup` `gitnexus setup` copies the hook to ~/.claude/hooks/gitnexus/, but the hook used a relative path (../../dist/cli/index.js) that only works when running from inside the npm package directory. After setup, the relative path resolves to ~/.claude/dist/cli/index.js which doesn't exist, causing "Cannot find module" errors on every Grep/Glob/Bash call in Claude Code. Fix: try the relative path first (backward compatible), then fall back to dynamic CLI discovery via `which gitnexus` or `npm root -g`. Works cross-platform (macOS/Linux/Windows). Fixes abhigyanpatwari#108 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
@atul-trycoral is attempting to deploy a commit to the NexusCore Team on Vercel. A member of the Team first needs to authorize it. |
abhigyanpatwari
left a comment
There was a problem hiding this comment.
Thanks for tackling this — the approach is right (try relative, fall back to discovery), but I found a few cross-platform issues that would make this silently fail on Windows and non-Homebrew installs.
1. CRLF in where output (Windows)
where outputs \r\n line endings. .trim().split('\n')[0] leaves a trailing \r in the path, so realpathSync fails with ENOENT.
Fix: .split(/\r?\n/)[0]
2. realpathSync on shell script wrappers
which/where returns the shell wrapper, not the JS entry point. realpathSync just gives back the shell script path, then node <shell-script> augment ... fails.
This only worked in your testing because Homebrew creates symlinks that resolve to the actual .js file. Standard npm -g installs create shell wrappers instead.
Better approach — derive from the binary's directory:
const binDir = path.dirname(binPath.replace(/\r/g, ''));
cliPath = path.join(binDir, 'node_modules', 'gitnexus', 'dist', 'cli', 'index.js');3. execFileSync('npm', ...) on Windows
npm is a .cmd wrapper on Windows. execFileSync without { shell: true } can't run .cmd files — fails with ENOENT/EINVAL.
4. Minor: execFileSync is already imported at line 14, no need to re-require it as efs on line 111.
I verified all of this on Windows with a global npm install. Net result right now: all fallback paths fail on Windows, and the which path fails on Linux with standard npm global installs. The relative path (in-package) still works fine, so this doesn't regress anything — it just doesn't fix the issue on those platforms.
Happy to help with a corrected version if you'd like.
|
|
Please submit a new PR if this is still relevant |
Summary
Cannot find moduleerror that occurs on every Grep/Glob/Bash call in Claude Code after runninggitnexus setup../../dist/cli/index.js) that only works inside the npm package, not after being copied to~/.claude/hooks/which/whereandnpm root -gTest plan
whereinstead ofwhich)Fixes #108
🤖 Generated with Claude Code