Skip to content

Commit 128035e

Browse files
committed
fix(filesystem): match a bare exclude name at any depth in search_files
`search_files` matched `excludePatterns` with a single `minimatch` against the path relative to the searched root, while `directory_tree` also matches a bare name at any depth (`**/<name>`) and the subtree below it (`**/<name>/**`). A caller that passed `["node_modules"]` therefore pruned a top-level `node_modules` but still walked into `packages/a/node_modules` and returned the files inside it, and the two tools disagreed about the same argument. The predicate now lives in `isExcludedPath` in lib.ts and both tools use it, so `search_files` and `directory_tree` read `excludePatterns` the same way. Wildcard patterns keep their existing meaning: they are matched against the relative path exactly as written. Signed-off-by: xubenqiang <[email protected]>
1 parent d1b59ba commit 128035e

5 files changed

Lines changed: 79 additions & 16 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@modelcontextprotocol/server-filesystem": patch
3+
---
4+
5+
`search_files` now reads `excludePatterns` the same way `directory_tree` does: a bare name such as `"node_modules"` also excludes an entry with that name at any depth and the whole subtree below it, instead of only matching it at the searched root. Previously `excludePatterns: ["node_modules"]` pruned a top-level `node_modules` but still descended into `packages/a/node_modules` and returned its files.

‎src/filesystem/__tests__/lib.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
moveFile,
2727
// Search & filtering functions
2828
searchFilesWithValidation,
29+
isExcludedPath,
2930
// File editing functions
3031
applyFileEdits,
3132
tailFile,
@@ -1211,3 +1212,31 @@ describe("Lib Functions", () => {
12111212
});
12121213
});
12131214
});
1215+
1216+
describe("isExcludedPath", () => {
1217+
it("matches a bare name at any depth, and everything below it", () => {
1218+
expect(isExcludedPath("node_modules", ["node_modules"])).toBe(true);
1219+
expect(isExcludedPath("packages/a/node_modules", ["node_modules"])).toBe(
1220+
true,
1221+
);
1222+
expect(
1223+
isExcludedPath("packages/a/node_modules/x.js", ["node_modules"]),
1224+
).toBe(true);
1225+
expect(isExcludedPath("src/index.js", ["node_modules"])).toBe(false);
1226+
});
1227+
1228+
it("does not turn a bare name into a prefix match", () => {
1229+
expect(
1230+
isExcludedPath("nested/node_modules_old/x.js", ["node_modules"]),
1231+
).toBe(false);
1232+
expect(isExcludedPath(".env", [".env"])).toBe(true);
1233+
expect(isExcludedPath(".env.local", [".env"])).toBe(false);
1234+
});
1235+
1236+
it("matches a wildcard pattern against the relative path only", () => {
1237+
// "*.log" does not cross a separator; "**/*.log" does.
1238+
expect(isExcludedPath("b.log", ["*.log"])).toBe(true);
1239+
expect(isExcludedPath("a/b.log", ["*.log"])).toBe(false);
1240+
expect(isExcludedPath("a/b.log", ["**/*.log"])).toBe(true);
1241+
});
1242+
});

‎src/filesystem/__tests__/server-tools.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2000,6 +2000,15 @@ describe("search_files", () => {
20002000
).toEqual([path.join(dir, "top.txt")]);
20012001
});
20022002

2003+
// directory_tree reads the same excludePatterns and has always matched a bare
2004+
// name at any depth; search_files only matched it at the root, so
2005+
// "deeper" pruned "sub/deeper" for the tree but not for the search.
2006+
it("matches a bare exclude name at any depth, like directory_tree", async () => {
2007+
expect(
2008+
await search({ pattern: "**/*.txt", excludePatterns: ["deeper"] }),
2009+
).toEqual([path.join(dir, "sub", "mid.txt"), path.join(dir, "top.txt")]);
2010+
});
2011+
20032012
it('says "No matches found" when nothing matches', async () => {
20042013
const result = await call(client, "search_files", {
20052014
path: dir,

‎src/filesystem/lib.ts‎

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,39 @@ export async function headFile(
924924
}
925925
}
926926

927+
/**
928+
* Matches a path relative to the operation's root against a list of exclude
929+
* patterns. `directory_tree` and `search_files` both read the same
930+
* `excludePatterns` argument, so they share this predicate: a pattern with a
931+
* wildcard is matched against the relative path as written, while a bare name
932+
* also matches that name at any depth and the whole subtree below a directory
933+
* with that name.
934+
*/
935+
/**
936+
* Matches a path relative to the operation's root against a list of exclude
937+
* patterns. `directory_tree` and `search_files` both read the same
938+
* `excludePatterns` argument, so they share this predicate: a pattern with a
939+
* wildcard is matched against the relative path as written, while a bare name
940+
* also matches that name at any depth and the whole subtree below a directory
941+
* with that name.
942+
*/
943+
export function isExcludedPath(
944+
relativePath: string,
945+
excludePatterns: readonly string[],
946+
): boolean {
947+
return excludePatterns.some((pattern) => {
948+
if (pattern.includes("*")) {
949+
return minimatch(relativePath, pattern, { dot: true });
950+
}
951+
// For files: match exact name or as part of path
952+
// For directories: match as directory path
953+
return (
954+
minimatch(relativePath, pattern, { dot: true }) ||
955+
minimatch(relativePath, `**/${pattern}`, { dot: true }) ||
956+
minimatch(relativePath, `**/${pattern}/**`, { dot: true })
957+
);
958+
});
959+
}
927960
export async function searchFilesWithValidation(
928961
rootPath: string,
929962
pattern: string,
@@ -944,9 +977,7 @@ export async function searchFilesWithValidation(
944977
// excluded subtree is never realpath'd or read (#4162): excluding a slow
945978
// provider folder keeps the walk from touching it at all.
946979
const relativePath = path.relative(rootPath, fullPath);
947-
const shouldExclude = excludePatterns.some((excludePattern) =>
948-
minimatch(relativePath, excludePattern, { dot: true }),
949-
);
980+
const shouldExclude = isExcludedPath(relativePath, excludePatterns);
950981
if (shouldExclude) continue;
951982

952983
visit();

‎src/filesystem/server.ts‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { createReadStream } from "fs";
1111
import path from "path";
1212
import { pathToFileURL } from "url";
1313
import { z } from "zod";
14-
import { minimatch } from "minimatch";
1514
import { getValidRootDirectories } from "./roots-utils.js";
1615
import {
1716
formatSize,
@@ -22,6 +21,7 @@ import {
2221
createDirectory,
2322
moveFile,
2423
searchFilesWithValidation,
24+
isExcludedPath,
2525
applyFileEdits,
2626
tailFile,
2727
headFile,
@@ -666,18 +666,7 @@ export function createServer(
666666
rootPath,
667667
path.join(currentPath, entry.name),
668668
);
669-
const shouldExclude = excludePatterns.some((pattern) => {
670-
if (pattern.includes("*")) {
671-
return minimatch(relativePath, pattern, { dot: true });
672-
}
673-
// For files: match exact name or as part of path
674-
// For directories: match as directory path
675-
return (
676-
minimatch(relativePath, pattern, { dot: true }) ||
677-
minimatch(relativePath, `**/${pattern}`, { dot: true }) ||
678-
minimatch(relativePath, `**/${pattern}/**`, { dot: true })
679-
);
680-
});
669+
const shouldExclude = isExcludedPath(relativePath, excludePatterns);
681670
if (shouldExclude) continue;
682671
// Counted after the exclude check, so excluded subtrees cost
683672
// nothing; the cap and the readdir timeout keep one call from

0 commit comments

Comments
 (0)