Skip to content

Commit 11737cc

Browse files
committed
fix(hook): release-workflow-guard — derive project dir from script path
Cascade from socket-btm bc4fa392. Hook resolved its project dir via sandbox didn't propagate the env var. Adds script-path derivation fallback so the hook works even when the runner doesn't export CLAUDE_PROJECT_DIR.
1 parent 3e9a810 commit 11737cc

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

  • .claude/hooks/release-workflow-guard

.claude/hooks/release-workflow-guard/index.mts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,28 @@ function workflowDeclaresDryRunInput(
315315
// intentionally excluded so a same-named workflow in the current
316316
// checkout can't false-positive a cross-repo dispatch.
317317
function resolveSearchRoots(command: string): string[] {
318-
const projectDir = process.env['CLAUDE_PROJECT_DIR'] ?? process.cwd()
318+
// Resolution order: $CLAUDE_PROJECT_DIR (Claude Code sets this when
319+
// it remembers to) → derive from this hook script's path (the hook
320+
// lives at <project>/.claude/hooks/release-workflow-guard/index.mts,
321+
// so go three levels up from __dirname) → $PWD as last resort.
322+
// The script-path derivation is the most robust because it doesn't
323+
// depend on the runner exporting env vars correctly.
324+
let projectDir = process.env['CLAUDE_PROJECT_DIR']
325+
if (!projectDir) {
326+
// process.argv[1] is the absolute path of this hook script when
327+
// invoked via `node <path>`. Walk up to the repo root.
328+
const scriptPath = process.argv[1]
329+
if (scriptPath) {
330+
// .claude/hooks/release-workflow-guard/index.mts → ../../../ = repo
331+
const candidate = path.resolve(scriptPath, '..', '..', '..', '..')
332+
if (existsSync(path.join(candidate, '.github', 'workflows'))) {
333+
projectDir = candidate
334+
}
335+
}
336+
}
337+
if (!projectDir) {
338+
projectDir = process.cwd()
339+
}
319340
const repoMatch = GH_REPO_FLAG_RE.exec(command)
320341
if (!repoMatch || path.basename(projectDir) === repoMatch[1]!) {
321342
return [projectDir]

0 commit comments

Comments
 (0)