[BUG] Edit tool frequently fails with "String to replace not found" on exact matches (macOS)
Description
The Edit tool frequently fails with String to replace was not found even when the old_string exactly matches the file content (confirmed by prior Read output). A second attempt with the identical old_string usually succeeds, suggesting a stale file state cache issue.
Steps to Reproduce
- Read a file with the Read tool
- Attempt to Edit the file using content from the Read output
- Frequently get:
Error editing file: String to replace was not found - Re-read the file (content is identical)
- Same Edit call succeeds
Expected Behavior
Edit tool should match old_string when it exactly matches the file content as shown by the Read tool.
Actual Behavior
First Edit attempt fails ~30-50% of the time, requiring a redundant Read before retry.
Environment
- Claude Code version: 2.1.139
- macOS: 26.5 (Build 25F71)
- Shell: fish (/opt/homebrew/bin/fish)
- Model: GLM-5.1
- Node.js: (via bun)
Additional Context
This happens consistently across multiple sessions and different file types (PHP, JS, JSON, TS). The pattern is: first Edit fails -> Read confirms content unchanged -> second Edit succeeds. This suggests the Edit tool's internal file state cache is not always in sync with the actual filesystem.
Related issues: #52482, #52241
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗