From 748b2e87cd44d8bcc1ab9d7e504703057e2000cd Mon Sep 17 00:00:00 2001 From: Ben Holmes Date: Wed, 14 Feb 2024 05:09:31 -0500 Subject: [PATCH] Refine content collection warnings (#10001) * feat: remove nonexistent collection warning * fix: remove markdown syntax from console.warn * fix: respect configured collections in types when dir is missing * chore: changeset * chore: revert test schema * docs: "Removes" Co-authored-by: Chris Swithinbank --------- Co-authored-by: Chris Swithinbank --- .changeset/hip-hotels-divide.md | 7 +++++ packages/astro/src/content/runtime.ts | 4 ++- packages/astro/src/content/types-generator.ts | 31 ++----------------- 3 files changed, 13 insertions(+), 29 deletions(-) create mode 100644 .changeset/hip-hotels-divide.md diff --git a/.changeset/hip-hotels-divide.md b/.changeset/hip-hotels-divide.md new file mode 100644 index 000000000000..296aea5db215 --- /dev/null +++ b/.changeset/hip-hotels-divide.md @@ -0,0 +1,7 @@ +--- +"astro": minor +--- + +Removes content collection warning when a configured collection does not have a matching directory name. This should resolve `i18n` collection warnings for Starlight users. + +This also ensures configured collection names are always included in `getCollection()` and `getEntry()` types even when a matching directory is absent. We hope this allows users to discover typos during development by surfacing type information. diff --git a/packages/astro/src/content/runtime.ts b/packages/astro/src/content/runtime.ts index cccef41d8974..c2dd96349055 100644 --- a/packages/astro/src/content/runtime.ts +++ b/packages/astro/src/content/runtime.ts @@ -62,7 +62,9 @@ export function createGetCollection({ } else { // 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.` + `The collection ${JSON.stringify( + collection + )} does not exist or is empty. Ensure a collection directory with this name exists.` ); return; } diff --git a/packages/astro/src/content/types-generator.ts b/packages/astro/src/content/types-generator.ts index 62f2fffbd695..f8c3b56325ec 100644 --- a/packages/astro/src/content/types-generator.ts +++ b/packages/astro/src/content/types-generator.ts @@ -312,13 +312,6 @@ export async function createContentTypesGenerator({ viteServer, }); invalidateVirtualMod(viteServer); - if (observable.status === 'loaded') { - warnNonexistentCollections({ - logger, - contentConfig: observable.config, - collectionEntryMap, - }); - } } } return { init, queueEvent }; @@ -370,6 +363,9 @@ async function writeContentFiles({ }) { let contentTypesStr = ''; let dataTypesStr = ''; + for (const [collection, config] of Object.entries(contentConfig?.collections ?? {})) { + collectionEntryMap[JSON.stringify(collection)] ??= { type: config.type, entries: {} }; + } for (const collectionKey of Object.keys(collectionEntryMap).sort()) { const collectionConfig = contentConfig?.collections[JSON.parse(collectionKey)]; const collection = collectionEntryMap[collectionKey]; @@ -455,24 +451,3 @@ async function writeContentFiles({ typeTemplateContent ); } - -function warnNonexistentCollections({ - contentConfig, - collectionEntryMap, - logger, -}: { - contentConfig: ContentConfig; - collectionEntryMap: CollectionEntryMap; - logger: Logger; -}) { - for (const configuredCollection in contentConfig.collections) { - if (!collectionEntryMap[JSON.stringify(configuredCollection)]) { - logger.warn( - 'content', - `The ${bold(configuredCollection)} collection is defined but no ${bold( - 'content/' + configuredCollection - )} folder exists in the content directory. Create a new folder for the collection, or check your content configuration file for typos.` - ); - } - } -}