Skip to content
Python

Keep ripgrep searches out of generated directories (#10)

Yashas4766e59c47c0
diff --git a/coworker/tools/search.py b/coworker/tools/search.py
index 2c63bde7..ad489e1f 100644
--- a/coworker/tools/search.py
+++ b/coworker/tools/search.py
@@ -98,6 +98,11 @@ def search_tools(workspace: str) -> list:
]
if glob:
cmd += ["--glob", glob]
+ # Do not rely solely on a workspace's .gitignore: the Python fallback
+ # always omits these generated/dependency directories too. Exclusions come
+ # last because ripgrep resolves conflicting globs with the later one winning.
+ for ignored in sorted(_IGNORE_DIRS):
+ cmd += ["--glob", f"!**/{ignored}/**"]
cmd.append(str(base))
try:
out = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
diff --git a/tests/test_code_tools.py b/tests/test_code_tools.py
index 1e25d498..0edc83b2 100644
--- a/tests/test_code_tools.py
+++ b/tests/test_code_tools.py
@@ -7,6 +7,7 @@ against temp dirs (git_log needs a real `git`, which the dev box has).
from __future__ import annotations
import subprocess
+from types import SimpleNamespace
import pytest
@@ -36,9 +37,30 @@ def test_grep_finds_matches_and_respects_glob(tmp_path):
# glob filter restricts to python files
only_py = grep(pattern="hello", glob="*.py")
assert all(m["file"].endswith(".py") for m in only_py["matches"])
+ assert not any("node_modules" in m["file"] for m in only_py["matches"])
assert only_py["matches"][0]["line"] == 1
+def test_ripgrep_uses_the_same_ignored_dirs_as_the_python_fallback(tmp_path, monkeypatch):
+ import coworker.tools.search as search
+
+ commands = []
+ monkeypatch.setattr(search.shutil, "which", lambda name: "rg")
+ monkeypatch.setattr(
+ search.subprocess,
+ "run",
+ lambda cmd, **kwargs: commands.append(cmd)
+ or SimpleNamespace(returncode=0, stdout="", stderr=""),
+ )
+
+ search_tools(str(tmp_path))[0](pattern="hello", glob="*.py")
+
+ assert commands
+ user_glob = commands[0].index("*.py")
+ for ignored in search._IGNORE_DIRS:
+ assert commands[0].index(f"!**/{ignored}/**") > user_glob
+
+
def test_grep_rejects_path_escape(tmp_path):
grep = search_tools(str(tmp_path))[0]
assert "escapes" in grep(pattern="x", path="../..")["error"]