Skip to content

The agent that grows with you

fix(search): report unreadable subtrees in the pruned grep fallback

Byrd-ITff20443d69b9
diff --git a/tests/tools/test_search_grep_pruned_permissions.py b/tests/tools/test_search_grep_pruned_permissions.py
new file mode 100644
index 00000000000..a1ccbabc9e8
--- /dev/null
+++ b/tests/tools/test_search_grep_pruned_permissions.py
@@ -0,0 +1,137 @@
+"""Live regression tests: the pruned grep fallback reports unreadable subtrees.
+
+With ``rg`` unavailable, a search rooted under a dot-directory takes the
+``find ... -prune -exec grep {} +`` lane. That lane used to discard stderr
+(``2>/dev/null``), so a readable match next to an unreadable subtree came back
+as a confident, warning-free result (find exits 1 with a nonempty payload and
+its ``find: '<dir>': Permission denied`` diagnostic never reached the parser).
+
+Contract: the matches (or counts) are returned AND the skipped path is named in
+``warning``; clean searches stay warning-free.
+"""
+
+import os
+
+import pytest
+
+from tools.environments.local import LocalEnvironment
+from tools.file_operations import ShellFileOperations
+from tools.file_operations_search import (
+ _extract_unreadable_paths,
+ _is_permission_only_error,
+ _split_tool_diagnostics,
+)
+
+_LIVE = [
+ pytest.mark.platforms("posix"),
+ pytest.mark.skipif(
+ hasattr(os, "geteuid") and os.geteuid() == 0,
+ reason="root reads chmod 000 dirs",
+ ),
+]
+
+
+def live(func):
+ """Real find/grep on a real mode-000 directory (POSIX, non-root only)."""
+ for mark in reversed(_LIVE):
+ func = mark(func)
+ return func
+
+
+def _grep_only_ops(root, monkeypatch):
+ ops = ShellFileOperations(LocalEnvironment(str(root)), cwd=str(root))
+ # rg absent, grep present: forces the grep lane, and a hidden root forces
+ # its pruned (find ... -exec grep) variant.
+ monkeypatch.setattr(ops, "_has_command", lambda command: command == "grep")
+ return ops
+
+
+def hidden_root(tmp_path):
+ root = tmp_path / ".hidden-root"
+ (root / "open").mkdir(parents=True)
+ (root / "open" / "a.txt").write_text("needle one\n")
+ return root
+
+
+def locked_dir(hidden_root):
+ locked = hidden_root / "locked"
+ locked.mkdir()
+ (locked / "b.txt").write_text("needle two\n")
+ os.chmod(locked, 0o000)
+ try:
+ yield locked
+ finally:
+ os.chmod(locked, 0o755)
+
+
+@live
+def test_content_mode_partial_results_name_unreadable_subtree(
+ hidden_root, locked_dir, monkeypatch):
+ result = _grep_only_ops(hidden_root, monkeypatch).search(
+ "needle", path=str(hidden_root), target="content")
+
+ assert result.error is None
+ assert [m.path for m in result.matches] == [str(hidden_root / "open" / "a.txt")]
+ assert "unreadable path" in (result.warning or "")
+ assert str(locked_dir) in (result.warning or "")
+
+
+@live
+def test_count_mode_partial_results_name_unreadable_subtree(
+ hidden_root, locked_dir, monkeypatch):
+ result = _grep_only_ops(hidden_root, monkeypatch).search(
+ "needle", path=str(hidden_root), target="content", output_mode="count")
+
+ assert result.error is None
+ assert result.counts == {str(hidden_root / "open" / "a.txt"): 1}
+ assert "unreadable path" in (result.warning or "")
+ assert str(locked_dir) in (result.warning or "")
+
+
+@live
+def test_no_match_beside_unreadable_subtree_still_warns(
+ hidden_root, locked_dir, monkeypatch):
+ result = _grep_only_ops(hidden_root, monkeypatch).search(
+ "absent-token", path=str(hidden_root), target="content")
+
+ assert result.error is None
+ assert result.total_count == 0
+ assert str(locked_dir) in (result.warning or "")
+
+
+@live
[email protected]("output_mode", ["content", "count"])
+def test_clean_match_has_no_warning(hidden_root, monkeypatch, output_mode):
+ result = _grep_only_ops(hidden_root, monkeypatch).search(
+ "needle", path=str(hidden_root), target="content", output_mode=output_mode)
+
+ assert result.error is None
+ assert result.total_count == 1
+ assert result.warning is None
+
+
+@live
[email protected]("output_mode", ["content", "count"])
+def test_clean_no_match_has_no_warning(hidden_root, monkeypatch, output_mode):
+ result = _grep_only_ops(hidden_root, monkeypatch).search(
+ "absent-token", path=str(hidden_root), target="content", output_mode=output_mode)
+
+ assert result.error is None
+ assert result.total_count == 0
+ assert result.warning is None
+
+
[email protected]("quoted", ["\u2018/r/locked\u2019", "'/r/locked'"])
+def test_find_permission_diagnostic_is_recognized_in_both_quote_styles(quoted):
+ line = f"find: {quoted}: Permission denied"
+
+ assert _is_permission_only_error(line)
+ assert _extract_unreadable_paths(line) == ["/r/locked"]
+ assert _split_tool_diagnostics(f"{line}\n/r/a.txt:1:needle") == (
+ line, "/r/a.txt:1:needle")
+
+
+def test_unrecognized_find_diagnostic_is_not_a_permission_error():
+ assert not _is_permission_only_error("find: \u2018/r\u2019: No such file or directory")
diff --git a/tools/file_operations_search.py b/tools/file_operations_search.py
index 6e398387c13..da8c7e8a671 100644
--- a/tools/file_operations_search.py
+++ b/tools/file_operations_search.py
@@ -148,7 +148,7 @@ def _split_tool_diagnostics(output: str) -> tuple[str, str]:
if not line.strip():
continue
# Prefix check first: a match path can contain "-<digit>" (".../pytest-686/...").
- if line.lstrip().startswith(("rg: ", "grep: ")):
+ if line.lstrip().startswith(("rg: ", "grep: ", "find: ")):
diagnostics.append(line)
elif line == "--" or _SEARCH_OUTPUT_RE.match(line):
payload.append(line)
@@ -161,12 +161,17 @@ def _split_tool_diagnostics(output: str) -> tuple[str, str]:
# rg: ``rg: <path>: Permission denied (os error 13)``
# rg modified-sort walk: ``rg: <path>: IO error for operation on <path>: Permission denied (os error 13)``
# grep: ``grep: <path>: Permission denied`` (no ``(os error N)`` suffix)
+# find: ``find: '<path>': Permission denied`` (pruned grep lane; GNU find quotes the
+# path with ASCII ``'`` or, in a UTF-8 locale, with typographic quotes)
# The ``(os error N)`` suffix is ripgrep's Rust io::Error spelling, so it is optional.
_PERMISSION_DENIED_RE = re.compile(
- r"^(?:(?:rg|grep): )?(?P<path>.+?): "
+ r"^(?:(?P<tool>rg|grep|find): )?(?P<path>.+?): "
r"(?:IO error for operation on .+?: )?Permission denied(?: \(os error \d+\))?$")
+_FIND_QUOTES_RE = re.compile(r"^['\u2018]|['\u2019]$")
+
+
def _is_permission_only_error(diagnostics: str) -> bool:
"""True when every diagnostic line reports an unreadable path (rg or grep).
That is a partial-success condition — the tool still emits results for the
@@ -183,6 +188,8 @@ def _extract_unreadable_paths(diagnostics: str) -> list[str]:
if not match:
continue
path = match.group("path").strip()
+ if match.group("tool") == "find":
+ path = _FIND_QUOTES_RE.sub("", path)
if path and path not in paths:
paths.append(path)
return paths
@@ -1256,5 +1263,8 @@ class SearchMixin:
find_parts.extend([self._hidden_prune_expr([q_root]), "-o", "-type f"])
if file_glob:
find_parts.extend(["-name", self._escape_shell_arg(file_glob)])
- find_parts.extend(["-exec", *grep_parts, "{}", "+", "2>/dev/null"])
+ # stderr stays attached (``_exec`` merges it into stdout): find's
+ # ``Permission denied`` lines must reach the parser so an unreadable
+ # subtree beside a readable match is reported, not silently dropped.
+ find_parts.extend(["-exec", *grep_parts, "{}", "+"])
return self._run_search_pipeline(find_parts, output_mode, limit, offset, context, line_cap=True)