fix: handle Windows drive-letter paths in grep (ripgrep) output - #123
fix: handle Windows drive-letter paths in grep (ripgrep) output#123engmohamedsalah wants to merge 1 commit into
Conversation
a7b2d49 to
d8ec995
Compare
`_parse_rg` split ripgrep output on ":" positionally, so a Windows absolute path like `C:\ws\a.py:12:def f()` parsed as file="C", line=0, with the real line number swallowed into the matched text. Every grep match on Windows (when `rg` is on PATH) came back corrupted. Pass `--with-filename --null` so ripgrep always prints the path and NUL-separates it from `line:text`, then split the path off on the NUL byte — a byte that cannot appear in a path — before parsing. `--null` defeats the drive-letter colon; `--with-filename` keeps the format universal so a single-file target isn't silently dropped. Portable: harmless on Linux/macOS, correct everywhere. Adds a platform-independent regression test that feeds a NUL-delimited drive-letter line through `_parse_rg` directly, since CI runs on Linux without `rg` and never exercises this code path otherwise. Closes andrewyng#17
d8ec995 to
0622e93
Compare
rajpratham1
left a comment
There was a problem hiding this comment.
This pull request improves cross-platform compatibility for ripgrep-based search by enabling --with-filename and --null output, allowing filenames to be separated from match data without relying on colon delimiters. The parser is updated to split on the NUL separator before extracting the line number and matched text, preventing Windows drive-letter paths (for example, C:...) from being misinterpreted as field separators. The accompanying regression test verifies the corrected parsing behavior for Windows-style absolute paths while remaining platform-independent. Based on the visible changes, the implementation is focused, addresses the parsing issue cleanly, and no blocking issues are apparent.
|
Friendly ping on this one — @rajpratham1 approved it back in July and the branch still merges cleanly ( |
|
Heads up, since this is your PR and I did not want to pile onto it: while testing this branch on Windows I ran into a second, separate problem in the same function, and I have filed it as #576. Short version is that the exclusion globs ( Your Not trying to step on this one. It should go in. Happy to pick up #576 separately once this lands, or to leave it with you if you would rather do both together. |
|
Thanks for testing this @Dhevenddra — appreciate the detailed writeup. Go ahead and leave #576 with me, I'll take it once this lands (it's in the same function so the context is fresh). |
|
Follow-up: #576 is now fixed in #578, with a regression test. It touches the same |
Issue: #17
What
Fixes
grepreturning corrupted results on Windows when ripgrep (rg) is onPATH. Closes #17.Root cause
_parse_rgparsed ripgrep output withline.split(":", 2). The search path is absolute, and on Windows an absolute path starts with a drive letter, so:split into
["C", "\\ws\\a.py", "12:def hello()"]— every match was reported as fileC, line0, with the real line number swallowed into the matched text. Linux/macOS never hit this (no drive-letter colon), and CI doesn't either: it has norg, so it uses the pure-Python fallback (_py_grep), which was already correct.Fix
Pass
--with-filename --nullso ripgrep always prints the path and NUL-separates it fromline:text, then split the path off on the NUL byte — a byte that can't appear in a path — before parsing line/text.--nulldefeats the drive-letter colon;--with-filenamekeeps the format universal so a single-file target isn't silently dropped (see note). Portable: harmless on Linux/macOS, correct everywhere. No new dependencies.Tests
test_grep_finds_matches_and_respects_globnow passes on Windows-with-rg(it was the reproducer for this bug).test_parse_rg_handles_windows_drive_letter_pathsfeeds a NUL-delimited drive-letter line straight through_parse_rg, so it runs on every platform (CI is Linux-without-rg, which otherwise never exercises this code path). I confirmed it fails against the old parser, so it's a real regression guard.Before / after (Windows 11, ripgrep 15.1.0)
(Terminal screenshot of the same before/after run attached in the comments.)
Notes
--with-filenamealso removes a pre-existing silent-drop: whenpathnames a single file (outside the documented "subdirectory" usage), ripgrep previously emitted no filename, so the parser returnedcount=0with no error. The pure-Python fallback (_py_grep) still doesn't support single-file targets (os.walkon a file yields nothing) — out of scope here and worth a separate issue if single-file search should be supported.mainwithout this change, so they're not introduced here.