claude-code - 💡(How to fix) Fix [BUG] Edit tool frequently fails with "String to replace not found" on exact matches (macOS)

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

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.

Error Message

  1. Frequently get: Error editing file: String to replace was not found

Root Cause

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.

RAW_BUFFERClick to expand / collapse

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

  1. Read a file with the Read tool
  2. Attempt to Edit the file using content from the Read output
  3. Frequently get: Error editing file: String to replace was not found
  4. Re-read the file (content is identical)
  5. 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

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING