|
| 1 | +// search_files and directory_tree read `excludePatterns` the same way (#5216). |
| 2 | +// directory_tree matched a bare name such as "node_modules" at any depth and |
| 3 | +// pruned the subtree below it, while search_files matched every pattern only |
| 4 | +// against the path relative to the searched root, so the same argument left a |
| 5 | +// nested node_modules in one tool's result and not the other's. Both now go |
| 6 | +// through isExcludedPath. These tests drive both tools over one tree through a |
| 7 | +// real client, check that a nested excluded directory is still never realpath'd |
| 8 | +// or read (#4162), and pin the predicate's rules directly: whole-name matching, |
| 9 | +// and wildcard patterns matched as written. |
| 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 | +// packages/app/debug.log |
| 42 | +// debug.log |
| 43 | +beforeEach(async () => { |
| 44 | + quietStderr(); |
| 45 | + dir = await makeTempDir("mcp-fs-exclude-"); |
| 46 | + const files = [ |
| 47 | + "index.js", |
| 48 | + "debug.log", |
| 49 | + "node_modules/dep.js", |
| 50 | + "node_modules_old/keep.js", |
| 51 | + "packages/app/main.js", |
| 52 | + "packages/app/debug.log", |
| 53 | + "packages/app/node_modules/nested.js", |
| 54 | + ]; |
| 55 | + for (const file of files) { |
| 56 | + await fs.mkdir(path.dirname(path.join(dir, file)), { recursive: true }); |
| 57 | + await fs.writeFile(path.join(dir, file), ""); |
| 58 | + } |
| 59 | + conn = await connect([dir]); |
| 60 | + client = conn.client; |
| 61 | +}); |
| 62 | + |
| 63 | +afterEach(async () => { |
| 64 | + vi.restoreAllMocks(); |
| 65 | + await conn.close(); |
| 66 | + await fs.rm(dir, { recursive: true, force: true }); |
| 67 | +}); |
| 68 | + |
| 69 | +/** The search_files result as paths relative to `dir`, sorted. */ |
| 70 | +async function search( |
| 71 | + pattern: string, |
| 72 | + excludePatterns: string[], |
| 73 | +): Promise<string[]> { |
| 74 | + const result = await call(client, "search_files", { |
| 75 | + path: dir, |
| 76 | + pattern, |
| 77 | + excludePatterns, |
| 78 | + }); |
| 79 | + expect(result.isError).toBeFalsy(); |
| 80 | + return textOf(result) |
| 81 | + .split("\n") |
| 82 | + .map((line) => path.relative(dir, line)) |
| 83 | + .sort(); |
| 84 | +} |
| 85 | + |
| 86 | +/** Every file path in a directory_tree result, relative to `dir`, sorted. */ |
| 87 | +async function treeFiles(excludePatterns: string[]): Promise<string[]> { |
| 88 | + const result = await call(client, "directory_tree", { |
| 89 | + path: dir, |
| 90 | + excludePatterns, |
| 91 | + }); |
| 92 | + expect(result.isError).toBeFalsy(); |
| 93 | + const files: string[] = []; |
| 94 | + const walk = (entries: TreeEntry[], prefix: string) => { |
| 95 | + for (const entry of entries) { |
| 96 | + const relative = path.join(prefix, entry.name); |
| 97 | + if (entry.type === "file") files.push(relative); |
| 98 | + walk(entry.children ?? [], relative); |
| 99 | + } |
| 100 | + }; |
| 101 | + walk(JSON.parse(textOf(result)) as TreeEntry[], ""); |
| 102 | + return files.sort(); |
| 103 | +} |
| 104 | + |
| 105 | +describe("search_files excludePatterns (#5216)", () => { |
| 106 | + it("excludes a bare name at any depth, not only at the searched root", async () => { |
| 107 | + expect(await search("**/*.js", ["node_modules"])).toEqual( |
| 108 | + ["index.js", "node_modules_old/keep.js", "packages/app/main.js"].map( |
| 109 | + (p) => path.join(...p.split("/")), |
| 110 | + ), |
| 111 | + ); |
| 112 | + }); |
| 113 | + |
| 114 | + it("returns the same files directory_tree lists for the same excludePatterns", async () => { |
| 115 | + for (const excludePatterns of [ |
| 116 | + ["node_modules"], |
| 117 | + ["app"], |
| 118 | + ["*.log"], |
| 119 | + ["**/*.log", "node_modules"], |
| 120 | + ]) { |
| 121 | + expect(await search("**/*.*", excludePatterns)).toEqual( |
| 122 | + await treeFiles(excludePatterns), |
| 123 | + ); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + it("never realpaths or reads a nested excluded directory (#4162)", async () => { |
| 128 | + const realpath = vi.spyOn(fs, "realpath"); |
| 129 | + const readdir = vi.spyOn(fs, "readdir"); |
| 130 | + await search("**/*.js", ["node_modules"]); |
| 131 | + const nested = path.join(dir, "packages", "app", "node_modules"); |
| 132 | + const touched = [...realpath.mock.calls, ...readdir.mock.calls].map( |
| 133 | + ([target]) => target.toString(), |
| 134 | + ); |
| 135 | + expect(touched.filter((target) => target.startsWith(nested))).toEqual([]); |
| 136 | + }); |
| 137 | + |
| 138 | + it("keeps matching a wildcard pattern against the relative path as written", async () => { |
| 139 | + // "*.log" has a wildcard, so it is not expanded to any depth: it excludes |
| 140 | + // the root's debug.log only, in both tools. |
| 141 | + const expected = ["packages/app/debug.log"].map((p) => |
| 142 | + path.join(...p.split("/")), |
| 143 | + ); |
| 144 | + expect(await search("**/*.log", ["*.log"])).toEqual(expected); |
| 145 | + expect( |
| 146 | + (await treeFiles(["*.log"])).filter((file) => file.endsWith(".log")), |
| 147 | + ).toEqual(expected); |
| 148 | + }); |
| 149 | +}); |
| 150 | + |
| 151 | +describe("isExcludedPath", () => { |
| 152 | + const p = (posix: string) => path.join(...posix.split("/")); |
| 153 | + |
| 154 | + it("matches a bare name at the root, at any depth, and below it", () => { |
| 155 | + expect(isExcludedPath("node_modules", ["node_modules"])).toBe(true); |
| 156 | + expect(isExcludedPath(p("a/b/node_modules"), ["node_modules"])).toBe(true); |
| 157 | + expect(isExcludedPath(p("a/node_modules/x/y.js"), ["node_modules"])).toBe( |
| 158 | + true, |
| 159 | + ); |
| 160 | + }); |
| 161 | + |
| 162 | + it("matches whole names, never a prefix", () => { |
| 163 | + expect(isExcludedPath("node_modules_old", ["node_modules"])).toBe(false); |
| 164 | + expect(isExcludedPath(p("a/node_modules_old/x.js"), ["node_modules"])).toBe( |
| 165 | + false, |
| 166 | + ); |
| 167 | + }); |
| 168 | + |
| 169 | + it("matches a wildcard pattern only as written", () => { |
| 170 | + expect(isExcludedPath("debug.log", ["*.log"])).toBe(true); |
| 171 | + expect(isExcludedPath(p("a/debug.log"), ["*.log"])).toBe(false); |
| 172 | + expect(isExcludedPath(p("a/debug.log"), ["**/*.log"])).toBe(true); |
| 173 | + }); |
| 174 | + |
| 175 | + it("excludes nothing for an empty list", () => { |
| 176 | + expect(isExcludedPath("anything", [])).toBe(false); |
| 177 | + }); |
| 178 | +}); |
0 commit comments