Edit tool: show diff when 'String to replace not found' fails
Problem
When the Edit tool can't find old_string in the file, it returns:
String to replace not found in file
This gives no information about why the match failed. In practice, the cause is almost always invisible whitespace differences — trailing spaces, tabs vs spaces, CRLF vs LF, or formatting drift from auto-save/linters.
Suggested Fix
When the match fails, show the closest near-match from the file with differences highlighted. For example:
String to replace not found. Closest match at line 42: - Expected:def foo(self):(4 spaces) - Found:\tdef foo(self):(1 tab)
Even a simple "did you mean line X?" with the actual content would help. The user currently has to re-read the file and visually diff to find what changed.
Impact
Minor but frequent UX pain point. Especially affects projects where formatters normalize whitespace between Read and Edit calls. Related to the linter race condition issue (#51361) but distinct — this also happens when the user manually constructs an old_string with incorrect whitespace.
Environment
- Claude Code CLI on Windows 11
- Projects with mixed formatting tools (Black, Prettier, EditorConfig)
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗