From 29640641cae2595260d4bf1f01f5585f17c291f0 Mon Sep 17 00:00:00 2001 From: Timon Jurschitsch Date: Sun, 3 Sep 2023 09:11:18 +0200 Subject: [PATCH 1/4] Remove AstroError if collections directory is empty --- packages/astro/src/content/runtime.ts | 8 +++++--- packages/astro/test/content-collections.test.js | 16 ++++++++++++++++ .../content-collections-empty-dir/package.json | 9 +++++++++ .../src/content/config.ts | 11 +++++++++++ .../src/pages/index.astro | 8 ++++++++ pnpm-lock.yaml | 6 ++++++ 6 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 packages/astro/test/fixtures/content-collections-empty-dir/package.json create mode 100644 packages/astro/test/fixtures/content-collections-empty-dir/src/content/config.ts create mode 100644 packages/astro/test/fixtures/content-collections-empty-dir/src/pages/index.astro diff --git a/packages/astro/src/content/runtime.ts b/packages/astro/src/content/runtime.ts index 7b8654ee7037..e4f84a75d73e 100644 --- a/packages/astro/src/content/runtime.ts +++ b/packages/astro/src/content/runtime.ts @@ -55,9 +55,11 @@ export function createGetCollection({ } else if (collection in dataCollectionToEntryMap) { type = 'data'; } else { - throw new AstroError({ - ...AstroErrorData.CollectionDoesNotExistError, - message: AstroErrorData.CollectionDoesNotExistError.message(collection), + return zodString().transform((_, ctx) => { + ctx.addIssue({ + code: ZodIssueCode.custom, + message: `The collection **${collection}** does not exist. Ensure a collection directory with this name exists.`, + }); }); } const lazyImports = Object.values( diff --git a/packages/astro/test/content-collections.test.js b/packages/astro/test/content-collections.test.js index 10c65b0dd20f..bb097654241a 100644 --- a/packages/astro/test/content-collections.test.js +++ b/packages/astro/test/content-collections.test.js @@ -230,6 +230,22 @@ describe('Content Collections', () => { }); }); + describe('With empty collections directory', () => { + it('Handles the empty directory correclty', async () => { + const fixture = await loadFixture({ + root: './fixtures/content-collections-empty-dir/' + }); + let error; + try { + await fixture.build(); + } catch (e) { + error = e.message; + } + expect(error).to.be.undefined; + // TODO: try to render a page + }) + }) + describe('SSR integration', () => { let app; diff --git a/packages/astro/test/fixtures/content-collections-empty-dir/package.json b/packages/astro/test/fixtures/content-collections-empty-dir/package.json new file mode 100644 index 000000000000..a811df0717ff --- /dev/null +++ b/packages/astro/test/fixtures/content-collections-empty-dir/package.json @@ -0,0 +1,9 @@ +{ + "name": "@test/content-collections-empty-dir", + "version": "0.0.0", + "private": true, + "type": "module", + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/content-collections-empty-dir/src/content/config.ts b/packages/astro/test/fixtures/content-collections-empty-dir/src/content/config.ts new file mode 100644 index 000000000000..991994433870 --- /dev/null +++ b/packages/astro/test/fixtures/content-collections-empty-dir/src/content/config.ts @@ -0,0 +1,11 @@ +import { z, defineCollection } from 'astro:content'; + +const blog = defineCollection({ + schema: z.object({ + title: z.string(), + }), +}); + +export const collections = { + blog, +}; diff --git a/packages/astro/test/fixtures/content-collections-empty-dir/src/pages/index.astro b/packages/astro/test/fixtures/content-collections-empty-dir/src/pages/index.astro new file mode 100644 index 000000000000..4b5e0ffb8631 --- /dev/null +++ b/packages/astro/test/fixtures/content-collections-empty-dir/src/pages/index.astro @@ -0,0 +1,8 @@ +--- +import { getCollection } from 'astro:content' + +const blogs = await getCollection("blogs") +// console.log(blogs) +--- + +

This should still render

diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9988bdbac8be..ed4729e64578 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2360,6 +2360,12 @@ importers: specifier: workspace:* version: link:../../.. + packages/astro/test/fixtures/content-collections-empty-dir: + dependencies: + astro: + specifier: workspace:* + version: link:../../.. + packages/astro/test/fixtures/content-collections-empty-md-file: dependencies: astro: From 13b3017e08c8f58de8d916ff0c2985a3d5750596 Mon Sep 17 00:00:00 2001 From: Timon Jurschitsch Date: Sun, 10 Sep 2023 13:11:56 +0200 Subject: [PATCH 2/4] Add changeset --- .changeset/hungry-dancers-hug.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/hungry-dancers-hug.md diff --git a/.changeset/hungry-dancers-hug.md b/.changeset/hungry-dancers-hug.md new file mode 100644 index 000000000000..f118deaf4b76 --- /dev/null +++ b/.changeset/hungry-dancers-hug.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +remove AstroError for empty collection directory From 3b7411afab73ac43ed6e105624de6dd22aa7af4b Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Tue, 12 Sep 2023 14:55:19 +0100 Subject: [PATCH 3/4] Update .changeset/hungry-dancers-hug.md --- .changeset/hungry-dancers-hug.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/hungry-dancers-hug.md b/.changeset/hungry-dancers-hug.md index f118deaf4b76..43b994ef0c4b 100644 --- a/.changeset/hungry-dancers-hug.md +++ b/.changeset/hungry-dancers-hug.md @@ -2,4 +2,4 @@ 'astro': patch --- -remove AstroError for empty collection directory +Do not throw an error for an empty collection directory. From e26a89f3f46dab0534cd90c148efbdc096025be4 Mon Sep 17 00:00:00 2001 From: Timon Jurschitsch Date: Thu, 14 Sep 2023 17:37:33 +0200 Subject: [PATCH 4/4] Use logger instead of zod error --- packages/astro/src/content/runtime.ts | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/packages/astro/src/content/runtime.ts b/packages/astro/src/content/runtime.ts index e4f84a75d73e..3309bc642aea 100644 --- a/packages/astro/src/content/runtime.ts +++ b/packages/astro/src/content/runtime.ts @@ -13,6 +13,7 @@ import { type AstroComponentFactory, } from '../runtime/server/index.js'; import type { ContentLookupMap } from './utils.js'; +import type { AstroIntegration } from '../@types/astro.js'; type LazyImport = () => Promise; type GlobResult = Record; @@ -55,12 +56,7 @@ export function createGetCollection({ } else if (collection in dataCollectionToEntryMap) { type = 'data'; } else { - return zodString().transform((_, ctx) => { - ctx.addIssue({ - code: ZodIssueCode.custom, - message: `The collection **${collection}** does not exist. Ensure a collection directory with this name exists.`, - }); - }); + return warnOfEmptyCollection(collection) } const lazyImports = Object.values( type === 'content' @@ -394,3 +390,16 @@ 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.` + ); + }, + }, + }; +}