diff --git a/lib/internal/fs/glob.js b/lib/internal/fs/glob.js index a0e7837db7c342..c608016833f99e 100644 --- a/lib/internal/fs/glob.js +++ b/lib/internal/fs/glob.js @@ -936,15 +936,16 @@ function matchGlobPattern(path, pattern, windows = isWindows) { validateString(path, 'path'); validateString(pattern, 'pattern'); + const cacheKey = `${windows ? 'win32' : 'posix'}:${pattern}`; let matcher; - if (patternFnCache.has(pattern)) { - matcher = patternFnCache.get(pattern); + if (patternFnCache.has(cacheKey)) { + matcher = patternFnCache.get(cacheKey); } else { matcher = createMatcher(pattern, { kEmptyObject, platform: windows ? 'win32' : 'posix', }); - patternFnCache.set(pattern, matcher); + patternFnCache.set(cacheKey, matcher); if (patternFnCache.size >= kGlobPatternCacheLimit) { patternFnCache.delete(patternFnCache.keys().next().value); diff --git a/test/parallel/test-path-glob.js b/test/parallel/test-path-glob.js index 47647e12784e5a..11633f52245ee9 100644 --- a/test/parallel/test-path-glob.js +++ b/test/parallel/test-path-glob.js @@ -31,6 +31,9 @@ const globs = { ], }; +// Matchers for the same pattern must not be shared across path platforms. +assert.strictEqual(path.posix.matchesGlob('platform-cache/file', 'platform-cache/**'), true); +assert.strictEqual(path.win32.matchesGlob('platform-cache\\file', 'platform-cache/**'), true); for (const [platform, platformGlobs] of Object.entries(globs)) { for (const [pathStr, glob, expected] of platformGlobs) {