Skip to content

Set CLAUDE_PROJECT_DIR to the session cwd, as Claude Code does - #428

Merged
ilovepixelart merged 4 commits into
ilovepixelart:mainfrom
sproott:hook-project-dir-cwd
Nov 14, 2026
Merged

ilovepixelart merged 4 commits into
ilovepixelart:mainfrom
sproott:hook-project-dir-cwd

Conversation

@sproott

@sproott sproott commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Claude Code sets CLAUDE_PROJECT_DIR to the directory the session started in, even in a repo subdirectory. pi used the checkout root. Hooks, stdio MCP server env, plugin MCP config, command and skill variables, ! span env, and the status line's project_dir now all use the session cwd.

Claude Code 2.1.322 from a subdirectory:

mkdir -p /tmp/cpd/repo/sub && cd /tmp/cpd/repo && git init -q
echo '{"hooks":{"SessionStart":[{"hooks":[{"type":"command","command":"echo \"$CLAUDE_PROJECT_DIR\" > /tmp/cpd/out.txt"}]}]}}' > /tmp/cpd/hook.json
cd sub && claude -p 'reply with ok' --max-turns 1 --settings /tmp/cpd/hook.json >/dev/null
cat /tmp/cpd/out.txt   # /tmp/cpd/repo/sub

Hook if paths like Write(/src/**) anchor at the cwd too. I checked that Claude does the same, and the permissions docs say so for project and local settings.

One deliberate difference: a hook from a project or local settings file gets the folder that holds its .claude as CLAUDE_PROJECT_DIR. Claude loads the repo root's settings.local.json from a subdirectory but passes the cwd, so "$CLAUDE_PROJECT_DIR/.claude/hooks/x.sh" fails with exit 127 there. In a worktree, where that file lives in the main checkout, the hook gets the worktree's own root. On Windows the local file always sits in the cwd, so this never comes up.

A command's ${CLAUDE_PROJECT_DIR} in allowed-tools now expands to the cwd, as in Claude. /-rooted allowed-tools rules still use the checkout root.

Left for later: Claude doesn't export CLAUDE_PROJECT_DIR to ! spans at all, and /-rooted allowed-tools rules and CLAUDE.md imports still anchor at the checkout root.

  • npm run check passes (Biome, strict tsc, Vitest)
  • Tests cover the change

🤖 Generated with Claude Code

@sproott
sproott force-pushed the hook-project-dir-cwd branch from ffe200e to e751721 Compare November 14, 2026 13:59
@sproott
sproott marked this pull request as draft November 14, 2026 13:59
@sproott
sproott force-pushed the hook-project-dir-cwd branch from e751721 to 93bf8a5 Compare November 14, 2026 14:13
@sproott sproott changed the title Make hook projectDir resolve to cwd instead of repo root Set CLAUDE_PROJECT_DIR to the session cwd, as Claude Code does Nov 14, 2026
@sproott
sproott force-pushed the hook-project-dir-cwd branch from 93bf8a5 to f4bebb3 Compare November 14, 2026 14:24
@sproott
sproott marked this pull request as ready for review November 14, 2026 16:37
Claude Code sets CLAUDE_PROJECT_DIR to the directory the session started
in, even in a repository subdirectory. pi used the checkout root for hooks,
stdio MCP servers, plugin MCP config, command variables and spans, and the
status line's project_dir. All of them now use the session cwd, and hook
`if` paths keep anchoring there, as Claude's do.

A hook from a project or local settings file gets the checkout folder
holding its .claude instead, so a repo-root settings.local.json hook that
runs $CLAUDE_PROJECT_DIR/.claude/hooks/x.sh still resolves from a
subdirectory. In a worktree that is the worktree's own root. When the same
handler is declared in user and local settings, the local copy runs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@sproott
sproott force-pushed the hook-project-dir-cwd branch from f4bebb3 to 93ca1bd Compare November 14, 2026 17:04
collectCommands had a cognitive complexity of 19 against the Biome limit
of 15. The duplicate-handler bookkeeping moves to keepOnce; the key, the
order of the result and the rule that a project or local copy replaces
the user copy are unchanged.
With the session cwd in packages/api of a git repository, a plugin
server's ${CLAUDE_PROJECT_DIR} argument and its CLAUDE_PROJECT_DIR
environment variable must both be the cwd. Passing checkoutRoot(ctx.cwd)
to loadPluginServers expands the argument to the repository root and
fails the test.
settings-env.md, hooks.md, mcp.md, commands.md and statusline.md state
that CLAUDE_PROJECT_DIR and workspace.project_dir are the directory the
session started in, and that a /-rooted hook `if` path anchors there.

hooks.md lists two behaviours under divergences: a hook from the
repository root's settings.local.json gets the checkout root as
CLAUDE_PROJECT_DIR where Claude Code passes the cwd, and a handler
declared in user settings and in project or local settings runs once as
the project or local copy.

The checkoutRoot comment names the hook use it still has.
@ilovepixelart
ilovepixelart merged commit 22d97c0 into ilovepixelart:main Nov 14, 2026
12 checks passed
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.

2 participants