Skip to content

Commit

Permalink
fs: fix glob returning duplicates
Browse files Browse the repository at this point in the history
PR-URL: #50881
Fixes: #50875
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
  • Loading branch information
MoLow authored Nov 26, 2023
1 parent ed5cb37 commit f28839b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/internal/fs/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class Glob {
#root;
#exclude;
#cache = new Cache();
#results = [];
#results = new SafeSet();
#queue = [];
#subpatterns = new SafeMap();
constructor(patterns, options = kEmptyObject) {
Expand Down Expand Up @@ -193,7 +193,7 @@ class Glob {
.forEach((patterns, path) => ArrayPrototypePush(this.#queue, { __proto__: null, path, patterns }));
this.#subpatterns.clear();
}
return this.#results;
return ArrayFrom(this.#results);
}
#addSubpattern(path, pattern) {
if (!this.#subpatterns.has(path)) {
Expand Down Expand Up @@ -240,7 +240,7 @@ class Glob {
const p = pattern.at(-1);
const stat = this.#cache.statSync(join(fullpath, p));
if (stat && (p || isDirectory)) {
ArrayPrototypePush(this.#results, join(path, p));
this.#results.add(join(path, p));
}
if (pattern.indexes.size === 1 && pattern.indexes.has(last)) {
return;
Expand All @@ -249,7 +249,7 @@ class Glob {
(path !== '.' || pattern.at(0) === '.' || (last === 0 && stat))) {
// If pattern ends with **, add to results
// if path is ".", add it only if pattern starts with "." or pattern is exactly "**"
ArrayPrototypePush(this.#results, path);
this.#results.add(path);
}

if (!isDirectory) {
Expand Down Expand Up @@ -296,15 +296,15 @@ class Glob {
subPatterns.add(index);
} else if (!fromSymlink && index === last) {
// If ** is last, add to results
ArrayPrototypePush(this.#results, entryPath);
this.#results.add(entryPath);
}

// Any pattern after ** is also a potential pattern
// so we can already test it here
const nextMatches = pattern.test(nextIndex, entry.name);
if (nextMatches && nextIndex === last && !isLast) {
// If next pattern is the last one, add to results
ArrayPrototypePush(this.#results, entryPath);
this.#results.add(entryPath);
} else if (nextMatches && entry.isDirectory()) {
// Pattern mached, meaning two patterns forward
// are also potential patterns
Expand Down Expand Up @@ -337,11 +337,11 @@ class Glob {
} else {
if (!this.#cache.seen(path, pattern, nextIndex)) {
this.#cache.add(path, pattern.child(new SafeSet([nextIndex])));
ArrayPrototypePush(this.#results, path);
this.#results.add(path);
}
if (!this.#cache.seen(path, pattern, nextIndex) || !this.#cache.seen(parent, pattern, nextIndex)) {
this.#cache.add(parent, pattern.child(new SafeSet([nextIndex])));
ArrayPrototypePush(this.#results, parent);
this.#results.add(parent);
}
}
}
Expand All @@ -354,7 +354,7 @@ class Glob {
} else if (current === '.' && pattern.test(nextIndex, entry.name)) {
// If current pattern is ".", proceed to test next pattern
if (nextIndex === last) {
ArrayPrototypePush(this.#results, entryPath);
this.#results.add(entryPath);
} else {
subPatterns.add(nextIndex + 1);
}
Expand All @@ -364,7 +364,7 @@ class Glob {
// If current pattern is a regex that matches entry name (e.g *.js)
// add next pattern to potential patterns, or to results if it's the last pattern
if (index === last) {
ArrayPrototypePush(this.#results, entryPath);
this.#results.add(entryPath);
} else if (entry.isDirectory()) {
subPatterns.add(nextIndex);
}
Expand Down
1 change: 1 addition & 0 deletions test/parallel/test-fs-glob.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const patterns = {
],
'a/{b,c,d,e,f}/**/g': [],
'a/b/**': ['a/b', 'a/b/c', 'a/b/c/d'],
'a/{b/**,b/c}': ['a/b', 'a/b/c', 'a/b/c/d'],
'./**/g': ['a/abcdef/g', 'a/abcfed/g'],
'a/abc{fed,def}/g/h': ['a/abcdef/g/h', 'a/abcfed/g/h'],
'a/abc{fed/g,def}/**/': ['a/abcdef', 'a/abcdef/g', 'a/abcfed/g'],
Expand Down

0 comments on commit f28839b

Please sign in to comment.