Write tool EEXIST error when writing to existing directories on Windows
Description
The Write tool fails with EEXIST: file already exists, mkdir '<path>' when creating new files in any existing subdirectory on Windows. The tool internally calls fs.mkdir() on the parent directory before writing, but does not use { recursive: true }, causing it to throw when the directory already exists.
Reproduction Steps
- Have any existing subdirectory in your project (e.g.,
docs/,src/, etc.) - Use the Write tool to create a new file in that directory:
Write file_path: K:\Workspace\MyProject\docs\new-file.md
- Error:
EEXIST: file already exists, mkdir 'K:\Workspace\MyProject\docs'
Test Matrix
| Scenario | Result |
|----------|--------|
| Write to project root (parent = repo root) | SUCCESS |
| Write to any existing subdirectory (docs/, bot/, cloudflare/) | EEXIST ERROR |
| Write to existing nested subdirectories (docs/reviews/, docs/audit/) | EEXIST ERROR |
| Write to a brand-new directory that doesn't exist yet | SUCCESS (mkdir creates it) |
| Bash echo > to same directories | SUCCESS (filesystem is fine) |
Root Cause
The Write tool's internal fs.mkdir() call on the parent directory path does not use { recursive: true }. Without that flag, mkdir throws EEXIST when the target directory already exists. With { recursive: true }, it silently succeeds (equivalent to mkdir -p).
Expected Behavior
The Write tool should create files in any directory — existing or new — without errors.
Suggested Fix
Change the internal fs.mkdir(parentDir) to fs.mkdir(parentDir, { recursive: true }).
Environment
- Platform: Windows 11 Pro 10.0.26200
- Shell: Git Bash
- Claude Code model: claude-opus-4-6
This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗