From c88cec82e725461873ad89f29d2badfcead094bc Mon Sep 17 00:00:00 2001 From: Archkon <180910180+Archkon@users.noreply.github.com> Date: Sat, 18 Jul 2026 15:04:06 +0800 Subject: [PATCH] fs: key glob matcher cache by platform Glob matchers capture the path platform when they are created. Include the platform in the cache key so path.posix and path.win32 do not share incompatible matchers for the same pattern. Signed-off-by: Archkon <180910180+Archkon@users.noreply.github.com> --- lib/internal/fs/glob.js | 7 ++++--- test/parallel/test-path-glob.js | 3 +++ 2 files changed, 7 insertions(+), 3 deletions(-) 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) {