Skip to content

Commit

Permalink
fix: better logs for invalid content config (#12798)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Swithinbank <[email protected]>
  • Loading branch information
ascorbic and delucis authored Dec 20, 2024
1 parent 5f4c543 commit 7b0cb85
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-sloths-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Improves warning logs for invalid content collection configuration
9 changes: 8 additions & 1 deletion packages/astro/src/content/content-layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,15 @@ export class ContentLayer {
async #doSync(options: RefreshContentOptions) {
const contentConfig = globalContentConfigObserver.get();
const logger = this.#logger.forkIntegrationLogger('content');

if (contentConfig?.status === 'error') {
logger.error(`Error loading content config. Skipping sync.\n${contentConfig.error.message}`);
return;
}

// It shows as loaded with no collections even if there's no config
if (contentConfig?.status !== 'loaded') {
logger.debug('Content config not loaded, skipping sync');
logger.error('Content config not loaded, skipping sync');
return;
}

Expand Down
20 changes: 19 additions & 1 deletion packages/astro/src/content/loaders/glob.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { promises as fs } from 'node:fs';
import { promises as fs, existsSync } from 'node:fs';
import { relative } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import fastGlob from 'fast-glob';
import { bold, green } from 'kleur/colors';
Expand Down Expand Up @@ -215,10 +216,27 @@ export function glob(globOptions: GlobOptions): Loader {
baseDir.pathname = `${baseDir.pathname}/`;
}

const filePath = fileURLToPath(baseDir);
const relativePath = relative(fileURLToPath(config.root), filePath);

const exists = existsSync(baseDir);

if (!exists) {
// We warn and don't return because we will still set up the watcher in case the directory is created later
logger.warn(`The base directory "${fileURLToPath(baseDir)}" does not exist.`);
}

const files = await fastGlob(globOptions.pattern, {
cwd: fileURLToPath(baseDir),
});

if (exists && files.length === 0) {
logger.warn(
`No files found matching "${globOptions.pattern}" in directory "${relativePath}"`,
);
return;
}

function configForFile(file: string) {
const ext = file.split('.').at(-1);
if (!ext) {
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/content/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export function createGetCollection({
console.warn(
`The collection ${JSON.stringify(
collection,
)} does not exist or is empty. Ensure a collection directory with this name exists.`,
)} does not exist or is empty. Please check your content config file for errors.`,
);
return [];
}
Expand Down
27 changes: 26 additions & 1 deletion packages/astro/test/content-layer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import assert from 'node:assert/strict';
import { promises as fs, existsSync } from 'node:fs';
import { sep } from 'node:path';
import { sep as posixSep } from 'node:path/posix';
import { Writable } from 'node:stream';
import { after, before, describe, it } from 'node:test';
import * as cheerio from 'cheerio';
import * as devalue from 'devalue';
import { Logger } from '../dist/core/logger/core.js';

import { loadFixture } from './test-utils.js';
describe('Content Layer', () => {
Expand Down Expand Up @@ -316,8 +318,21 @@ describe('Content Layer', () => {
describe('Dev', () => {
let devServer;
let json;
const logs = [];
before(async () => {
devServer = await fixture.startDevServer({ force: true });
devServer = await fixture.startDevServer({
force: true,
logger: new Logger({
level: 'warn',
dest: new Writable({
objectMode: true,
write(event, _, callback) {
logs.push(event);
callback();
},
}),
}),
});
// Vite may not have noticed the saved data store yet. Wait a little just in case.
await fixture.onNextDataStoreChange(1000).catch(() => {
// Ignore timeout, because it may have saved before we get here.
Expand All @@ -331,6 +346,16 @@ describe('Content Layer', () => {
devServer?.stop();
});

it("warns about missing directory in glob() loader's path", async () => {
assert.ok(logs.find((log) => log.level === 'warn' && log.message.includes('does not exist')));
});

it("warns about missing files in glob() loader's path", async () => {
assert.ok(
logs.find((log) => log.level === 'warn' && log.message.includes('No files found matching')),
);
});

it('Generates content types files', async () => {
assert.ok(existsSync(new URL('./.astro/content.d.ts', fixture.config.root)));
const data = await fs.readFile(new URL('./.astro/types.d.ts', fixture.config.root), 'utf-8');
Expand Down
10 changes: 10 additions & 0 deletions packages/astro/test/fixtures/content-layer/src/content.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,13 @@ const numbers = defineCollection({
loader: glob({ pattern: 'src/data/glob-data/*', base: '.' }),
});

const notADirectory = defineCollection({
loader: glob({ pattern: '*', base: 'src/nonexistent' }),
});

const nothingMatches = defineCollection({
loader: glob({ pattern: 'nothingmatches/*', base: 'src/data' }),
});
const images = defineCollection({
loader: () => [
{
Expand Down Expand Up @@ -259,4 +266,7 @@ export const collections = {
songs,
probes,
rodents,
notADirectory,
nothingMatches
};

0 comments on commit 7b0cb85

Please sign in to comment.