Skip to content

Commit 2953f9c

Browse files
authored
Merge pull request #2164 from boneskull/boneskull/issue2151
fix(fs): glob will traverse node_modules if asked to
2 parents 2d02bf6 + 0e2aa61 commit 2953f9c

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/lib/utils/fs.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,11 @@ export function glob(
149149
// cache of fs.realpathSync results to avoid extra I/O
150150
const realpathCache: Map<string, string> = new Map();
151151
const { includeDirectories = false, followSymlinks = false } = options;
152-
152+
// if we _specifically asked_ for something in node_modules, fine, otherwise ignore it
153+
// to avoid globs like '**/*.ts' finding all the .d.ts files in node_modules.
154+
// however, if the pattern is something like `!**/node_modules/**`, this will also
155+
// cause node_modules to be considered, though it will be discarded by minimatch.
156+
const shouldIncludeNodeModules = pattern.includes("node_modules");
153157
let dir = dirs.shift();
154158

155159
const handleFile = (path: string) => {
@@ -216,8 +220,10 @@ export function glob(
216220
})) {
217221
if (child.isFile()) {
218222
handleFile(child.name);
219-
} else if (child.isDirectory() && child.name !== "node_modules") {
220-
handleDirectory(child.name);
223+
} else if (child.isDirectory()) {
224+
if (shouldIncludeNodeModules || child.name !== "node_modules") {
225+
handleDirectory(child.name);
226+
}
221227
} else if (followSymlinks && child.isSymbolicLink()) {
222228
handleSymlink(child.name);
223229
}

src/test/utils/fs.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,32 @@ describe("fs.ts", () => {
9797
});
9898
});
9999

100+
describe("when node_modules is present in the pattern", function () {
101+
it("should traverse node_modules", function () {
102+
fix.dir("node_modules").addFile("test.ts").path;
103+
fix.write();
104+
equal(
105+
glob(`${fix.cwd}/node_modules/test.ts`, fix.cwd).map((f) =>
106+
basename(f)
107+
),
108+
["test.ts"]
109+
);
110+
});
111+
});
112+
113+
describe("when node_modules is not present in the pattern", function () {
114+
it("should not traverse node_modules", function () {
115+
fix.dir("node_modules").addFile("test.ts").path;
116+
fix.write();
117+
equal(
118+
glob(`${fix.cwd}/**/test.ts`, fix.cwd).map((f) =>
119+
basename(f)
120+
),
121+
[]
122+
);
123+
});
124+
});
125+
100126
it("should ignore anything that is not a file, symbolic link, or directory", function (done) {
101127
// Use unix socket for example, because that's easiest to create.
102128
// Skip on Windows because it doesn't support unix sockets

0 commit comments

Comments
 (0)