From c56fa1a673a59aa0c12ef900be0f03365e4d2c1d Mon Sep 17 00:00:00 2001 From: VascoSch92 Date: Fri, 6 Nov 2026 11:58:34 +0100 Subject: [PATCH] fix(tools): pass the ripgrep pattern with -e so it cannot be read as a flag The ripgrep backend put the search pattern straight into argv, so a pattern starting with "-" was parsed as options: "-foo" failed as "-f oo" and "--files" listed every file instead of searching. Passing it as the value of -e makes ripgrep always treat it as a pattern. Ports the pinned tests test_pattern_starting_with_dash (ripgrep case) and test_ripgrep_pattern_cannot_inject_flags from #6348 without their xfail markers. --- openhands-tools/openhands/tools/grep/impl.py | 2 ++ tests/tools/grep/test_grep_executor.py | 23 +++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/openhands-tools/openhands/tools/grep/impl.py b/openhands-tools/openhands/tools/grep/impl.py index 86f9ab55dd9..ee27ad59cd9 100644 --- a/openhands-tools/openhands/tools/grep/impl.py +++ b/openhands-tools/openhands/tools/grep/impl.py @@ -231,6 +231,8 @@ def _execute_with_ripgrep( "rg", "-l", "-i", + # -e keeps a pattern that starts with "-" from being read as a flag. + "-e", action.pattern, str(search_path), "--sortr=modified", diff --git a/tests/tools/grep/test_grep_executor.py b/tests/tools/grep/test_grep_executor.py index 3e6f2f0f9f9..75b0e6f17bd 100644 --- a/tests/tools/grep/test_grep_executor.py +++ b/tests/tools/grep/test_grep_executor.py @@ -15,7 +15,7 @@ import openhands.tools.grep.impl as grep_impl from openhands.tools.grep import GrepAction from openhands.tools.grep.impl import GrepExecutor -from openhands.tools.utils import _check_grep_available +from openhands.tools.utils import _check_grep_available, _check_ripgrep_available def test_grep_executor_initialization(): @@ -249,6 +249,27 @@ def test_grep_executor_invalid_regex(): assert "Invalid regex pattern" in observation.text +@pytest.mark.skipif(not _check_ripgrep_available(), reason="ripgrep not available") +def test_pattern_starting_with_dash(tmp_path): + (tmp_path / "flags.txt").write_text("run with -foo enabled\n") + (tmp_path / "other.txt").write_text("nothing here\n") + + observation = GrepExecutor(working_dir=str(tmp_path))(GrepAction(pattern="-foo")) + + assert observation.is_error is False + assert [Path(match).name for match in observation.matches] == ["flags.txt"] + + +@pytest.mark.skipif(not _check_ripgrep_available(), reason="ripgrep not available") +def test_ripgrep_pattern_cannot_inject_flags(tmp_path): + (tmp_path / "a.txt").write_text("alpha\n") + (tmp_path / "b.txt").write_text("beta\n") + + observation = GrepExecutor(working_dir=str(tmp_path))(GrepAction(pattern="--files")) + + assert observation.matches == [] + + def test_grep_executor_concurrent(): """Test that concurrent grep calls return correct results.