From 12f7fc055867c5f545bef89efbfb53510503f7e2 Mon Sep 17 00:00:00 2001 From: Archkon <180910180+Archkon@users.noreply.github.com> Date: Fri, 31 Jul 2026 00:14:23 +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> PR-URL: https://github.com/nodejs/node/pull/64571 Fixes: https://github.com/nodejs/node/issues/64570 Reviewed-By: James M Snell Reviewed-By: Aviv Keller Reviewed-By: Moshe Atlow --- 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 a0e7837db7c..c608016833f 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 47647e12784..11633f52245 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) {