Skip to content

Commit

Permalink
Refine content collection warnings (#10001)
Browse files Browse the repository at this point in the history
* 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 <swithinbank@gmail.com>

---------

Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
  • Loading branch information
bholmesdev and delucis authored Feb 14, 2024
1 parent 5759fd9 commit 748b2e8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 29 deletions.
7 changes: 7 additions & 0 deletions .changeset/hip-hotels-divide.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 3 additions & 1 deletion packages/astro/src/content/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
31 changes: 3 additions & 28 deletions packages/astro/src/content/types-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,6 @@ export async function createContentTypesGenerator({
viteServer,
});
invalidateVirtualMod(viteServer);
if (observable.status === 'loaded') {
warnNonexistentCollections({
logger,
contentConfig: observable.config,
collectionEntryMap,
});
}
}
}
return { init, queueEvent };
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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.`
);
}
}
}

0 comments on commit 748b2e8

Please sign in to comment.