Skip to content

Commit 11f0615

Browse files
committed
fs: apply exclude function to root path
In at least some situations, the root path was being added to the results of globbing even if it matched the optional exclude function. In the relevant test file, a variable was renamed for consistency. (There was a patterns and pattern2, rather than patterns2.) The test case is added to patterns2. Fixes: #56260
1 parent c3ed292 commit 11f0615

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

lib/internal/fs/glob.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,18 @@ class Glob {
326326
if (this.#isExcluded(path)) {
327327
return;
328328
}
329+
if (this.#exclude) {
330+
if (this.#withFileTypes) {
331+
const stat = this.#cache.statSync(path);
332+
if (stat !== null) {
333+
if (this.#exclude(stat)) {
334+
return;
335+
}
336+
}
337+
} else if (this.#exclude(path)) {
338+
return;
339+
}
340+
}
329341
if (!this.#subpatterns.has(path)) {
330342
this.#subpatterns.set(path, [pattern]);
331343
} else {

test/parallel/test-fs-glob.mjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ describe('fsPromises glob - withFileTypes', function() {
388388
});
389389

390390
// [pattern, exclude option, expected result]
391-
const pattern2 = [
391+
const patterns2 = [
392392
['a/{b,c}*', ['a/*c'], ['a/b', 'a/cb']],
393393
['a/{a,b,c}*', ['a/*bc*', 'a/cb'], ['a/b', 'a/c']],
394394
['a/**/[cg]', ['**/c'], ['a/abcdef/g', 'a/abcfed/g']],
@@ -427,6 +427,7 @@ const pattern2 = [
427427
[`${absDir}/*{a,q}*`, './a/*{c,b}*/*'],
428428
[`${absDir}/foo`, 'a/c', ...(common.isWindows ? [] : ['a/symlink/a/b/c'])],
429429
],
430+
[ 'a/**', () => true, [] ],
430431
];
431432

432433
describe('globSync - exclude', function() {
@@ -436,7 +437,7 @@ describe('globSync - exclude', function() {
436437
assert.strictEqual(actual.length, 0);
437438
});
438439
}
439-
for (const [pattern, exclude, expected] of pattern2) {
440+
for (const [pattern, exclude, expected] of patterns2) {
440441
test(`${pattern} - exclude: ${exclude}`, () => {
441442
const actual = globSync(pattern, { cwd: fixtureDir, exclude }).sort();
442443
const normalized = expected.filter(Boolean).map((item) => item.replaceAll('/', sep)).sort();
@@ -453,7 +454,7 @@ describe('glob - exclude', function() {
453454
assert.strictEqual(actual.length, 0);
454455
});
455456
}
456-
for (const [pattern, exclude, expected] of pattern2) {
457+
for (const [pattern, exclude, expected] of patterns2) {
457458
test(`${pattern} - exclude: ${exclude}`, async () => {
458459
const actual = (await promisified(pattern, { cwd: fixtureDir, exclude })).sort();
459460
const normalized = expected.filter(Boolean).map((item) => item.replaceAll('/', sep)).sort();
@@ -471,7 +472,7 @@ describe('fsPromises glob - exclude', function() {
471472
assert.strictEqual(actual.length, 0);
472473
});
473474
}
474-
for (const [pattern, exclude, expected] of pattern2) {
475+
for (const [pattern, exclude, expected] of patterns2) {
475476
test(`${pattern} - exclude: ${exclude}`, async () => {
476477
const actual = [];
477478
for await (const item of asyncGlob(pattern, { cwd: fixtureDir, exclude })) actual.push(item);

0 commit comments

Comments
 (0)