Skip to content

grep tool silently returns no results on Windows — shlex.split() strips backslashes #259

@arnonuem

Description

@arnonuem

Problem

On Windows, the grep tool (_grep in code_puppy/tools/file_operations.py) silently returns zero matches whenever the search string contains a backslash — which on Windows is basically always (regex escapes like \b, \d, \w, ., or literal paths like C:\Users...).

Root Cause

Line ~654 of code_puppy/tools/file_operations.py:

​`python parts = shlex.split(search_string) ​`

shlex.split() defaults to POSIX mode, where \ is an escape character. So the search term gets mangled before it ever reaches ripgrep:

Input

  • \bdef\b
  • \d+
  • C:\Users\me
  • foo\.bar

shlex.split() output

  • bdefb
  • C:Usersme
  • foo.bar

ripgrep then dutifully searches for the mangled string, finds nothing, and returns rc=1 (no matches).
The user sees an empty result and assumes grep is broken.

Why nobody noticed

_grep() never inspects result.returncode or result.stderr, so any error from ripgrep (bad regex, missing ignore file, etc.) is silently swallowed.
Only result.stdout is parsed.

Suggested fix

Two small changes in _grep():

  1. Keep backslashes intact:
    ​`python
    try:
    parts = shlex.split(search_string, posix=False)

    Strip the surrounding quotes posix=False leaves behind

    parts = [
    p[1:-1] if len(p) >= 2 and p[0] == p[-1] and p[0] in ('"', "'") else p
    for p in parts
    ]
    except ValueError:
    parts = [search_string]
    ​`

  2. Surface ripgrep errors instead of hiding them:
    python if result.returncode not in (0, 1): # 0 = matches, 1 = no matches error_message = f"ripgrep failed (rc={result.returncode}): {result.stderr.strip()[:500]}" ​

Reproduction

On Windows:

python grep("\\bdef\\b", "code_puppy") # returns 0 matches grep("def grep", "code_puppy") # works (no backslashes) ​

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions