Skip to content

fix(RemoteFilesystem): 添加glob时无法匹配搜索目录根层文件#2343

Merged
jujn merged 1 commit into
agentscope-ai:mainfrom
CyberXavier:fix/remote-filesystem-glob-root-files
Jul 22, 2026
Merged

fix(RemoteFilesystem): 添加glob时无法匹配搜索目录根层文件#2343
jujn merged 1 commit into
agentscope-ai:mainfrom
CyberXavier:fix/remote-filesystem-glob-root-files

Conversation

@CyberXavier

@CyberXavier CyberXavier commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

fix #2342

原因分析

RemoteFilesystem.glob() 当前创建了两个 PathMatcher

PathMatcher matcher =
    FileSystems.getDefault()
        .getPathMatcher(
            "glob:"
                + (effectivePattern.startsWith("**")
                    ? effectivePattern
                    : "**/" + effectivePattern)
        );

PathMatcher directMatcher =
    FileSystems.getDefault()
        .getPathMatcher("glob:" + effectivePattern);

当调用参数为:

effectivePattern = **/hello.txt

两个匹配器实际上完全相同:

matcher       = glob:**/hello.txt
directMatcher = glob:**/hello.txt

在 Windows JDK 17 中,该模式会被编译为类似正则:

^.*\\hello\.txt$

它要求 hello.txt 前面至少存在一个路径分隔符,因此:

hello.txt          不匹配
reports\hello.txt  匹配

可以通过以下代码验证:

PathMatcher matcher =
    FileSystems.getDefault()
        .getPathMatcher("glob:**/hello.txt");

assertFalse(matcher.matches(Path.of("hello.txt")));
assertTrue(matcher.matches(Path.of("reports/hello.txt")));

RemoteFilesystem.glob() 在搜索根路径 / 时,会把 Store key:

/hello.txt

正确转换为相对路径:

hello.txt

但随后使用 glob:**/hello.txt 进行匹配,导致该文件被过滤。

相关实现对比

CompositeFilesystem.glob() 已经处理了相同的 Java NIO glob 行为:

String directExpr;
if (effectivePattern.startsWith("**/")) {
    directExpr = effectivePattern.substring(3);
} else if (effectivePattern.equals("**")) {
    directExpr = "*";
} else {
    directExpr = effectivePattern;
}

PathMatcher directFileMatcher =
    FileSystems.getDefault()
        .getPathMatcher("glob:" + directExpr);

RemoteFilesystem.glob() 没有采用相同逻辑,因此其中的 directMatcher 无法匹配根层文件。

修复

RemoteFilesystem.glob() 中为直接匹配器去掉开头的 **/

String directExpr;
if (effectivePattern.startsWith("**/")) {
    directExpr = effectivePattern.substring(3);
} else if (effectivePattern.equals("**")) {
    directExpr = "*";
} else {
    directExpr = effectivePattern;
}

PathMatcher directMatcher =
    FileSystems.getDefault()
        .getPathMatcher("glob:" + directExpr);

这样对于:

effectivePattern = **/hello.txt

可以得到:

matcher       = glob:**/hello.txt
directMatcher = glob:hello.txt

从而同时匹配:

hello.txt
reports/hello.txt

- 添加了测试用例验证**模式匹配根目录和子目录中的文件
- 添加了测试用例验证双星号模式匹配根目录和子目录中的所有文件
- 实现了对以**开头的模式进行直接路径匹配的逻辑
- 确保递归匹配器能够正确匹配搜索根目录中的文件
@CyberXavier

Copy link
Copy Markdown
Contributor Author

@jujn 我提交了一个bug fix,我们生产使用时遇到的问题,我提交的issue中有详细的问题说明和复现过程,pr中有问题原因分析和修复方案。

@codecov

codecov Bot commented Jul 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@oss-maintainer oss-maintainer left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Changes look good.


Automated review by github-manager-bot

@CyberXavier

Copy link
Copy Markdown
Contributor Author

@jujn 我貌似没有合并权限,能否麻烦您帮忙操作合并一下,万分感谢!

@jujn

jujn commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

@jujn 我貌似没有合并权限,能否麻烦您帮忙操作合并一下,万分感谢!

OK. I'll watch it tonight

@jujn jujn left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jujn
jujn merged commit 2dd6823 into agentscope-ai:main Jul 22, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]:RemoteFilesystem.glob() 使用 **/ 模式时无法匹配搜索目录根层文件

3 participants