Skip to content

fs: apply exclude function to root path #57420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion lib/internal/fs/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
ArrayPrototypePop,
ArrayPrototypePush,
ArrayPrototypeSome,
Promise,
PromisePrototypeThen,
SafeMap,
SafeSet,
Expand Down Expand Up @@ -125,7 +126,8 @@ class Cache {
}
statSync(path) {
const cached = this.#statsCache.get(path);
if (cached) {
// Do not return a promise from a sync function.
if (cached && !(cached instanceof Promise)) {
return cached;
}
const val = getDirentSync(path);
Expand Down Expand Up @@ -326,6 +328,28 @@ class Glob {
if (this.#isExcluded(path)) {
return;
}
const fullpath = resolve(this.#root, path);

// If path is a directory, add trailing slash and test patterns again.
// TODO(Trott): Would running #isExcluded() first and checking isDirectory() only
// if it matches be more performant in the typical use case? #isExcluded()
// is often ()=>false which is about as optimizable as a function gets.
if (this.#cache.statSync(fullpath).isDirectory() && this.#isExcluded(`${fullpath}/`)) {
return;
}

if (this.#exclude) {
if (this.#withFileTypes) {
const stat = this.#cache.statSync(path);
if (stat !== null) {
if (this.#exclude(stat)) {
return;
}
}
} else if (this.#exclude(path)) {
return;
}
}
if (!this.#subpatterns.has(path)) {
this.#subpatterns.set(path, [pattern]);
} else {
Expand Down
12 changes: 8 additions & 4 deletions test/parallel/test-fs-glob.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ describe('fsPromises glob - withFileTypes', function() {
});

// [pattern, exclude option, expected result]
const pattern2 = [
const patterns2 = [
['a/{b,c}*', ['a/*c'], ['a/b', 'a/cb']],
['a/{a,b,c}*', ['a/*bc*', 'a/cb'], ['a/b', 'a/c']],
['a/**/[cg]', ['**/c'], ['a/abcdef/g', 'a/abcfed/g']],
Expand Down Expand Up @@ -427,6 +427,10 @@ const pattern2 = [
[`${absDir}/*{a,q}*`, './a/*{c,b}*/*'],
[`${absDir}/foo`, 'a/c', ...(common.isWindows ? [] : ['a/symlink/a/b/c'])],
],
[ 'a/**', () => true, [] ],
[ 'a/**', [ '*' ], [] ],
[ 'a/**', [ '**' ], [] ],
[ 'a/**', [ 'a/**' ], [] ],
];

describe('globSync - exclude', function() {
Expand All @@ -436,7 +440,7 @@ describe('globSync - exclude', function() {
assert.strictEqual(actual.length, 0);
});
}
for (const [pattern, exclude, expected] of pattern2) {
for (const [pattern, exclude, expected] of patterns2) {
test(`${pattern} - exclude: ${exclude}`, () => {
const actual = globSync(pattern, { cwd: fixtureDir, exclude }).sort();
const normalized = expected.filter(Boolean).map((item) => item.replaceAll('/', sep)).sort();
Expand All @@ -453,7 +457,7 @@ describe('glob - exclude', function() {
assert.strictEqual(actual.length, 0);
});
}
for (const [pattern, exclude, expected] of pattern2) {
for (const [pattern, exclude, expected] of patterns2) {
test(`${pattern} - exclude: ${exclude}`, async () => {
const actual = (await promisified(pattern, { cwd: fixtureDir, exclude })).sort();
const normalized = expected.filter(Boolean).map((item) => item.replaceAll('/', sep)).sort();
Expand All @@ -471,7 +475,7 @@ describe('fsPromises glob - exclude', function() {
assert.strictEqual(actual.length, 0);
});
}
for (const [pattern, exclude, expected] of pattern2) {
for (const [pattern, exclude, expected] of patterns2) {
test(`${pattern} - exclude: ${exclude}`, async () => {
const actual = [];
for await (const item of asyncGlob(pattern, { cwd: fixtureDir, exclude })) actual.push(item);
Expand Down
Loading