Skip to content

Commit 51416bf

Browse files
committed
fix(hooks): anchor hook commands + project paths to $CLAUDE_PROJECT_DIR
Hook commands previously used relative paths like `node .claude/hooks/<name>/index.mts`, which resolved against the session cwd. When cwd drifted into a subpackage, Node couldn't find the hook module and the Stop hook crashed with MODULE_NOT_FOUND. Two changes (where applicable): 1. settings.json: rewrite every hook command from `node .claude/hooks/foo/index.mts` to `node "$CLAUDE_PROJECT_DIR"/.claude/hooks/foo/index.mts`. Claude Code injects $CLAUDE_PROJECT_DIR on every hook run. 2. auth-rotation-reminder + setup-security-tools: replace `process.cwd()` with `process.env.CLAUDE_PROJECT_DIR ?? process.cwd()` for project-local file lookups (.snooze, .env.local). Same root cause — process.cwd() is unreliable, the env var is canonical. Mirrors socket-repo-template@1dbd8e2.
1 parent e5fe38c commit 51416bf

2 files changed

Lines changed: 16 additions & 14 deletions

File tree

.claude/hooks/auth-rotation-reminder/index.mts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,18 @@ const STATE_FILE = path.join(STATE_DIR, 'last-run')
6969
const GLOBAL_SNOOZE = path.join(STATE_DIR, 'snooze')
7070
const GLOBAL_SKIP_LIST = path.join(STATE_DIR, 'services-skip')
7171

72-
// Project-local files live at the repo root next to .claude/. Claude
73-
// Code spawns Stop hooks with the working directory set to the repo
74-
// root so process.cwd() is reliable here.
72+
// Project-local files live at the repo root next to .claude/. Use
73+
// CLAUDE_PROJECT_DIR (Claude Code injects this on every hook run) so
74+
// the paths stay correct regardless of session cwd — process.cwd()
75+
// drifts when the user navigates into a subpackage.
76+
const PROJECT_DIR = process.env['CLAUDE_PROJECT_DIR'] ?? process.cwd()
7577
const PROJECT_SNOOZE = path.join(
76-
process.cwd(),
78+
PROJECT_DIR,
7779
'.claude',
7880
'auth-rotation.snooze',
7981
)
8082
const PROJECT_SKIP_LIST = path.join(
81-
process.cwd(),
83+
PROJECT_DIR,
8284
'.claude',
8385
'auth-rotation.services-skip',
8486
)

.claude/settings.json

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
"hooks": [
77
{
88
"type": "command",
9-
"command": "node .claude/hooks/check-new-deps/index.mts"
9+
"command": "node \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/check-new-deps/index.mts"
1010
},
1111
{
1212
"type": "command",
13-
"command": "node .claude/hooks/logger-guard/index.mts"
13+
"command": "node \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/logger-guard/index.mts"
1414
},
1515
{
1616
"type": "command",
17-
"command": "node .claude/hooks/path-guard/index.mts"
17+
"command": "node \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/path-guard/index.mts"
1818
}
1919
]
2020
},
@@ -23,19 +23,19 @@
2323
"hooks": [
2424
{
2525
"type": "command",
26-
"command": "node .claude/hooks/private-name-guard/index.mts"
26+
"command": "node \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/private-name-guard/index.mts"
2727
},
2828
{
2929
"type": "command",
30-
"command": "node .claude/hooks/public-surface-reminder/index.mts"
30+
"command": "node \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/public-surface-reminder/index.mts"
3131
},
3232
{
3333
"type": "command",
34-
"command": "node .claude/hooks/release-workflow-guard/index.mts"
34+
"command": "node \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/release-workflow-guard/index.mts"
3535
},
3636
{
3737
"type": "command",
38-
"command": "node .claude/hooks/token-guard/index.mts"
38+
"command": "node \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/token-guard/index.mts"
3939
}
4040
]
4141
}
@@ -45,11 +45,11 @@
4545
"hooks": [
4646
{
4747
"type": "command",
48-
"command": "node .claude/hooks/auth-rotation-reminder/index.mts"
48+
"command": "node \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/auth-rotation-reminder/index.mts"
4949
},
5050
{
5151
"type": "command",
52-
"command": "node .claude/hooks/stale-process-sweeper/index.mts"
52+
"command": "node \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/stale-process-sweeper/index.mts"
5353
}
5454
]
5555
}

0 commit comments

Comments
 (0)