Skip to content

Commit 91d2d00

Browse files
committed
fixup! fs: apply exclude function to root path
1 parent 7a3b027 commit 91d2d00

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/internal/fs/glob.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ class Cache {
125125
}
126126
statSync(path) {
127127
const cached = this.#statsCache.get(path);
128-
if (cached) {
128+
// Do not return a promise from a sync function.
129+
if (cached && !(cached instanceof Promise)) {
129130
return cached;
130131
}
131132
const val = getDirentSync(path);
@@ -326,6 +327,16 @@ class Glob {
326327
if (this.#isExcluded(path)) {
327328
return;
328329
}
330+
const fullpath = resolve(this.#root, path);
331+
332+
// If path is a directory, add trailing slash and test patterns again.
333+
// TODO: Would running #isExcluded() first and checking isDirectory() only
334+
// if it matches be more performant in the typical use case? #isExcluded()
335+
// is often ()=>false which is about as optimizable as a function gets.
336+
if (this.#cache.statSync(fullpath).isDirectory() && this.#isExcluded(`${fullpath}/`)) {
337+
return;
338+
}
339+
329340
if (this.#exclude) {
330341
if (this.#withFileTypes) {
331342
const stat = this.#cache.statSync(path);

test/parallel/test-fs-glob.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,9 @@ const patterns2 = [
428428
[`${absDir}/foo`, 'a/c', ...(common.isWindows ? [] : ['a/symlink/a/b/c'])],
429429
],
430430
[ 'a/**', () => true, [] ],
431+
[ 'a/**', [ '*' ], [] ],
432+
[ 'a/**', [ '**' ], [] ],
433+
[ 'a/**', [ 'a/**' ], [] ],
431434
];
432435

433436
describe('globSync - exclude', function() {

0 commit comments

Comments
 (0)