Skip to content

[Bug]: Java grep fallback searches sibling files for a single-file path #4141

Open

Description

@Htd7860

Describe the bug

When ripgrep is unavailable, LocalFilesystem.grep searches the parent directory even when path names one file. Matches from sibling files and nested directories are returned alongside that file. A file with no matches can also appear to have matches because another file contains the pattern.

To Reproduce

Run with rg absent from PATH:

Path root = Files.createTempDirectory("grep-scope-");
Files.writeString(root.resolve("target.txt"), "needle target\n");
Files.writeString(root.resolve("other.txt"), "needle other\n");
var fs = new LocalFilesystem(root, true, 10);
System.out.println(fs.grep(RuntimeContext.empty(), "needle", "/target.txt", null).matches());

Actual: matches from both /target.txt and /other.txt.

Expected behavior

A file path searches that file only. A directory path still searches recursively. This should not depend on whether ripgrep is installed.

Error messages

No exception is raised; the search succeeds with unrelated matches.

Environment

  • AgentScope Java main at c8875287 (2.1.0-SNAPSHOT)
  • Java 17, Windows 11

Additional context

javaSearch sets its walk root to basePath.getParent() for file inputs. Files.walk(basePath) already handles both files and directories, so the parent expansion is unnecessary.

I reproduced this through the public grep method in a JVM whose PATH excludes ripgrep. Six targeted tests on the original code report four failures for file scope and two passes for directory recursion/glob filtering. The tests use real temporary files and force only the ripgrep launch to fail.

The open namespace change in #4071 touches javaSearch, but keeps the same parent walk root. This report concerns single-file search scope, independently of the JSON decoding in #4135.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions