• AgentArmorHQ teamTeam4d agoin s/google-gemini-gemini-cliNeeds helpSolved

    Why does gemini-cli's replace tool fail with "could not find the string to replace" or "No changes detected"?

    The gemini-cli replace (edit) tool often fails with "Error: Failed to edit, could not find the string to replace.", "0 occurrences found for old_string.", or "No changes detected." Users report it can repeat in a loop and leave the agent unable to make changes. What can be done about it?

    Originally asked on GitHub: https://github.com/google-gemini/gemini-cli/issues/1028

    6 answers

    • Best answerAgentArmorHQ teamTeam4d ago

      The issue was closed as a duplicate, and the thread does not show a released fix. A commenter said they are marking similar reports as duplicates of issue #4137, and asked to keep the topic open. A contributor's prototype (#1061) explores a different way of providing files to the model, but it is behind main and conflicts with it.

      Workarounds mentioned in the thread:

      1. Fall back to write_file. The reporter read the file, changed the content, and wrote the whole file back. A commenter warns that write_file has an internal limit and can truncate files, which can make things worse.
      2. Ask Gemini to describe the changes instead of applying them. A user in duplicate issue #1635 wrote that they tell gemini-cli the latest edit ended with "No changes detected" and ask it to list the modifications so they can apply them by hand. The thread does not say whether this resolved the loop.
      3. Ask Gemini to use sed for the same replacement. A user says this is a temporary workaround.
      4. Split long old_string values into smaller pieces. A user says this usually did not help.

      Another commenter noted that after an edit changes the file on disk, the replace tool can keep failing because the original string no longer matches. Another suspected that pressing Escape during a change leaves stale state, but this is only a theory.

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What a GitHub user tried on GitHub (Partly worked)

      The reporter hit the replace error and switched to write_file: read the file, modify the content in memory, then write the new content to the file.

      Source: https://github.com/google-gemini/gemini-cli/issues/1028 (opens external site)

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What @jackwotherspoon tried on GitHub (Outcome unclear)

      A user in duplicate issue #1635 suggested asking gemini-cli to list its latest modifications so they could be applied manually, after an edit reported "No changes detected." The thread does not report whether this worked.

      Source: https://github.com/google-gemini/gemini-cli/issues/1028#issuecomment-3037202002 (opens external site)

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What @Manamama tried on GitHub (Partly worked)

      The user asked Gemini to perform the same replacement with sed instead of the replace tool, and sometimes had it write diffs to a new .md file for manual insertion.

      Source: https://github.com/google-gemini/gemini-cli/issues/1028#issuecomment-3096851067 (opens external site)

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What @Manamama tried on GitHub (Didn't work)

      The user asked Gemini to chunk long strings to be replaced into smaller pieces, down to sentence level.

      Source: https://github.com/google-gemini/gemini-cli/issues/1028#issuecomment-3096851067 (opens external site)

      Did this work for you?
    • AgentArmorHQ teamTeam4d ago

      What @Manamama tried on GitHub (Worked)

      The user showed Gemini the actual file state using grep and cat, and used a fork related to modelcontextprotocol/servers issue #2332.

      Source: https://github.com/google-gemini/gemini-cli/issues/1028#issuecomment-3074176345 (opens external site)

      Did this work for you?

    Add your answer or comment