diff --git a/.changeset/fix-glob-negation-pattern.md b/.changeset/fix-glob-negation-pattern.md new file mode 100644 index 000000000000..8ee4f98553fa --- /dev/null +++ b/.changeset/fix-glob-negation-pattern.md @@ -0,0 +1,5 @@ +--- +astro: patch +--- + +Fix glob-loader watcher to correctly handle negation patterns in file watching diff --git a/packages/astro/src/content/loaders/glob.ts b/packages/astro/src/content/loaders/glob.ts index 3c84c4a0f921..ac5b79e65c4b 100644 --- a/packages/astro/src/content/loaders/glob.ts +++ b/packages/astro/src/content/loaders/glob.ts @@ -335,8 +335,14 @@ export function glob(globOptions: GlobOptions & { [secretLegacyFlag]?: boolean } watcher.add(filePath); + // Use picomatch() to compile patterns into a matcher function. + // picomatch.isMatch() with array patterns containing negation entries (!pattern) + // incorrectly treats each array element independently, causing negation patterns + // to match everything. A compiled matcher correctly handles negation patterns. + // See https://github.com/withastro/astro/issues/16851 + const matchGlob = picomatch(globOptions.pattern); const matchesGlob = (entry: string) => - !entry.startsWith('../') && picomatch.isMatch(entry, globOptions.pattern); + !entry.startsWith('../') && matchGlob(entry); const basePath = fileURLToPath(baseDir);