claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

[BUG]  "Error: String to replace not found in file."

Open mrjoshuak opened this issue 7 months ago • 10 comments

Environment

  • Platform (select one):
    • [x] Anthropic API
  • Claude CLI version: 0.2.101 (Claude Code)
  • Operating System: macOS Sequoia 15.5
  • Terminal: iTerm2

Bug Description

Claude Code is unable to use the Update tool successfully. The error in the title is occurring on every single attempt to use the Update tool. 100% failure rate.

Steps to Reproduce

  1. Trigger the Update tool by any mechanism.

Expected Behavior

Given that Claude Code should always have a copy of the current code in the context when using an Update tool I would expect it to always be successful. I can't think of any case that one would expect an LLM to fail at string matching tasks given a working UTF-8 source code tokenizer.

Actual Behavior

I only ever get an error in response to the Update tool being called.

⏺ Update(../../node/graph_shader.go)…
  ⎿  Error: String to replace not found in file.

Additional Context

I am using go in most of my coding projects. Does Claude Code not use the on disk file content in the context? Perhaps Claude Code is relying on the idea that LLM output code remains unchanged when written to the file? If so this would fail to account for ecosystems like go in which source code might be reformatted on the fly. Claude Code should be careful to update its context with actual file content prior to every api call to insure that file content is quoted correctly accounting for external tool and user manipulation. But this is just my blind speculation as to a possible causes.

mrjoshuak avatar May 05 '25 21:05 mrjoshuak

This is marked as a dupe but not clear what it is a dupe of, is it #781 ?

sharatvisweswara avatar May 09 '25 19:05 sharatvisweswara

I'm seeing this in Python projects as well:

⏺ Update(hedwig/utils/config.py)…
  ⎿  Error: String to replace not found in file.
     String: <snip>
     … +3 lines (ctrl+r to see all)

⏺ Let me try again with the exact string from the file:

⏺ Read(hedwig/utils/config.py)…
  ⎿  Read 79 lines (ctrl+r to expand)

⏺ Update(hedwig/utils/config.py)…
  ⎿  Error: String to replace not found in file.
     String: <snip>
     … +3 lines (ctrl+r to see all)

⏺ It seems there might be some invisible characters or line ending differences. Let's try a different approach using Write instead:

⏺ Write(file_path: hedwig/utils/config.py)…

sharatvisweswara avatar May 10 '25 04:05 sharatvisweswara

I'm seeing this in a php project as well. It happens all the time so that I already thought that's "normal" before looking it up.

⏺ Update(site/plugins/posse/index.php)…
  ⎿  Error: String to replace not found in file.
     String:     'api' => [
             'routes' => [
                 [
                     'pattern' => 'posse/queue',
                     'action' => function() {
                         if (option('debug')) {
                             error_log('POSSE API: Queue route called');
                         }
                         try {
     … +54 lines (ctrl+r to see all)

⏺ Let me check the exact content of the API section:

⏺ Read(site/plugins/posse/index.php)…
  ⎿  Read 70 lines (ctrl+r to expand)

dewey avatar May 11 '25 02:05 dewey

Doubling up on this an receive it in python all update tools too

ProfVex avatar May 11 '25 09:05 ProfVex

Can confirm, this started last week in all my Swift/ObjectiveC projects.

pj4533 avatar May 11 '25 13:05 pj4533

Seeing this in VSCode on Windows 11 in WSL a lot with LF file endings in a NodeJS project. Uses a lot of tokens retrying and failing.

Feedback ID: babf96e8-9a7b-4e8e-931e-b0f04ddd7beb

EDIT: Might be because of tabs? https://github.com/anthropics/claude-code/issues/781 EDIT 2: I stopped having this problem after converting all tabs to spaces in my codebase EDIT 3: Spoke too soon, but at least it does not need 10 tries per change now

Dekadinious avatar May 12 '25 07:05 Dekadinious

Same issue, b27edbbb-8570-4482-a606-14b266bc779e

mquandalle avatar May 16 '25 10:05 mquandalle

Seems like this is related to using tabs over spaces for me. I switched my Biome linter to use spaces and reduced the issue occurrence.

sebmellen avatar Jun 09 '25 04:06 sebmellen