From f9d35ae4af1e25275711c4ccad8bf99cee877dcd Mon Sep 17 00:00:00 2001 From: Gyubong Date: Wed, 2 Feb 2022 17:30:02 +0900 Subject: [PATCH] Fix duplicated result when using globstar (#231) --- index.js | 6 ++++-- tests/globby.js | 7 +++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index f7a8000..ce5a656 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ import fs from 'node:fs'; +import nodePath from 'node:path'; import merge2 from 'merge2'; import fastGlob from 'fast-glob'; import dirGlob from 'dir-glob'; @@ -84,8 +85,9 @@ const createFilterFunction = isIgnored => { return fastGlobResult => { const path = fastGlobResult.path || fastGlobResult; - const seenOrIgnored = seen.has(path) || (isIgnored && isIgnored(path)); - seen.add(path); + const pathKey = nodePath.normalize(path); + const seenOrIgnored = seen.has(pathKey) || (isIgnored && isIgnored(path)); + seen.add(pathKey); return !seenOrIgnored; }; }; diff --git a/tests/globby.js b/tests/globby.js index c141c7b..43aeeec 100644 --- a/tests/globby.js +++ b/tests/globby.js @@ -116,6 +116,13 @@ test('glob - stream async iterator support', async t => { t.deepEqual(results, ['a.tmp', 'b.tmp', 'c.tmp', 'd.tmp', 'e.tmp']); }); +test('glob - duplicated patterns', async t => { + const result1 = await runGlobby(t, [`./${temporary}/**`, `./${temporary}`]); + t.deepEqual(result1, ['./tmp/a.tmp', './tmp/b.tmp', './tmp/c.tmp', './tmp/d.tmp', './tmp/e.tmp']); + const result2 = await runGlobby(t, [`./${temporary}`, `./${temporary}/**`]); + t.deepEqual(result2, ['tmp/a.tmp', 'tmp/b.tmp', 'tmp/c.tmp', 'tmp/d.tmp', 'tmp/e.tmp']); +}); + test.serial('cwd option', async t => { process.chdir(temporary); t.deepEqual(await runGlobby(t, '*.tmp', {cwd}), ['a.tmp', 'b.tmp', 'c.tmp', 'd.tmp', 'e.tmp']);