Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn on empty content collections #8640

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

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

Warn on empty content collections
18 changes: 3 additions & 15 deletions packages/astro/src/content/runtime.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { MarkdownHeading } from '@astrojs/markdown-remark';
import { ZodIssueCode, string as zodString } from 'zod';
import type { AstroIntegration } from '../@types/astro.js';
import { AstroError, AstroErrorData } from '../core/errors/index.js';
import { prependForwardSlash } from '../core/path.js';
import {
Expand Down Expand Up @@ -56,7 +55,9 @@ export function createGetCollection({
} else if (collection in dataCollectionToEntryMap) {
type = 'data';
} else {
return warnOfEmptyCollection(collection);
// eslint-disable-next-line no-console
console.warn(`The collection **${collection}** does not exist or is empty. Ensure a collection directory with this name exists.`);
natemoo-re marked this conversation as resolved.
Show resolved Hide resolved
return;
}
const lazyImports = Object.values(
type === 'content'
Expand Down Expand Up @@ -390,16 +391,3 @@ type PropagatedAssetsModule = {
function isPropagatedAssetsModule(module: any): module is PropagatedAssetsModule {
return typeof module === 'object' && module != null && '__astroPropagation' in module;
}

function warnOfEmptyCollection(collection: string): AstroIntegration {
return {
name: 'astro-collection',
hooks: {
'astro:server:start': ({ logger }) => {
logger.warn(
`The collection **${collection}** does not exist or is empty. Ensure a collection directory with this name exists.`
);
},
},
};
}
1 change: 1 addition & 0 deletions packages/astro/src/core/errors/errors-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,7 @@ export const ContentSchemaContainsSlugError = {
/**
* @docs
* @message A collection queried via `getCollection()` does not exist.
* @deprecated Collections that do not exist are no longer an error. A warning is omitted instead.
matthewp marked this conversation as resolved.
Show resolved Hide resolved
* @description
* When querying a collection, ensure a collection directory with the requested name exists under `src/content/`.
*/
Expand Down