Repository navigation
Set CLAUDE_PROJECT_DIR to the session cwd, as Claude Code does - #428
Merged
Merged
Conversation
sproott
force-pushed
the
hook-project-dir-cwd
branch
from
November 14, 2026 13:59
ffe200e to
e751721
Compare
sproott
marked this pull request as draft
November 14, 2026 13:59
sproott
force-pushed
the
hook-project-dir-cwd
branch
from
November 14, 2026 14:13
e751721 to
93bf8a5
Compare
sproott
force-pushed
the
hook-project-dir-cwd
branch
from
November 14, 2026 14:24
93bf8a5 to
f4bebb3
Compare
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
force-pushed
the
hook-project-dir-cwd
branch
from
November 14, 2026 17:04
f4bebb3 to
93ca1bd
Compare
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Claude Code sets
CLAUDE_PROJECT_DIRto 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'sproject_dirnow all use the session cwd.Claude Code 2.1.322 from a subdirectory:
Hook
ifpaths likeWrite(/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
.claudeasCLAUDE_PROJECT_DIR. Claude loads the repo root'ssettings.local.jsonfrom 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}inallowed-toolsnow expands to the cwd, as in Claude./-rootedallowed-toolsrules still use the checkout root.Left for later: Claude doesn't export
CLAUDE_PROJECT_DIRto!spans at all, and/-rootedallowed-toolsrules and CLAUDE.md imports still anchor at the checkout root.npm run checkpasses (Biome, strict tsc, Vitest)🤖 Generated with Claude Code