Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/whole-papers-stop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes the `glob()` loader not reloading changed files in dev when the pattern starts with an extglob such as `!(drafts)/**/*.md`
6 changes: 4 additions & 2 deletions packages/astro/src/content/loaders/glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,13 @@ export function glob(globOptions: GlobOptions & { [secretLegacyFlag]?: boolean }
// Split negation patterns out and pass them as picomatch's `ignore` option
// so watcher filtering matches tinyglobby's semantics (set subtraction),
// not picomatch's default (any-match union). See #17484.
// A leading `!(` is an extglob ("anything except"), not a negation (#18289).
const patterns = Array.isArray(globOptions.pattern)
? globOptions.pattern
: [globOptions.pattern];
const positivePatterns = patterns.filter((p) => !p.startsWith('!'));
const negationPatterns = patterns.filter((p) => p.startsWith('!')).map((p) => p.slice(1));
const isNegation = (p: string) => p.startsWith('!') && !p.startsWith('!(');
const positivePatterns = patterns.filter((p) => !isNegation(p));
const negationPatterns = patterns.filter(isNegation).map((p) => p.slice(1));
const matchesGlob = (entry: string) =>
!entry.startsWith('../') &&
picomatch.isMatch(entry, positivePatterns, {
Expand Down
56 changes: 56 additions & 0 deletions packages/astro/test/units/content-layer/glob-loader.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { strict as assert } from 'node:assert';
import { EventEmitter } from 'node:events';
import { mkdirSync, rmSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import { describe, it } from 'node:test';
Expand Down Expand Up @@ -754,3 +755,58 @@ describe('Glob Loader', () => {
}
});
});

describe('Glob Loader watcher', () => {
it('reloads changed files matched by a leading extglob pattern', async () => {
const tempDir = createTempDir();
const contentDir = join(fileURLToPath(tempDir), 'src', 'content');
mkdirSync(join(contentDir, 'blog'), { recursive: true });
mkdirSync(join(contentDir, 'hidden'), { recursive: true });
const postPath = join(contentDir, 'blog', 'post.md');
const hiddenPath = join(contentDir, 'hidden', 'secret.md');
writeFileSync(postPath, '---\ntitle: Post\n---\nOriginal');
writeFileSync(hiddenPath, '---\ntitle: Secret\n---\nHidden');

const store = new MutableDataStore();
const settings = createMinimalSettings(tempDir, {
contentEntryTypes: [createMarkdownEntryType()],
});
const logger = new AstroLogger({
destination: { write: () => true },
level: 'silent',
});
const watcher = Object.assign(new EventEmitter(), { add: () => {} });

const collections = {
blog: defineCollection({
loader: glob({ pattern: '!(hidden)/**/*.md', base: 'src/content' }),
}),
};

const contentLayer = new ContentLayer({
settings,
logger,
store,
contentConfigObserver: createTestConfigObserver(collections),
watcher: watcher as any,
});

await contentLayer.sync();
assert.deepEqual(
store.values('blog').map((e) => e.id),
['blog/post'],
);

writeFileSync(postPath, '---\ntitle: Post\n---\nUpdated');
watcher.emit('change', postPath);
watcher.emit('change', hiddenPath);
await new Promise((resolve) => setTimeout(resolve, 100));

const entries = store.values('blog');
assert.deepEqual(
entries.map((e) => e.id),
['blog/post'],
);
assert.equal(entries[0].body?.trim(), 'Updated');
});
});
Loading