[BUG] $CLAUDE_PROJECT_DIR is empty/unset in hook and Bash tool execution (v2.1.74)
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
$CLAUDE_PROJECT_DIR is completely empty/unset in both PreToolUse hook execution and Bash tool calls. This causes any hook that references $CLAUDE_PROJECT_DIR to silently fail (script not found → non-zero exit → Claude Code reports a non-blocking hook error and continues).
This is distinct from #27343 (wrong value in worktrees) — in our case the variable is entirely absent from the environment.
What Should Happen?
$CLAUDE_PROJECT_DIR should be set to the project root directory, as documented in the hooks reference.
Error Messages/Logs
PreToolUse:Bash hook error: [python3 "$CLAUDE_PROJECT_DIR"/.claude/hooks/git-safety.py]: No stderr output
Diagnostic evidence
# In a Bash tool call during the session:
$ env | grep -i claude
CLAUDE_CODE_ENTRYPOINT=cli
CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMS=1
CLAUDECODE=1
# Note: no CLAUDE_PROJECT_DIR
$ echo "CLAUDE_PROJECT_DIR=$CLAUDE_PROJECT_DIR"
CLAUDE_PROJECT_DIR=
Other Claude-set env vars (CLAUDECODE, CLAUDE_CODE_ENTRYPOINT) are present — only CLAUDE_PROJECT_DIR is missing.
Steps to Reproduce
- Create
.claude/settings.jsonwith a PreToolUse hook using$CLAUDE_PROJECT_DIR:
``json``
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "python3 \"$CLAUDE_PROJECT_DIR\"/.claude/hooks/my-hook.py"
}
]
}
]
}
}
- Start Claude Code in the project directory
- Run any Bash command
- Observe hook error —
$CLAUDE_PROJECT_DIRis empty, so the script path resolves to/.claude/hooks/my-hook.py(nonexistent)
Workaround
Fall back to git rev-parse --show-toplevel:
"command": "bash -c 'root=\"${CLAUDE_PROJECT_DIR:-$(git rev-parse --show-toplevel 2>/dev/null)}\" && python3 \"$root/.claude/hooks/my-hook.py\"'"
Claude Model
Opus
Is this a regression?
Uncertain — $CLAUDE_PROJECT_DIR was added in v1.0.58. It may never have worked in our environment, or it may be a regression in 2.x.
Claude Code Version
2.1.74
Platform
macOS (Darwin 25.3.0)
This issue has 7 comments on GitHub. Read the full discussion on GitHub ↗