Skip to content
Python

fix: search workspaces under ignored directory names (#576)

Mohamed Salah530058341340
diff --git a/coworker/sandbox/runner/tools_grep.py b/coworker/sandbox/runner/tools_grep.py
index 75ba8155..b51d7c32 100644
--- a/coworker/sandbox/runner/tools_grep.py
+++ b/coworker/sandbox/runner/tools_grep.py
@@ -87,26 +87,36 @@ def grep(
# 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))
+ # Search from the workspace itself: ripgrep resolves --glob patterns against paths
+ # relative to the *current working directory*, so passing an absolute path made the
+ # exclusion globs match the workspace's ancestors too — a workspace under e.g.
+ # .../node_modules/ excluded itself entirely (issue #576). With cwd anchored to the
+ # search root and "." as the target, only directory names inside the workspace can match.
+ cmd.append(".")
try:
- out = subprocess.run(cmd, capture_output=True, text=True, timeout=30)
+ out = subprocess.run(cmd, capture_output=True, text=True, timeout=30, cwd=str(base))
except Exception as exc:
return {"error": f"grep failed: {exc}"}
if out.returncode not in (0, 1): # 1 = no matches
return {"error": (out.stderr or "ripgrep error").strip()[:300]}
- return {"engine": "ripgrep", **_parse_rg(out.stdout, root, n)}
+ return {"engine": "ripgrep", **_parse_rg(out.stdout, root, n, base=base)}
return {"engine": "python", **_py_grep(root, base, pattern, glob, n)}
-def _rel(path: str, root: Path) -> str:
+def _rel(path: str, root: Path, base: Path | None = None) -> str:
try:
- return str(Path(path).resolve().relative_to(root))
+ p = Path(path)
+ # ripgrep emits paths relative to its own cwd (the search base); resolve them against
+ # that, not the process cwd, before relativizing.
+ if not p.is_absolute() and base is not None:
+ p = base / p
+ return str(p.resolve().relative_to(root))
except (ValueError, OSError):
return path
-def _parse_rg(stdout: str, root: Path, n: int) -> dict[str, Any]:
+def _parse_rg(stdout: str, root: Path, n: int, base: Path | None = None) -> dict[str, Any]:
# `rg --null` emits each match as `<path>\0<line>:<text>`. Splitting the path off
# on the NUL byte first means the colon inside a Windows drive letter (C:\...) is
# never confused with the line/text separators — the old `split(":", 2)` parsed
@@ -119,7 +129,7 @@ def _parse_rg(stdout: str, root: Path, n: int) -> dict[str, Any]:
ln, _, txt = rest.partition(":")
matches.append(
{
- "file": _rel(path, root),
+ "file": _rel(path, root, base),
"line": int(ln) if ln.isdigit() else 0,
"text": txt[:300],
}
diff --git a/tests/test_code_tools.py b/tests/test_code_tools.py
index cb9e14e9..6922c9d9 100644
--- a/tests/test_code_tools.py
+++ b/tests/test_code_tools.py
@@ -59,6 +59,19 @@ def test_parse_rg_handles_windows_drive_letter_paths():
assert m["file"].endswith("a.py")
+def test_grep_works_when_workspace_sits_under_an_ignored_dir_name(tmp_path):
+ # Regression for #576: ripgrep matches --glob patterns against paths
+ # relative to the *current working directory*, not the search root, so a
+ # workspace living under e.g. .../node_modules/ws excluded itself entirely.
+ ws = tmp_path / "node_modules" / "ws"
+ ws.mkdir(parents=True)
+ (ws / "a.py").write_text("hello under ignored parent\n", encoding="utf-8")
+ grep = search_tools(str(ws))[0]
+ out = grep(pattern="hello")
+ assert out["count"] == 1
+ assert out["matches"][0]["file"] == "a.py"
+
+
def test_ripgrep_uses_the_same_ignored_dirs_as_the_python_fallback(tmp_path, monkeypatch):
import coworker.tools.search as search