Skip to content

Fix PreToolUse hook CLI path resolution after gitnexus setup#109

Closed
atul-trycoral wants to merge 1 commit into
abhigyanpatwari:mainfrom
atul-trycoral:fix/hook-cli-path-resolution
Closed

Fix PreToolUse hook CLI path resolution after gitnexus setup#109
atul-trycoral wants to merge 1 commit into
abhigyanpatwari:mainfrom
atul-trycoral:fix/hook-cli-path-resolution

Conversation

@atul-trycoral

Copy link
Copy Markdown

Summary

  • Fixes the Cannot find module error that occurs on every Grep/Glob/Bash call in Claude Code after running gitnexus setup
  • The hook was using a relative path (../../dist/cli/index.js) that only works inside the npm package, not after being copied to ~/.claude/hooks/
  • Now tries the relative path first (backward compatible), then falls back to dynamic CLI discovery via which/where and npm root -g

Test plan

  • Verified fix works on macOS with global npm install
  • Test on Linux with global npm install
  • Test on Windows (uses where instead of which)
  • Test that relative path still works when hook is run from inside the package

Fixes #108

🤖 Generated with Claude Code

…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>
@vercel

vercel Bot commented Feb 27, 2026

Copy link
Copy Markdown

@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 abhigyanpatwari left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@magyargergo

Copy link
Copy Markdown
Collaborator

⚠️ Upcoming Prettier formatting — rebase instructions

PR #563 adds Prettier as the code formatter for the repo. When it merges, the bulk format commit will touch ~350 files (style-only: whitespace, quotes, trailing commas). Your branch will likely conflict.

After #563 merges, rebase your branch:

git fetch origin
git checkout <your-branch>
git rebase origin/main

# Conflicts will be formatting-only — accept your version:
git checkout --theirs .
git add .
git rebase --continue

# Then re-format your branch to match the new style:
npx prettier --write .
git add -A
git commit -m "style: apply prettier formatting"
git push --force-with-lease

New setup step: Run npm install at the repo root (not just in gitnexus/) to get prettier + activate the pre-commit hook. The hook auto-formats staged files on every commit going forward.

@magyargergo

Copy link
Copy Markdown
Collaborator

Please submit a new PR if this is still relevant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PreToolUse hook breaks when installed via gitnexus setup (global npm install)

3 participants