Skip to content

chore: auto-set-up new worktrees with env copy, install, and build#443

Open
MarioCadenas wants to merge 2 commits into
mainfrom
chore/claude-worktree-setup
Open

chore: auto-set-up new worktrees with env copy, install, and build#443
MarioCadenas wants to merge 2 commits into
mainfrom
chore/claude-worktree-setup

Conversation

@MarioCadenas

Copy link
Copy Markdown
Collaborator

Translates the Cursor .cursor/worktrees.json setup into the equivalent for Claude Code worktrees, so a freshly created worktree is ready to run without manual setup.

What it does

Cursor setup-worktree step Claude Code equivalent (this PR)
cp .../apps/dev-playground/.env .worktreeinclude (native, copies at creation)
cp .../apps/clean-app/.env same .worktreeinclude (pattern .env matches both)
pnpm install SessionStart hook → .claude/hooks/setup-worktree.sh
pnpm run build same hook

Files

  • .worktreeinclude (repo root) — Claude Code copies gitignored files matching these .gitignore-syntax globs (.env, .env.local) into new worktrees at creation time. Tracked templates like .env.dist / .env.tmpl are never duplicated.
  • .claude/settings.json — registers a SessionStart hook (startup/resume).
  • .claude/hooks/setup-worktree.sh — runs pnpm install + pnpm run build the first time a session opens in a fresh linked worktree (guarded on missing node_modules). Silent no-op in the primary checkout and in already-set-up worktrees; build failures are reported on stderr but never block session startup.

Why a SessionStart hook (not WorktreeCreate)

Claude Code has no post-create worktree hook. The only worktree hook, WorktreeCreate, replaces git's worktree creation entirely (and disables .worktreeinclude), which would mean reimplementing git's default branch/baseRef/placement logic and risk disrupting subagent worktree creation. The SessionStart approach is additive and never touches how worktrees are created. Trade-off: install/build run at first session-start rather than at creation (moments later in practice).

Notes

  • Scope is Claude Code only — .worktreeinclude is read by Claude Code, not git, so it does not affect worktrees created by other tools.
  • Separately worth doing (not in this PR): add .claude/worktrees/ to .gitignore so agent worktree copies stop appearing in git status.

This pull request and its description were written by Isaac.

Translate the Cursor .cursor/worktrees.json setup into Claude Code worktrees:

- .worktreeinclude (repo root): copies gitignored .env / .env.local into new
  worktrees at creation time, using .gitignore glob syntax.
- .claude/settings.json: registers a SessionStart hook (startup/resume).
- .claude/hooks/setup-worktree.sh: runs 'pnpm install' + 'pnpm run build' the
  first time a session opens in a fresh linked worktree (guarded on missing
  node_modules), and is a silent no-op in the primary checkout and in
  already-set-up worktrees.

Co-authored-by: Isaac
Signed-off-by: MarioCadenas <MarioCadenas@users.noreply.github.com>
@MarioCadenas MarioCadenas requested a review from a team as a code owner June 12, 2026 09:07
@MarioCadenas MarioCadenas requested a review from calvarjorge June 12, 2026 09:07
Mirror .cursor/worktrees.json by naming the exact files to copy
(apps/dev-playground/.env, apps/clean-app/.env) instead of a bare `.env`
glob, so worktree creation copies precisely the dev env files and nothing
picked up at unexpected depths.

Signed-off-by: MarioCadenas <MarioCadenas@users.noreply.github.com>
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.

1 participant