bug: removed last line break in multi-line replacement #1148

Closed
opened 2026-02-16 17:29:38 -05:00 by yindo · 7 comments
Owner

Originally created by @cvzakharchenko on GitHub (Aug 7, 2025).

Originally assigned to: @thdxr on GitHub.

The image illustrates it perfectly, I asked it to replace 2 lines with another 2 lines, and it got one line break removed.

Image

Below is what opencode and Opus identify as a problem. Hope it will be helpful.

### The Problem

In the LineTrimmedReplacer (lines 161-196), when calculating the matchEndIndex, the code adds + 1 for every line in the match, including the last one:

for (let k = 0; k < searchLines.length; k++) {
  matchEndIndex += originalLines[i + k].length + 1; // BUG: Always adds +1

This means when matching a single line like "line2" in "line1\nline2\nline3", it extracts "line2\n" instead of just "line2". The extra newline then gets replaced along with the content.
Originally created by @cvzakharchenko on GitHub (Aug 7, 2025). Originally assigned to: @thdxr on GitHub. The image illustrates it perfectly, I asked it to replace 2 lines with another 2 lines, and it got one line break removed. <img width="1010" height="950" alt="Image" src="https://github.com/user-attachments/assets/fe5ab027-9cf2-48b3-80c2-6b52a46a1d68" /> Below is what opencode and Opus identify as a problem. Hope it will be helpful. ``` ### The Problem In the LineTrimmedReplacer (lines 161-196), when calculating the matchEndIndex, the code adds + 1 for every line in the match, including the last one: for (let k = 0; k < searchLines.length; k++) { matchEndIndex += originalLines[i + k].length + 1; // BUG: Always adds +1 This means when matching a single line like "line2" in "line1\nline2\nline3", it extracts "line2\n" instead of just "line2". The extra newline then gets replaced along with the content. ```
yindo closed this issue 2026-02-16 17:29:38 -05:00
Author
Owner

@rekram1-node commented on GitHub (Aug 7, 2025):

Can you replicate consistently? This may just be the llm messing it's tool call for editing no?

See: https://opencode.ai/s/8Yxo0NXb

@rekram1-node commented on GitHub (Aug 7, 2025): Can you replicate consistently? This may just be the llm messing it's tool call for editing no? See: https://opencode.ai/s/8Yxo0NXb
Author
Owner

@cvzakharchenko commented on GitHub (Aug 7, 2025):

Can you replicate consistently? This may just be the llm messing it's tool call for editing no?

See: https://opencode.ai/s/8Yxo0NXb

5 times in a row. But it might be because I have windows line endings? Tried opencode in WSL and a native for Windows.

@cvzakharchenko commented on GitHub (Aug 7, 2025): > Can you replicate consistently? This may just be the llm messing it's tool call for editing no? > > See: https://opencode.ai/s/8Yxo0NXb 5 times in a row. But it might be because I have windows line endings? Tried opencode in WSL and a native for Windows.
Author
Owner

@cvzakharchenko commented on GitHub (Aug 7, 2025):

Code edits are always messy too.

@cvzakharchenko commented on GitHub (Aug 7, 2025): Code edits are always messy too.
Author
Owner

@cvzakharchenko commented on GitHub (Aug 9, 2025):

I tried it again on a file with just LF line endings, and it works fine. But replacing multiple lines in a file with CR LF line endings always eats up the last line break. Replacing a single line works fine too though.
https://opencode.ai/s/6Ko9i9nz

@cvzakharchenko commented on GitHub (Aug 9, 2025): I tried it again on a file with just LF line endings, and it works fine. But replacing multiple lines in a file with CR LF line endings always eats up the last line break. Replacing a single line works fine too though. https://opencode.ai/s/6Ko9i9nz
Author
Owner

@cvzakharchenko commented on GitHub (Aug 10, 2025):

I believe there is a simple fix in LineTrimmedReplacer, similar to what BlockAnchorReplacer already does:
https://github.com/sst/opencode/pull/1777/commits/66738dcb6aeedba32e625b25ed291e409633e749

I tried it, both linux and windows files now work fine for me.

@cvzakharchenko commented on GitHub (Aug 10, 2025): I believe there is a simple fix in LineTrimmedReplacer, similar to what BlockAnchorReplacer already does: https://github.com/sst/opencode/pull/1777/commits/66738dcb6aeedba32e625b25ed291e409633e749 I tried it, both linux and windows files now work fine for me.
Author
Owner

@rekram1-node commented on GitHub (Aug 10, 2025):

awesome!

@rekram1-node commented on GitHub (Aug 10, 2025): awesome!
Author
Owner

@rekram1-node commented on GitHub (Aug 11, 2025):

Can this be closed thanks to your pr being part of latest release @cvzakharchenko ?

@rekram1-node commented on GitHub (Aug 11, 2025): Can this be closed thanks to your pr being part of latest release @cvzakharchenko ?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#1148