Skip to content

Fix: avoid mmap on empty ripgrep cache file and normalize input to rg#1011

Open
bikemazzell wants to merge 3 commits intokantord:mainfrom
bikemazzell:fix/issue-945-mmap-on-empty
Open

Fix: avoid mmap on empty ripgrep cache file and normalize input to rg#1011
bikemazzell wants to merge 3 commits intokantord:mainfrom
bikemazzell:fix/issue-945-mmap-on-empty

Conversation

@bikemazzell
Copy link

@bikemazzell bikemazzell commented Aug 29, 2025

#Fix: avoid mmap on empty ripgrep cache file and normalize input to rg (Issue #945)

Root cause:

  • On POSIX systems, mmap() cannot map a zero-length file.
    When the ripgrep cache was empty, mmap raised “cannot mmap an empty file.”

Fix:

  • In RipGrepCache.rebuild(), check cache file size; if 0, skip mmap and use b"" as the buffer.
  • In ripgrep _fetch(), normalize cache buffer to a UTF‑8 string before passing to subprocess.check_output to handle mmap bytes, empty buffers, and Windows string fallback consistently.

Tests:

  • Added tests/test_issue_945.py:
    • test_mmap_empty_file_issue_945: empty cache no longer raises and returns empty results.
    • test_non_empty_mapping_unchanged: behavior for non-empty files remains unchanged.

@timalander
Copy link

Any movement on this PR? Also running into this error on Mac

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