[BUG] Edit tool fails in subdirectories (works in root directory)
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?
Summary
I've discovered a specific pattern for the "File has been unexpectedly modified" error that hasn't been documented yet: The Edit tool consistently fails in subdirectories but works perfectly in the root directory.
Environment
- Platform: Windows (MINGW64_NT-10.0-26200)
- Claude Code Version: 2.0.74
- File System: NTFS
- Shell: Git Bash
What Should Happen?
The file should be edited successfully.
Error Messages/Logs
Steps to Reproduce
Reproduction Steps
Setup
cd "C:/path/to/your/project"
echo "line 1" > root_test.txt
mkdir -p subdirectory
echo "line 1" > subdirectory/subdir_test.txt
Test 1: Edit file in ROOT directory (✅ WORKS)
Read: C:/path/to/your/project/root_test.txt
Edit: Change "line 1" to "line 1 EDITED"
Result: ✅ SUCCESS - File edited successfully
Test 2: Edit file in SUBDIRECTORY (❌ FAILS)
Read: C:/path/to/your/project/subdirectory/subdir_test.txt
Edit: Change "line 1" to "line 1 EDITED"
Result: ❌ ERROR - "File has been unexpectedly modified"
Comprehensive Testing Results
I tested multiple scenarios to isolate the cause:
| Test Scenario | Root Directory | Subdirectory |
|--------------|----------------|--------------|
| Small text file (33 bytes) | ✅ Works | ❌ Fails |
| Large HTML file (43KB) | ✅ Works | ❌ Fails |
| Freshly created file | ✅ Works | ❌ Fails |
| File copied from subdirectory | ✅ Works | ❌ Fails |
| .txt extension | ✅ Works | ❌ Fails |
| .html extension | ✅ Works | ❌ Fails |
| .md extension | ✅ Works | ❌ Fails |
| Different subdirectory names | ✅ Works | ❌ Fails |
Key Findings
What DOESN'T cause the issue
- ❌ File sync services (OneDrive disabled)
- ❌ Antivirus software (disabled during testing)
- ❌ File size
- ❌ File type/extension
- ❌ Directory permissions (identical between root and subdirectories)
- ❌ Specific directory names
- ❌ External processes modifying files
Claude Model
Sonnet (default)
Is this a regression?
I don't know
Last Working Version
_No response_
Claude Code Version
2.0.74 (Claude Code)
Platform
Anthropic API
Operating System
Windows
Terminal/Shell
VS Code integrated terminal
Additional Information
Root Cause Hypothesis
The Edit tool's file change detection mechanism appears to have a path resolution or file watcher bug that breaks when working with files in subdirectories. The issue is likely in how the tool:
- Stores the initial file state during Read operation
- Retrieves the file path for comparison during Edit operation
- Compares file metadata (timestamps/checksums) between operations
The discrepancy between root and subdirectory behavior suggests the tool may be:
- Using different path formats (relative vs absolute)
- Having path normalization issues (forward slashes vs backslashes)
- Accessing different file handles or inodes for the same file
Workaround
Confirmed workaround: Move files to the root directory temporarily for editing:
# Move to root
mv subdirectory/file.html ./
# Claude can now edit successfully
# (Edit operations work)
# Move back
mv file.html subdirectory/
Additional Evidence
File system timestamps show no external modifications:
# File in subdirectory (fails to edit)
stat subdirectory/subdir_test.txt
Access: 2025-12-20 11:32:xx
Modify: 2025-12-20 11:32:xx
Change: 2025-12-20 11:32:xx
# No changes between Read and Edit operationsThis issue has 3 comments on GitHub. Read the full discussion on GitHub ↗