Skip to content

fix(grep): handle CRLF line endings in ripgrep output parsing#3292

Open
zhoufanscut wants to merge 1 commit intocode-yeongyu:devfrom
zhoufanscut:fix/grep-crlf-parsing
Open

fix(grep): handle CRLF line endings in ripgrep output parsing#3292
zhoufanscut wants to merge 1 commit intocode-yeongyu:devfrom
zhoufanscut:fix/grep-crlf-parsing

Conversation

@zhoufanscut
Copy link
Copy Markdown

@zhoufanscut zhoufanscut commented Apr 9, 2026

Summary

  • Fix parseOutput and parseCountOutput in src/tools/grep/cli.ts to handle CRLF (\r\n) line endings from ripgrep output
  • Add unit tests for both parsers covering CRLF, LF, and empty-output cases

Problem

When the Grep tool searches files with Windows-style CRLF line endings, ripgrep preserves the \r in its stdout. The parsers split on "\n" only, leaving a trailing \r on every line. This causes the content-mode regex /^(.+?):(\d+):(.*)$/ to silently fail — the $ anchor does not match before \r in JavaScript — so all matches are dropped and the tool returns "No matches found".

The files_with_matches mode was unaffected because it uses line.trim(), which strips \r. The count mode has the same latent bug in parseCountOutput.

Reproduction

# Search a CRLF file in content mode → returns "No matches found"
grep(pattern="proxy", path="/path/to/crlf_file.py", output_mode="content")

# Same file in files_with_matches mode → works fine
grep(pattern="proxy", path="/path/to/crlf_file.py", output_mode="files_with_matches")

Fix

Change output.split("\n")output.split(/\r?\n/) in both parseOutput (line 103) and parseCountOutput (line 135). After proper splitting, the existing regexes work correctly with no further changes needed.

Changes

File Change
src/tools/grep/cli.ts split("\n")split(/\r?\n/) in parseOutput and parseCountOutput; export both functions for testability
src/tools/grep/cli.test.ts New — 7 unit tests covering CRLF content, CRLF files-only, CRLF no-trailing-\r, CRLF count, LF content, LF files-only, and empty output

Testing

  • Existing result-formatter.test.ts — 5/5 pass (no regressions)
  • New cli.test.ts — all assertions verified (import currently blocked by unrelated missing js-yaml dependency in the test runner, logic verified independently)
  • Manual reproduction confirmed: CRLF file now returns correct matches in content mode

Summary by cubic

Fix parsing of ripgrep output with CRLF line endings so content and count modes return correct results on Windows. Adds tests covering CRLF, LF, and empty-output cases.

  • Bug Fixes
    • Split lines with /\r?\n/ in parseOutput and parseCountOutput in src/tools/grep/cli.ts to remove stray \r.
    • Export both functions and add src/tools/grep/cli.test.ts covering content, files-only, and count modes for CRLF/LF and empty output.
    • Resolves "No matches found" in content mode on CRLF files; count mode handles CRLF correctly.

Written for commit 697e44a. Summary will update on new commits.

parseOutput and parseCountOutput split on "\n" only, leaving
trailing \r when ripgrep preserves CRLF from source files.  This
caused content-mode regex to silently fail, returning zero matches
for any file with Windows line endings.

Change split("\n") to split(/\r?\n/) in both functions so \r is
stripped before regex matching.  Also export the two parsers and add
unit tests covering CRLF, LF, and empty-output cases.
Copilot AI review requested due to automatic review settings April 9, 2026 15:11
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 9, 2026

All contributors have signed the CLA. Thank you! ✅
Posted by the CLA Assistant Lite bot.

Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai 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 across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Auto-approved: Fixes CRLF parsing in ripgrep output using standard regex split. Safe change with comprehensive tests and no impact on existing LF logic.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes ripgrep output parsing to correctly handle Windows-style CRLF (\r\n) line endings so matches aren’t dropped in content and count modes.

Changes:

  • Update parseOutput and parseCountOutput to split stdout with /\r?\n/ instead of "\n".
  • Export both parser functions to enable direct unit testing.
  • Add unit tests covering LF, CRLF, and empty-output cases for both parsers.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/tools/grep/cli.ts Makes stdout splitting CRLF-tolerant in both parsers; exports parsers for testability.
src/tools/grep/cli.test.ts Adds Bun unit tests validating parsing behavior for LF/CRLF and empty output.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@zhoufanscut
Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

github-actions bot added a commit that referenced this pull request Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants