[BUG] Agent tool with isolation: "worktree" fails with EEXIST when .claude/worktrees/ directory already exists

Resolved 💬 3 comments Opened Mar 27, 2026 by Fly-onlyone Closed Apr 30, 2026

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?

The Agent tool with isolation: "worktree" fails to launch if the .claude/worktrees/ directory already exists in the project — even if it's empty. The internal mkdir for the worktrees directory doesn't use { recursive: true }, so it throws EEXIST instead of succeeding idempotently.

This makes it impossible to launch multiple waves of worktree-isolated agents in the same session. After the first wave creates .claude/worktrees/, all subsequent isolation: "worktree" agent launches fail.

Same root cause as the stop hook EEXIST bug (#39673) and other mkdir issues (#33118, #36919, #37306) — missing { recursive: true }.

What Should Happen?

The mkdir for .claude/worktrees/ should use fs.mkdir(path, { recursive: true }) so it succeeds whether the directory exists or not. Agents with isolation: "worktree" should be launchable multiple times in the same session.

Error Messages/Logs

EEXIST: file already exists, mkdir 'C:\Users\PC\Documents\Trading agent\.claude\worktrees'
The agent tool returns this as an error result and the agent never launches.

Steps to Reproduce

  1. Open a project that does NOT have a .claude/worktrees/ directory
  2. Launch an agent with isolation: "worktree" — succeeds, creates .claude/worktrees/agent-xxx/
  3. Wait for the agent to complete
  4. Clean up the worktree (rm -rf .claude/worktrees/agent-xxx, git worktree prune)
  5. Launch another agent with isolation: "worktree"fails with EEXIST because .claude/worktrees/ still exists (empty)

Even without cleanup, launching a second isolation: "worktree" agent in the same session fails because the parent directory was already created by the first agent.

Claude Model

None

Is this a regression?

I don't know

Last Working Version

_No response_

Claude Code Version

2.1.85

Platform

Anthropic API

Operating System

Windows

Terminal/Shell

IntelliJ IDEA terminal

Additional Information

Workaround: Manually pre-create worktrees with git worktree add and pass the path to the agent via the prompt instead of using isolation: "worktree":

git worktree add -b my-branch .claude/worktrees/my-task HEAD
# Then launch agent WITHOUT isolation flag, telling it to work in the worktree path

This defeats the purpose of the isolation feature. The fix should be a one-line change: use fs.mkdir(path, { recursive: true }) when creating the worktrees directory.

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗