Skip to content

Commit 9209bf1

Browse files
Archkonaduh95
authored andcommitted
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: #64571 Fixes: #64570 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Aviv Keller <me@aviv.sh> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent e970e67 commit 9209bf1

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

lib/internal/fs/glob.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -936,15 +936,16 @@ function matchGlobPattern(path, pattern, windows = isWindows) {
936936
validateString(path, 'path');
937937
validateString(pattern, 'pattern');
938938

939+
const cacheKey = `${windows ? 'win32' : 'posix'}:${pattern}`;
939940
let matcher;
940-
if (patternFnCache.has(pattern)) {
941-
matcher = patternFnCache.get(pattern);
941+
if (patternFnCache.has(cacheKey)) {
942+
matcher = patternFnCache.get(cacheKey);
942943
} else {
943944
matcher = createMatcher(pattern, {
944945
kEmptyObject,
945946
platform: windows ? 'win32' : 'posix',
946947
});
947-
patternFnCache.set(pattern, matcher);
948+
patternFnCache.set(cacheKey, matcher);
948949

949950
if (patternFnCache.size >= kGlobPatternCacheLimit) {
950951
patternFnCache.delete(patternFnCache.keys().next().value);

test/parallel/test-path-glob.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ const globs = {
3131
],
3232
};
3333

34+
// Matchers for the same pattern must not be shared across path platforms.
35+
assert.strictEqual(path.posix.matchesGlob('platform-cache/file', 'platform-cache/**'), true);
36+
assert.strictEqual(path.win32.matchesGlob('platform-cache\\file', 'platform-cache/**'), true);
3437

3538
for (const [platform, platformGlobs] of Object.entries(globs)) {
3639
for (const [pathStr, glob, expected] of platformGlobs) {

0 commit comments

Comments
 (0)