Skip to content

Commit e4e0add

Browse files
MoLowRafaelGSS
authored andcommitted
fs: fix glob returning duplicates
PR-URL: #50881 Fixes: #50875 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent feb8ff9 commit e4e0add

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

lib/internal/fs/glob.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class Glob {
148148
#root;
149149
#exclude;
150150
#cache = new Cache();
151-
#results = [];
151+
#results = new SafeSet();
152152
#queue = [];
153153
#subpatterns = new SafeMap();
154154
constructor(patterns, options = kEmptyObject) {
@@ -193,7 +193,7 @@ class Glob {
193193
.forEach((patterns, path) => ArrayPrototypePush(this.#queue, { __proto__: null, path, patterns }));
194194
this.#subpatterns.clear();
195195
}
196-
return this.#results;
196+
return ArrayFrom(this.#results);
197197
}
198198
#addSubpattern(path, pattern) {
199199
if (!this.#subpatterns.has(path)) {
@@ -240,7 +240,7 @@ class Glob {
240240
const p = pattern.at(-1);
241241
const stat = this.#cache.statSync(join(fullpath, p));
242242
if (stat && (p || isDirectory)) {
243-
ArrayPrototypePush(this.#results, join(path, p));
243+
this.#results.add(join(path, p));
244244
}
245245
if (pattern.indexes.size === 1 && pattern.indexes.has(last)) {
246246
return;
@@ -249,7 +249,7 @@ class Glob {
249249
(path !== '.' || pattern.at(0) === '.' || (last === 0 && stat))) {
250250
// If pattern ends with **, add to results
251251
// if path is ".", add it only if pattern starts with "." or pattern is exactly "**"
252-
ArrayPrototypePush(this.#results, path);
252+
this.#results.add(path);
253253
}
254254

255255
if (!isDirectory) {
@@ -296,15 +296,15 @@ class Glob {
296296
subPatterns.add(index);
297297
} else if (!fromSymlink && index === last) {
298298
// If ** is last, add to results
299-
ArrayPrototypePush(this.#results, entryPath);
299+
this.#results.add(entryPath);
300300
}
301301

302302
// Any pattern after ** is also a potential pattern
303303
// so we can already test it here
304304
const nextMatches = pattern.test(nextIndex, entry.name);
305305
if (nextMatches && nextIndex === last && !isLast) {
306306
// If next pattern is the last one, add to results
307-
ArrayPrototypePush(this.#results, entryPath);
307+
this.#results.add(entryPath);
308308
} else if (nextMatches && entry.isDirectory()) {
309309
// Pattern mached, meaning two patterns forward
310310
// are also potential patterns
@@ -337,11 +337,11 @@ class Glob {
337337
} else {
338338
if (!this.#cache.seen(path, pattern, nextIndex)) {
339339
this.#cache.add(path, pattern.child(new SafeSet([nextIndex])));
340-
ArrayPrototypePush(this.#results, path);
340+
this.#results.add(path);
341341
}
342342
if (!this.#cache.seen(path, pattern, nextIndex) || !this.#cache.seen(parent, pattern, nextIndex)) {
343343
this.#cache.add(parent, pattern.child(new SafeSet([nextIndex])));
344-
ArrayPrototypePush(this.#results, parent);
344+
this.#results.add(parent);
345345
}
346346
}
347347
}
@@ -354,7 +354,7 @@ class Glob {
354354
} else if (current === '.' && pattern.test(nextIndex, entry.name)) {
355355
// If current pattern is ".", proceed to test next pattern
356356
if (nextIndex === last) {
357-
ArrayPrototypePush(this.#results, entryPath);
357+
this.#results.add(entryPath);
358358
} else {
359359
subPatterns.add(nextIndex + 1);
360360
}
@@ -364,7 +364,7 @@ class Glob {
364364
// If current pattern is a regex that matches entry name (e.g *.js)
365365
// add next pattern to potential patterns, or to results if it's the last pattern
366366
if (index === last) {
367-
ArrayPrototypePush(this.#results, entryPath);
367+
this.#results.add(entryPath);
368368
} else if (entry.isDirectory()) {
369369
subPatterns.add(nextIndex);
370370
}

test/parallel/test-fs-glob.mjs

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ const patterns = {
6565
],
6666
'a/{b,c,d,e,f}/**/g': [],
6767
'a/b/**': ['a/b', 'a/b/c', 'a/b/c/d'],
68+
'a/{b/**,b/c}': ['a/b', 'a/b/c', 'a/b/c/d'],
6869
'./**/g': ['a/abcdef/g', 'a/abcfed/g'],
6970
'a/abc{fed,def}/g/h': ['a/abcdef/g/h', 'a/abcfed/g/h'],
7071
'a/abc{fed/g,def}/**/': ['a/abcdef', 'a/abcdef/g', 'a/abcfed/g'],

0 commit comments

Comments
 (0)