From 87cb4ed3a7b485f5c5f390cae95ff49732ac8828 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Fri, 22 Sep 2023 12:30:18 -0400 Subject: [PATCH 1/2] Warn on empty content collections --- .changeset/spotty-jokes-arrive.md | 5 +++++ packages/astro/src/content/runtime.ts | 18 +++--------------- packages/astro/src/core/errors/errors-data.ts | 1 + 3 files changed, 9 insertions(+), 15 deletions(-) create mode 100644 .changeset/spotty-jokes-arrive.md diff --git a/.changeset/spotty-jokes-arrive.md b/.changeset/spotty-jokes-arrive.md new file mode 100644 index 000000000000..8638f8b8b654 --- /dev/null +++ b/.changeset/spotty-jokes-arrive.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Warn on empty content collections diff --git a/packages/astro/src/content/runtime.ts b/packages/astro/src/content/runtime.ts index eeaa60e6c984..2c5a120ca965 100644 --- a/packages/astro/src/content/runtime.ts +++ b/packages/astro/src/content/runtime.ts @@ -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 { @@ -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.`); + return; } const lazyImports = Object.values( type === 'content' @@ -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.` - ); - }, - }, - }; -} diff --git a/packages/astro/src/core/errors/errors-data.ts b/packages/astro/src/core/errors/errors-data.ts index e4fe3554046b..1d67bbfa0eaa 100644 --- a/packages/astro/src/core/errors/errors-data.ts +++ b/packages/astro/src/core/errors/errors-data.ts @@ -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. * @description * When querying a collection, ensure a collection directory with the requested name exists under `src/content/`. */ From 46273496d76f2b06aeb6ec33673a8c5d369643ee Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Fri, 22 Sep 2023 13:53:07 -0400 Subject: [PATCH 2/2] Update packages/astro/src/core/errors/errors-data.ts Co-authored-by: Reuben Tier <64310361+TheOtterlord@users.noreply.github.com> --- packages/astro/src/core/errors/errors-data.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/astro/src/core/errors/errors-data.ts b/packages/astro/src/core/errors/errors-data.ts index 1d67bbfa0eaa..ace362cefc7a 100644 --- a/packages/astro/src/core/errors/errors-data.ts +++ b/packages/astro/src/core/errors/errors-data.ts @@ -1158,7 +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. + * @deprecated Collections that do not exist no longer result in an error. A warning is omitted instead. * @description * When querying a collection, ensure a collection directory with the requested name exists under `src/content/`. */