WorktreeCreate hook: $CLAUDE_PROJECT_DIR not expanded when Agent uses isolation: "worktree"
Summary
When spawning a subagent via the Agent tool with isolation: "worktree", the WorktreeCreate hook fails because $CLAUDE_PROJECT_DIR is not expanded (or not set) in the hook command string. Interactive worktree creation from the same project works fine — the failure is specific to the Agent-tool-spawned path.
Repro
.claude/settings.json:
{
"hooks": {
"WorktreeCreate": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/create-worktree.sh",
"timeout": 60
}
]
}
]
}
}
.claude/hooks/create-worktree.sh exists and is executable (chmod +x).
Spawn an Agent with isolation: "worktree".
Expected
Hook runs with $CLAUDE_PROJECT_DIR expanded to the project root; script executes.
Actual
WorktreeCreate hook failed: .claude/hooks/create-worktree.sh: /bin/sh: .claude/hooks/create-worktree.sh: No such file or directory
Note the error shows the relative path .claude/hooks/create-worktree.sh — $CLAUDE_PROJECT_DIR/ was stripped/empty, so the shell tried a relative lookup from whatever CWD the hook runner was in, which was not the project root.
Workarounds considered
- Hardcoding an absolute path breaks for users with concurrent worktrees at different paths.
cd "$CLAUDE_PROJECT_DIR" && ...hits the same expansion problem.
Environment
- Claude Code (Opus 4.6, 1M context)
- Darwin 25.3.0
- Project is a git repo with worktree-based subagent workflow
Impact
Blocks background Agent tool calls that require isolation: "worktree" whenever a WorktreeCreate hook references $CLAUDE_PROJECT_DIR. Forces fallback to un-isolated agents, losing the safety worktrees provide.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗