Skip to content

Commit

Permalink
Merge pull request #2164 from boneskull/boneskull/issue2151
Browse files Browse the repository at this point in the history
fix(fs): glob will traverse node_modules if asked to
  • Loading branch information
Gerrit0 committed Feb 10, 2023
2 parents 2d02bf6 + 0e2aa61 commit 2953f9c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/lib/utils/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ export function glob(
// cache of fs.realpathSync results to avoid extra I/O
const realpathCache: Map<string, string> = new Map();
const { includeDirectories = false, followSymlinks = false } = options;

// if we _specifically asked_ for something in node_modules, fine, otherwise ignore it
// to avoid globs like '**/*.ts' finding all the .d.ts files in node_modules.
// however, if the pattern is something like `!**/node_modules/**`, this will also
// cause node_modules to be considered, though it will be discarded by minimatch.
const shouldIncludeNodeModules = pattern.includes("node_modules");
let dir = dirs.shift();

const handleFile = (path: string) => {
Expand Down Expand Up @@ -216,8 +220,10 @@ export function glob(
})) {
if (child.isFile()) {
handleFile(child.name);
} else if (child.isDirectory() && child.name !== "node_modules") {
handleDirectory(child.name);
} else if (child.isDirectory()) {
if (shouldIncludeNodeModules || child.name !== "node_modules") {
handleDirectory(child.name);
}
} else if (followSymlinks && child.isSymbolicLink()) {
handleSymlink(child.name);
}
Expand Down
26 changes: 26 additions & 0 deletions src/test/utils/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,32 @@ describe("fs.ts", () => {
});
});

describe("when node_modules is present in the pattern", function () {
it("should traverse node_modules", function () {
fix.dir("node_modules").addFile("test.ts").path;
fix.write();
equal(
glob(`${fix.cwd}/node_modules/test.ts`, fix.cwd).map((f) =>
basename(f)
),
["test.ts"]
);
});
});

describe("when node_modules is not present in the pattern", function () {
it("should not traverse node_modules", function () {
fix.dir("node_modules").addFile("test.ts").path;
fix.write();
equal(
glob(`${fix.cwd}/**/test.ts`, fix.cwd).map((f) =>
basename(f)
),
[]
);
});
});

it("should ignore anything that is not a file, symbolic link, or directory", function (done) {
// Use unix socket for example, because that's easiest to create.
// Skip on Windows because it doesn't support unix sockets
Expand Down

0 comments on commit 2953f9c

Please sign in to comment.