Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Sep 22, 2025

Summary

This PR fixes Issue #8020 where apply_diff fails to strip line numbers from the last line in SEARCH and REPLACE blocks, particularly in Windows environments with CRLF line endings.

Root Cause

The bug occurs due to an interaction between the regex parser and line number stripping:

  1. The regex in multi-search-replace.ts uses (?:\n)? which optionally consumes trailing newlines from captured content
  2. In Windows environments with CRLF endings (\r\n), this can leave content ending with just \r (consuming the \n)
  3. When stripLineNumbers() processes this malformed content, it splits by /\r?\n/ but the line structure is broken
  4. The last line's line number prefix (e.g., 1488 |) cannot be matched and stripped properly
  5. Result: Line numbers appear in the final output

Example of the bug:

Input to stripLineNumbers: "1488 | code here\r"  (missing the \n from \r\n)
After split(/\r?\n/): ["1488 | code here\r"]  (single element, no proper line split)
After strip attempt: "1488 | code here\r"  (line number remains!)

Solution

Modified stripLineNumbers() in src/integrations/misc/extract-text.ts to preserve trailing newlines when present in the original content:

// Preserve trailing newline if original content had one
if (content.endsWith("\n") || content.endsWith("\r\n")) {
    if (!result.endsWith("\n") && !result.endsWith("\r\n")) {
        result += lineEnding
    }
}

This ensures that:

  • Line structure is maintained through the stripping process
  • Both LF and CRLF line endings are correctly handled
  • The line number regex patterns can properly match and remove prefixes from all lines including the last one

Why Not Fix the Regex Directly?

@daniel-lxs suggested modifying the regex pattern to use lookahead assertions to avoid consuming trailing newlines. However, this approach broke existing deletion tests where the replace block is empty. The current solution is more robust and doesn't require making the already complex regex even more fragile.

Changes

  • Modified stripLineNumbers() to preserve trailing newlines when present in the original content
  • Added comprehensive test cases for trailing newline preservation scenarios in Windows (CRLF) and Unix (LF) environments
  • All existing tests pass without regression

Testing

Fixes #8020

- Modified stripLineNumbers function to preserve trailing newlines
- This fixes issue #8020 where line numbers at the last line were not being stripped correctly
- Added comprehensive test cases for trailing newline preservation
- All existing tests pass without regression
@roomote roomote bot requested review from cte, jr and mrubens as code owners September 22, 2025 20:56
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Sep 22, 2025
@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Sep 22, 2025
@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Review] in Roo Code Roadmap Sep 22, 2025
roomote[bot]

This comment was marked as outdated.

@hannesrudolph hannesrudolph added PR - Needs Review and removed Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. labels Sep 22, 2025
@daniel-lxs daniel-lxs moved this from PR [Needs Review] to PR [Needs Prelim Review] in Roo Code Roadmap Sep 26, 2025
@daniel-lxs daniel-lxs moved this from PR [Needs Prelim Review] to PR [Needs Review] in Roo Code Roadmap Oct 23, 2025
@roomote
Copy link
Contributor Author

roomote bot commented Oct 24, 2025

✅ Review Complete - No Issues Found

I've completed a thorough review of this pull request and found no issues. The implementation correctly fixes the trailing newline preservation bug in stripLineNumbers(), and all previous review feedback has been addressed.

Summary

Code Quality: Implementation is clean and follows existing patterns
Test Coverage: Comprehensive tests cover all scenarios including CRLF/LF handling
Bug Fix: Correctly addresses issue #8020
Review Feedback: All previous suggestions have been incorporated

The PR is ready for approval by a human reviewer.


Follow Along on Roo Code Cloud

@mrubens mrubens merged commit ad56791 into main Oct 24, 2025
7 of 9 checks passed
@mrubens mrubens deleted the fix/apply-diff-trailing-newline-regex branch October 24, 2025 14:16
@github-project-automation github-project-automation bot moved this from PR [Needs Review] to Done in Roo Code Roadmap Oct 24, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Oct 24, 2025
Copy link
Contributor Author

@roomote roomote bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working PR - Needs Review size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

[BUG] apply_diff fail to strip the line number at last line in SEARCH and REPLACE

4 participants