gemini-cli - ✅(Solved) Fix replace may edit the wrong similar function when approximate matching is used [1 pull requests, 1 participants]

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…
GitHub stats
google-gemini/gemini-cli#26975Fetched 2026-05-14 03:46:06
View on GitHub
Comments
0
Participants
1
Timeline
10
Reactions
0
Author
Participants
Timeline (top)
labeled ×6added_to_project_v2 ×1cross-referenced ×1issue_type_added ×1

Root Cause

In the failure case I hit, a file had two highly similar functions and the model was asked to edit one specific function. Because the provided old_string was close but not exact, approximate matching could still select the wrong block instead of failing closed.

Fix Action

Fixed

PR fix notes

PR #26976: fix(core): stop replace from editing the wrong similar block

Description (problem / solution / changelog)

Summary

This fixes a case where replace could edit the wrong similar block when it had to rely on approximate matching.

Details

  • Makes flexible matching fail instead of editing when more than one match is found and allow_multiple is not enabled.
  • Makes regex matching follow the same behavior for multiple matches.
  • Tightens fuzzy matching so it stays on the intended block instead of drifting to a similarly shaped block with different identifiers.
  • Adds regression tests for flexible, regex, and fuzzy matching around similar-block collisions.

Related Issues

Fixes #26975

How to Validate

  1. Run: npx vitest run packages/core/src/tools/edit.test.ts

  2. Run: npx prettier --check packages/core/src/tools/edit.ts packages/core/src/tools/edit.test.ts

  3. Run: npx eslint packages/core/src/tools/edit.ts packages/core/src/tools/edit.test.ts

  4. Run: npm run typecheck --workspace @google/gemini-cli-core

Expected result:

  • the edit tool test suite passes
  • formatting, lint, and typecheck pass for the touched files

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • Windows
      • npm run
      • npx
      • Docker
    • MacOS
    • Linux

Changed files

  • packages/core/src/tools/edit.test.ts (modified, +161/-3)
  • packages/core/src/tools/edit.ts (modified, +153/-23)
RAW_BUFFERClick to expand / collapse

What happened?

When replace falls back to flexible, regex, or fuzzy matching, it can behave too permissively in files that contain very similar functions or blocks.

In the failure case I hit, a file had two highly similar functions and the model was asked to edit one specific function. Because the provided old_string was close but not exact, approximate matching could still select the wrong block instead of failing closed.

That makes a single-target edit risky in cases where two code regions are structurally very similar.

What did you expect to happen?

For single replacements, replace should only succeed when the target is uniquely identified.

Expected behavior:

  • If multiple non-exact matches exist and allow_multiple is not true, the edit should fail with no file changes.
  • Fuzzy recovery should tolerate formatting and small punctuation drift, but it should not retarget an edit to a different similarly shaped function with different identifiers.
  • Ambiguity handling should be consistent across flexible, regex, and fuzzy strategies.

Client information

<details> <summary>Client Information</summary>
About Gemini CLI

CLI Version                0.44.0-nightly.20260512.g022e8baef
Git Commit                 8cda688fe
Model                      Auto (Gemini 3)
Sandbox                    no sandbox
OS                         win32
Auth Method                Signed in with Google
Tier                       Gemini Code Assist in Google One AI Pro


### Login information

_No response_

### Anything else we need to know?

_No response_

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

gemini-cli - ✅(Solved) Fix replace may edit the wrong similar function when approximate matching is used [1 pull requests, 1 participants]