gemini-cli icon indicating copy to clipboard operation
gemini-cli copied to clipboard

Repeated issues with the `replace` (edit) tool

Open sijieamoy opened this issue 1 year ago • 60 comments

TLDR

Added by @jackwotherspoon.

Here are main issues being seen with the replace tool:

  • Error: Failed to edit, could not find the string to replace.
  • replace tool misinterprets literal newline characters in the old_string or new_string parameters, leading to syntax errors in the generated code. It seems to treat \n as an actual newline instead of the escaped character
  • 0 occurrences found for old_string.
  • No changes detected

Bug Description

Describe the bug Here's a very common pattern of GCLI encountered the Error: Failed to edit, could not find the string to replace. error with the replace tool and had to switch to write_file. ''' ✦ I've updated ChatInput.tsx. Now I will fix the errors in VimInput.tsx. ────────────────────────────────────────────────── │ ✔ Edit ...ges/cli/src/ui/components/VimInput.tsx: interface VimInputProps {... => interface VimInputProps {... │ │ │ │ Error: Failed to edit, could not find the string to replace. │ ──────────────────────────────────────────────────

✦ I apologize for the error. It seems I'm still having trouble with the replace tool. I will use write_file to update the VimInput.tsx file. I will first read the file, then modify the content in memory, and finally write the new content to the file. ╭────────────────────────────────────────────────────────╮ │ ✔ ReadFile ...ges/cli/src/ui/components/VimInput.tsx │ ╰────────────────────────────────────────────────────────╯

✦ I have the latest version of the VimInput.tsx file. I will now remove the unused value and onChange props and fix the any type in the vimDispatch prop. ╭───────────────────────────────────────────────── │ ✔ WriteFile Writing to ...ges/cli/src/ui/components/VimInput.tsx │ │ │ │ 8 import { VimState } from './vim.js'; │

Additional context Add any other context about the problem here.

sijieamoy avatar Jun 13 '25 23:06 sijieamoy

I made #1061 as a prototype that explores an alternate approach for providing files to the model in a way that reduces or eliminates these issues. You can try the branch; it's a bit behind now though and conflicts with main.

keir avatar Jun 15 '25 05:06 keir

For this issue, i have seen today that cli is unable to modify it's internal state of the files. Although it has changed the files in the workspace. So, the replacement tool keeps failing continuously since, it cannot find the original string (since, there is modified string in place) so further changes to this string fail.

age-of-ai avatar Jun 26 '25 13:06 age-of-ai

It can make the problem worse when falling back to writing an entire file as there is some internal limit and it can easily end up truncating the file.

EpsilonPrime avatar Jun 29 '25 04:06 EpsilonPrime

From duplicate issue #1635 comments for "No changes detected":

I wanted to add that this behavior has become annoyingly frequent. At one point, Gemini gave me the blank answer more than 5 times in less than 30 minutes across multiple runs of the app.

my solution is to tell gemini-cli that “your latest modification ended up with 'No changes detected.' to the file, tell me what the modifications are and I'll modify it by hand”

This happens every time the cli decides to switch to 2.5 flash

jackwotherspoon avatar Jul 04 '25 20:07 jackwotherspoon

Issue stil happening as of today.. Last version of Gemini CLI

redareda9 avatar Jul 07 '25 10:07 redareda9

Gemini CLI (latest version on MacOS, using iTerm) got into infinite loop due to this annoying "Error: Failed to edit, could not find the string to replace." problem, making it unable to do any code change at all and became useless.

x-byi avatar Jul 12 '25 16:07 x-byi

This may not be exactly the same issue, but may provide further context for debugging. The log below illustrates how Gemini is struggling with handling the context of repeated edits.

 ╭──────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ ✔  Edit main.py: import struct... => import uuid...                              │
 │                                                                                                      │
 │    1    + import uuid                                                                                │
 │    2      import struct                                                                              │
 │    3      from fastapi import FastAPI, WebSocket, WebSocketDisconnect                                │
 │    4      from .messages import (                                                                    │
 ╰──────────────────────────────────────────────────────────────────────────────────────────────────────╯
 ╭──────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ ✔  Edit main.py: import uuid... => import struct...                              │
 │                                                                                                      │
 │    1    - import uuid                                                                                │
 │    1      import struct                                                                              │
 │    2      from fastapi import FastAPI, WebSocket, WebSocketDisconnect                                │
 │    3      from .messages import (                                                                    │
 ╰──────────────────────────────────────────────────────────────────────────────────────────────────────╯
 ╭──────────────────────────────────────────╮
 │ ✔  ReadFile main.py  │
 ╰──────────────────────────────────────────╯
 ╭─────────────────────────────────────────────────────────╮
 │ ✔  Edit No file changes to main.py  │
 │                                                         │
 │    ╭──────────────────────╮                             │
 │    │                      │                             │
 │    │ No changes detected. │                             │
 │    │                      │                             │
 │    ╰──────────────────────╯                             │
 ╰─────────────────────────────────────────────────────────╯
 ╭──────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ ✔  Edit main.py: import struct => import uuid                                    │
 │                                                                                                      │
 │    1    + import uuid                                                                                │
 │    2      import struct                                                                              │
 │    3      from fastapi import FastAPI, WebSocket, WebSocketDisconnect                                │
 │    4      from .messages import (                                                                    │
 ╰──────────────────────────────────────────────────────────────────────────────────────────────────────╯
 ╭──────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ ✔  Edit main.py: import struct... => import uuid...                              │
 │                                                                                                      │
 │    1      import uuid                                                                                │
 │    2    + import uuid                                                                                │
 │    3      import struct                                                                              │
 │    4      from fastapi import FastAPI, WebSocket, WebSocketDisconnect                                │
 │    5      from .messages import (                                                                    │
 ╰──────────────────────────────────────────────────────────────────────────────────────────────────────╯
 ╭──────────────────────────────────────────╮
 │ ✔  ReadFile main.py  │
 ╰──────────────────────────────────────────╯
 ╭──────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ ✔  Edit main.py: import uuid... => import uuid...                                │
 │                                                                                                      │
 │    1      import uuid                                                                                │
 │    2    - import uuid                                                                                │
 │    2      import struct                                                                              │
 │    3      from fastapi import FastAPI, WebSocket, WebSocketDisconnect                                │
 │    4      from .messages import (                                                                    │
 ╰──────────────────────────────────────────────────────────────────────────────────────────────────────╯
 ╭─────────────────────────────────────────────────────────────────────╮
 │ ✔  Edit main.py: import uuid... => import uuid  │
 │                                                                     │
 │    ╭──────────────────────╮                                         │
 │    │                      │                                         │
 │    │ No changes detected. │                                         │
 │    │                      │                                         │
 │    ╰──────────────────────╯                                         │
 ╰─────────────────────────────────────────────────────────────────────╯
 ╭─────────────────────────────────────────────────────────╮
 │ ✔  Edit No file changes to main.py  │
 │                                                         │
 │    ╭──────────────────────╮                             │
 │    │                      │                             │
 │    │ No changes detected. │                             │
 │    │                      │                             │
 │    ╰──────────────────────╯                             │
 ╰─────────────────────────────────────────────────────────╯
 ╭──────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ ✔  Edit main.py: import uuid => import struct                                    │
 │                                                                                                      │
 │    1    - import uuid                                                                                │
 │    1      import struct                                                                              │
 │    2      from fastapi import FastAPI, WebSocket, WebSocketDisconnect                                │
 │    3      from .messages import (                                                                    │
 ╰──────────────────────────────────────────────────────────────────────────────────────────────────────╯
 ╭────────────────────────────────────────────────────────────────────╮
 │ ✔  Edit main.py: import uuid => import struct  │
 │                                                                    │
 │    ╭──────────────────────╮                                        │
 │    │                      │                                        │
 │    │ No changes detected. │                                        │
 │    │                      │                                        │
 │    ╰──────────────────────╯                                        │
 ╰────────────────────────────────────────────────────────────────────╯
 ╭──────────────────────────────────────────╮
 │ ✔  ReadFile main.py  │
 ╰──────────────────────────────────────────╯
 ╭──────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ ✔  Edit main.py: import struct... => import uuid...                              │
 │                                                                                                      │
 │    1    + import uuid                                                                                │
 │    2      import struct                                                                              │
 │    3      from fastapi import FastAPI, WebSocket, WebSocketDisconnect                                │
 │    4      from .messages import (                                                                    │
 ╰──────────────────────────────────────────────────────────────────────────────────────────────────────╯
 ╭──────────────────────────────────────────╮
 │ ✔  ReadFile main.py  │
 ╰──────────────────────────────────────────╯
 ╭──────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ ✔  Edit main.py: import struct => import uuid                                    │
 │                                                                                                      │
 │    1      import uuid                                                                                │
 │    2    + import uuid                                                                                │
 │    3      import struct                                                                              │
 │    4      from fastapi import FastAPI, WebSocket, WebSocketDisconnect                                │
 │    5      from .messages import (                                                                    │
 ╰──────────────────────────────────────────────────────────────────────────────────────────────────────╯
 ╭──────────────────────────────────────────╮
 │ ✔  ReadFile main.py  │
 ╰──────────────────────────────────────────╯
 ╭──────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ ✔  Edit main.py: import uuid... => import uuid                                   │
 │                                                                                                      │
 │    1      import uuid                                                                                │
 │    2    - import uuid                                                                                │
 │    2      import struct                                                                              │
 │    3      from fastapi import FastAPI, WebSocket, WebSocketDisconnect                                │
 │    4      from .messages import (                                                                    │
 ╰──────────────────────────────────────────────────────────────────────────────────────────────────────╯
 ╭──────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ ✔  Edit main.py: import uuid => import struct                                    │
 │                                                                                                      │
 │    1    - import uuid                                                                                │
 │    1      import struct                                                                              │
 │    2      from fastapi import FastAPI, WebSocket, WebSocketDisconnect                                │
 │    3      from .messages import (                                                                    │
 ╰──────────────────────────────────────────────────────────────────────────────────────────────────────╯
 ╭──────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ ✔  Edit Model did not provide valid parameters for edit tool                                         │
 │                                                                                                      │
 │    Error: Failed to edit, could not find the string to replace.                                      │
 ╰──────────────────────────────────────────────────────────────────────────────────────────────────────╯
 ╭──────────────────────────────────────────╮
 │ ✔  ReadFile main.py  │
 ╰──────────────────────────────────────────╯
 ╭─────────────────────────────────────────────────────────╮
 │ ✔  Edit No file changes to main.py  │
 │                                                         │
 │    ╭──────────────────────╮                             │
 │    │                      │                             │
 │    │ No changes detected. │                             │
 │    │                      │                             │
 │    ╰──────────────────────╯                             │
 ╰─────────────────────────────────────────────────────────╯
 ╭──────────────────────────────────────────╮
 │ ✔  ReadFile main.py  │
 ╰──────────────────────────────────────────╯
 ╭────────────────────────────────────────────────────────────────────╮
 │ ✔  Edit main.py: import uuid => import struct  │
 │                                                                    │
 │    ╭──────────────────────╮                                        │
 │    │                      │                                        │
 │    │ No changes detected. │                                        │
 │    │                      │                                        │
 │    ╰──────────────────────╯                                        │

tillahoffmann avatar Jul 12 '25 20:07 tillahoffmann

Is there a version before this broke Gemini CLI?

That way I can revert to the older version.

larspensjo avatar Jul 13 '25 09:07 larspensjo

I think worth keeping this open given all of the discussion-- I've been marking similar issues as dupes of https://github.com/google-gemini/gemini-cli/issues/4137

chrstnb avatar Jul 15 '25 14:07 chrstnb

I had it today. State transitions problem. I had to show it to ♊ via grep and cat plus teach it theory of sets first.

My fork of https://github.com/modelcontextprotocol/servers/issues/2332 helps a lot then.

Manamama avatar Jul 15 '25 15:07 Manamama

My theory is that if you hit escape when a code change happens the state Gemini has includes the change is incorrectly cached and it is confused because of that.

EpsilonPrime avatar Jul 15 '25 17:07 EpsilonPrime

It also still happens to me in the newest version: no clear pattern when. I have tried to make Gemini chunk the longer strings to be replaced into smaller ones, at a sentence level even - it does not help usually. OTOH, sometimes (very rarely) after three attempts where I thought Gemini would loop herself into oblivion, Gemini "clicks" somehow and matches them right.

My temp solution to hand prevent Gemin's looping: I tell Gemini to use sed for the same. If I am tired at the end of the day, I tell Gemini to just dump the diffs (usually an update, an end-of-day report) into a new .md file so that I can do the insertion by hand instead.

Manamama avatar Jul 21 '25 13:07 Manamama

Adding a specific scenario related to 'Repeated issues with the replace (edit) tool':

When using the replace tool, if the old_string is not unique within the file (e.g., a code snippet also appears as a literal string in a system_message or comment), the replace tool fails with the error Failed to edit, Expected 1 occurrence but found X for old_string.

This behavior, while intentional (to prevent accidental replacements), can lead to an agent getting stuck in a continuous loop of retries if its internal error handling logic doesn't explicitly account for this specific error message and adapt its strategy (e.g., by refining the old_string or asking for user clarification). This highlights a need for more robust agent-side error recovery for this particular replace tool failure mode.

hebenezer-m avatar Jul 21 '25 14:07 hebenezer-m

I am also facing this issue continuously. Very often happening. sometimes repeated loops

teamdaemons avatar Jul 27 '25 04:07 teamdaemons

Informal report that it has gotten more frequent: up to 1/3 of replace operations. (Or maybe my docs are so tricky?). I resort to my trick https://github.com/google-gemini/gemini-cli/issues/1028#issuecomment-3096851067 asap then to avoid loops and wasted cycles.

Manamama avatar Jul 27 '25 11:07 Manamama

Error: Failed to edit, expected 1 occurrence but found 2.

jeffery9 avatar Jul 27 '25 12:07 jeffery9

#1531

jeffery9 avatar Jul 28 '25 01:07 jeffery9

I get same issue. Image

cutun avatar Jul 30 '25 20:07 cutun

the same #5254

jeffery9 avatar Jul 31 '25 01:07 jeffery9

Still getting errors with the replace tool myself quite often:

✦ Okay, useSlashCommandProcessor.ts is updated, so commandContext has setBugReportConfirmationRequest. Now I'll add that function to the useSlashCommandProcessor call in App.tsx.
 ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ x  replace {"old_string":"import {\n  Message,\n  MessageType,\n  HistoryItemWithoutId,\n  HistoryItem,\n  SlashCommandProcessorResult,\n} from '../types.js';\nimport { LoadedSettings } from '../../config/settings.js';\nimpor…  │
 │                                                                                                                                                                                                                                     │
 │    Failed to edit, 0 occurrences found for old_string in /Users/jackwoth/dev/gemini-cli/packages/cli/src/ui/hooks/slashCommandProcessor.ts. No edits made. The exact text in old_string was not found. Ensure you're not            │
 │    escaping content incorrectly and check whitespace, indentation, and context. Use read_file tool to verify.                                                                                                                       │
 ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ Right, my mistake. I need to modify App.tsx to pass setBugReportConfirmationRequest to useSlashCommandProcessor, not the other way around. Correcting that now.
 ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ x  replace {"old_string":"    openPrivacyNotice,\n    toggleVimEnabled,\n    setIsProcessing,\n  );\n\n  const {\n    streamingState,\n    submitQuery,","new_string":"    openPrivacyNotice,\n    toggleVimEnabled,\n    setIsPr…  │
 │                                                                                                                                                                                                                                     │
 │    Failed to edit, 0 occurrences found for old_string in /Users/jackwoth/dev/gemini-cli/packages/cli/src/ui/App.tsx. No edits made. The exact text in old_string was not found. Ensure you're not escaping content                  │
 │    incorrectly and check whitespace, indentation, and context. Use read_file tool to verify.                                                                                                                                        │
 ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ I've been making repeated find-and-replace errors. My mistake was modifying the wrong context in App.tsx. I need to pass setBugReportConfirmationRequest to the useSlashCommandProcessor call itself. I'll correct that now.
 ╭────────────────────────────────────────────────────────╮
 │ ✔  ReadFile ...ages/ui/hooks/slashCommandProcessor.ts  │
 ╰────────────────────────────────────────────────────────╯
✦ Okay, useSlashCommandProcessor.ts reviewed. Modifying App.tsx to pass setBugReportConfirmationRequest to the useSlashCommandProcessor hook now.
 ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ x  replace {"file_path":"/Users/jackwoth/dev/gemini-cli/packages/cli/src/ui/App.tsx","old_string":"    openEditorDialog,\n    toggleCorgiMode,\n    setQuittingMessages,\n    openPrivacyNotice,\n    toggleVimEnabled,\n    setI…  │
 │                                                                                                                                                                                                                                     │
 │    Failed to edit, 0 occurrences found for old_string in /Users/jackwoth/dev/gemini-cli/packages/cli/src/ui/App.tsx. No edits made. The exact text in old_string was not found. Ensure you're not escaping content                  │
 │    incorrectly and check whitespace, indentation, and context. Use read_file tool to verify.                                                                                                                                        │
 ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ I'm sorry for the repeated replace tool errors. I'll be more careful now. I'll read the file to ensure I have the correct content, then modify the useSlashCommandProcessor call in App.tsx to pass the
  setBugReportConfirmationRequest function.
 ╭──────────────────────────────────────────╮
 │ ✔  ReadFile packages/cli/src/ui/App.tsx  │
 ╰──────────────────────────────────────────╯
✦ Okay, read App.tsx. Modifying useSlashCommandProcessor to pass setBugReportConfirmationRequest.
 ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ x  replace {"new_string":"    openEditorDialog,\n    toggleCorgiMode,\n    setQuittingMessages,\n    openPrivacyNotice,\n    toggleVimEnabled,\n    setIsProcessing,\n    setBugReportConfirmationRequest,\n  );\n\n  const {\n  …  │
 │                                                                                                                                                                                                                                     │
 │    Failed to edit, 0 occurrences found for old_string in /Users/jackwoth/dev/gemini-cli/packages/cli/src/ui/App.tsx. No edits made. The exact text in old_string was not found. Ensure you're not escaping content                  │
 │    incorrectly and check whitespace, indentation, and context. Use read_file tool to verify.                                                                                                                                        │
 ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ Replace is failing. I'll read slashCommandProcessor.ts, then App.tsx, then attempt the replacement.
 ╭────────────────────────────────────────────────────────╮
 │ ✔  ReadFile ...ages/ui/hooks/slashCommandProcessor.ts  │
 ╰────────────────────────────────────────────────────────╯
✦ Read slashCommandProcessor.ts. Now reading App.tsx. Then, replacement.
 ╭──────────────────────────────────────────╮
 │ ✔  ReadFile packages/cli/src/ui/App.tsx  │
 ╰──────────────────────────────────────────╯
✦ Okay, read App.tsx. Now, I'm passing setBugReportConfirmationRequest to the useSlashCommandProcessor call.
 ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ x  replace {"file_path":"/Users/jackwoth/dev/gemini-cli/packages/cli/src/ui/App.tsx","new_string":"    toggleCorgiMode,\n    setQuittingMessages,\n    openPrivacyNotice,\n    toggleVimEnabled,\n    setIsProcessing,\n    setBu…  │
 │                                                                                                                                                                                                                                     │
 │    Failed to edit, 0 occurrences found for old_string in /Users/jackwoth/dev/gemini-cli/packages/cli/src/ui/App.tsx. No edits made. The exact text in old_string was not found. Ensure you're not escaping content                  │
 │    incorrectly and check whitespace, indentation, and context. Use read_file tool to verify.                                                                                                                                        │
 ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ Still struggling with this replacement. I'll add more context to old_string.
 ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
 │ x  replace {"file_path":"/Users/jackwoth/dev/gemini-cli/packages/cli/src/ui/App.tsx","new_string":"    openAuthDialog,\n    openEditorDialog,\n    toggleCorgiMode,\n    setQuittingMessages,\n    openPrivacyNotice,\n    toggle…  │
 │                                                                                                                                                                                                                                     │
 │    Failed to edit, 0 occurrences found for old_string in /Users/jackwoth/dev/gemini-cli/packages/cli/src/ui/App.tsx. No edits made. The exact text in old_string was not found. Ensure you're not escaping content                  │
 │    incorrectly and check whitespace, indentation, and context. Use read_file tool to verify.                                                                                                                                        │
 ╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
✦ replace is failing. I'll read App.tsx, manually add setBugReportConfirmationRequest to the useSlashCommandProcessor call, and then overwrite the file with the updated content.

Will escalate this next week to have someone take a deeper look 👍

jackwotherspoon avatar Aug 03 '25 01:08 jackwotherspoon