|
| 1 | +// An excludePatterns glob ending in "/**" prunes the directory it names (#5445). |
| 2 | +// minimatch matches "**/node_modules/**" only against paths below a |
| 3 | +// node_modules, never the directory entry itself, so search_files and |
| 4 | +// directory_tree used to realpath and readdir an excluded node_modules (breaking |
| 5 | +// the #4162 promise that an excluded slow folder is never touched) and could |
| 6 | +// still return it: search_files listed the directory, directory_tree showed it |
| 7 | +// empty. isExcludedPath now also matches such a pattern with the "/**" removed. |
| 8 | +// These tests drive both tools through a real client and assert the directory |
| 9 | +// is neither touched nor returned, then pin the predicate's rule directly. |
| 10 | + |
| 11 | +import fs from "fs/promises"; |
| 12 | +import path from "path"; |
| 13 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 14 | +import type { Client } from "@modelcontextprotocol/client"; |
| 15 | +import { |
| 16 | + call, |
| 17 | + connect, |
| 18 | + makeTempDir, |
| 19 | + quietStderr, |
| 20 | + textOf, |
| 21 | + type Connected, |
| 22 | +} from "./helpers.js"; |
| 23 | +import { isExcludedPath } from "../lib.js"; |
| 24 | + |
| 25 | +interface TreeEntry { |
| 26 | + name: string; |
| 27 | + type: "file" | "directory"; |
| 28 | + children?: TreeEntry[]; |
| 29 | +} |
| 30 | + |
| 31 | +let dir: string; |
| 32 | +let conn: Connected; |
| 33 | +let client: Client; |
| 34 | + |
| 35 | +// dir/ |
| 36 | +// index.js |
| 37 | +// node_modules/dep.js |
| 38 | +// node_modules_old/keep.js |
| 39 | +// packages/app/main.js |
| 40 | +// packages/app/node_modules/nested.js |
| 41 | +beforeEach(async () => { |
| 42 | + quietStderr(); |
| 43 | + dir = await makeTempDir("mcp-fs-exclude-glob-"); |
| 44 | + const files = [ |
| 45 | + "index.js", |
| 46 | + "node_modules/dep.js", |
| 47 | + "node_modules_old/keep.js", |
| 48 | + "packages/app/main.js", |
| 49 | + "packages/app/node_modules/nested.js", |
| 50 | + ]; |
| 51 | + for (const file of files) { |
| 52 | + await fs.mkdir(path.dirname(path.join(dir, file)), { recursive: true }); |
| 53 | + await fs.writeFile(path.join(dir, file), ""); |
| 54 | + } |
| 55 | + conn = await connect([dir]); |
| 56 | + client = conn.client; |
| 57 | +}); |
| 58 | + |
| 59 | +afterEach(async () => { |
| 60 | + vi.restoreAllMocks(); |
| 61 | + await conn.close(); |
| 62 | + await fs.rm(dir, { recursive: true, force: true }); |
| 63 | +}); |
| 64 | + |
| 65 | +const p = (posix: string) => path.join(...posix.split("/")); |
| 66 | + |
| 67 | +/** The search_files result as paths relative to `dir`, sorted. */ |
| 68 | +async function search( |
| 69 | + pattern: string, |
| 70 | + excludePatterns: string[], |
| 71 | +): Promise<string[]> { |
| 72 | + const result = await call(client, "search_files", { |
| 73 | + path: dir, |
| 74 | + pattern, |
| 75 | + excludePatterns, |
| 76 | + }); |
| 77 | + expect(result.isError).toBeFalsy(); |
| 78 | + return textOf(result) |
| 79 | + .split("\n") |
| 80 | + .map((line) => path.relative(dir, line)) |
| 81 | + .sort(); |
| 82 | +} |
| 83 | + |
| 84 | +/** Every entry (files and directories) in a directory_tree result, relative to `dir`, sorted. */ |
| 85 | +async function treeEntries(excludePatterns: string[]): Promise<string[]> { |
| 86 | + const result = await call(client, "directory_tree", { |
| 87 | + path: dir, |
| 88 | + excludePatterns, |
| 89 | + }); |
| 90 | + expect(result.isError).toBeFalsy(); |
| 91 | + const entries: string[] = []; |
| 92 | + const walk = (list: TreeEntry[], prefix: string) => { |
| 93 | + for (const entry of list) { |
| 94 | + const relative = path.join(prefix, entry.name); |
| 95 | + entries.push(relative); |
| 96 | + walk(entry.children ?? [], relative); |
| 97 | + } |
| 98 | + }; |
| 99 | + walk(JSON.parse(textOf(result)) as TreeEntry[], ""); |
| 100 | + return entries.sort(); |
| 101 | +} |
| 102 | + |
| 103 | +/** Run `action` and return every realpath/readdir target under a node_modules directory. */ |
| 104 | +async function nodeModulesTouched(action: () => Promise<unknown>) { |
| 105 | + const realpath = vi.spyOn(fs, "realpath"); |
| 106 | + const readdir = vi.spyOn(fs, "readdir"); |
| 107 | + await action(); |
| 108 | + const excluded = [ |
| 109 | + path.join(dir, "node_modules"), |
| 110 | + path.join(dir, "packages", "app", "node_modules"), |
| 111 | + ]; |
| 112 | + return [...realpath.mock.calls, ...readdir.mock.calls] |
| 113 | + .map(([target]) => target.toString()) |
| 114 | + .filter((target) => |
| 115 | + excluded.some( |
| 116 | + (root) => target === root || target.startsWith(root + path.sep), |
| 117 | + ), |
| 118 | + ); |
| 119 | +} |
| 120 | + |
| 121 | +const kept = [ |
| 122 | + "index.js", |
| 123 | + "node_modules_old", |
| 124 | + "node_modules_old/keep.js", |
| 125 | + "packages", |
| 126 | + "packages/app", |
| 127 | + "packages/app/main.js", |
| 128 | +].map(p); |
| 129 | + |
| 130 | +describe('excludePatterns ending in "/**" (#5445)', () => { |
| 131 | + it("search_files does not return the excluded directory or anything below it", async () => { |
| 132 | + expect(await search("**/*", ["**/node_modules/**"])).toEqual(kept); |
| 133 | + }); |
| 134 | + |
| 135 | + it("search_files does not return the directory when the pattern names it", async () => { |
| 136 | + const result = await call(client, "search_files", { |
| 137 | + path: dir, |
| 138 | + pattern: "**/node_modules", |
| 139 | + excludePatterns: ["**/node_modules/**"], |
| 140 | + }); |
| 141 | + expect(result.isError).toBeFalsy(); |
| 142 | + expect(textOf(result)).toBe("No matches found"); |
| 143 | + }); |
| 144 | + |
| 145 | + it("directory_tree does not list the excluded directory, even as empty", async () => { |
| 146 | + expect(await treeEntries(["**/node_modules/**"])).toEqual(kept); |
| 147 | + }); |
| 148 | + |
| 149 | + it("search_files never realpaths or reads the excluded directory (#4162)", async () => { |
| 150 | + expect( |
| 151 | + await nodeModulesTouched(() => search("**/*", ["**/node_modules/**"])), |
| 152 | + ).toEqual([]); |
| 153 | + }); |
| 154 | + |
| 155 | + it("directory_tree never realpaths or reads the excluded directory (#4162)", async () => { |
| 156 | + expect( |
| 157 | + await nodeModulesTouched(() => treeEntries(["**/node_modules/**"])), |
| 158 | + ).toEqual([]); |
| 159 | + }); |
| 160 | + |
| 161 | + it("prunes only the directory a rooted pattern names", async () => { |
| 162 | + // "node_modules/**" names the root's node_modules only; the nested one stays. |
| 163 | + expect(await search("**/*.js", ["node_modules/**"])).toEqual( |
| 164 | + [ |
| 165 | + "index.js", |
| 166 | + "node_modules_old/keep.js", |
| 167 | + "packages/app/main.js", |
| 168 | + "packages/app/node_modules/nested.js", |
| 169 | + ].map(p), |
| 170 | + ); |
| 171 | + }); |
| 172 | +}); |
| 173 | + |
| 174 | +describe('isExcludedPath with a "/**" pattern', () => { |
| 175 | + it("matches the named directory as well as the paths below it", () => { |
| 176 | + expect(isExcludedPath("node_modules", ["**/node_modules/**"])).toBe(true); |
| 177 | + expect( |
| 178 | + isExcludedPath(p("packages/app/node_modules"), ["**/node_modules/**"]), |
| 179 | + ).toBe(true); |
| 180 | + expect( |
| 181 | + isExcludedPath(p("node_modules/dep.js"), ["**/node_modules/**"]), |
| 182 | + ).toBe(true); |
| 183 | + }); |
| 184 | + |
| 185 | + it("keeps whole-name matching and the rest of wildcard semantics", () => { |
| 186 | + expect(isExcludedPath("node_modules_old", ["**/node_modules/**"])).toBe( |
| 187 | + false, |
| 188 | + ); |
| 189 | + expect(isExcludedPath(p("a/node_modules"), ["node_modules/**"])).toBe( |
| 190 | + false, |
| 191 | + ); |
| 192 | + // Only a trailing "/**" is stripped: "**" alone still matches everything, |
| 193 | + // and a pattern without the suffix is matched as written. |
| 194 | + expect(isExcludedPath("anything", ["**"])).toBe(true); |
| 195 | + expect(isExcludedPath("node_modules", ["**/node_modules/*"])).toBe(false); |
| 196 | + }); |
| 197 | +}); |
0 commit comments