withastro / astro

The web framework for content-driven websites. ⭐️ Star to support our work!
https://astro.build
Other
45.6k stars 2.39k forks source link

experimental.contentIntellisense fails to generate JSON schemas with "Asynchronous transform" error #11721

Closed twodft closed 4 weeks ago

twodft commented 1 month ago

Astro Info

Astro                    v4.14.0
Node                     v20.5.0
System                   Windows (x64)
Package Manager          pnpm
Output                   hybrid
Adapter                  @astrojs/cloudflare
Integrations             @astrojs/mdx
                         @astrojs/tailwind
                         @astrojs/svelte
                         @astrojs/react
                         @astrojs/solid-js
                         @astrojs/sitemap
                         @astrojs/partytown
                         astro-icon

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

After enabling experimental.contentIntellisense in my Astro configuration (astro.config.mjs) and the corresponding setting in VS Code, I'm still encountering errors related to JSON schema creation for my content collections. The following warning messages are displayed in the terminal:

19:46:38 [WARN] [content] An error was encountered while creating the JSON schema for the "blogs" collection. Proceeding without it. Error: Error: Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.
19:46:38 [WARN] [content] An error was encountered while creating the JSON schema for the "events" collection. Proceeding without it. Error: Error: Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.
19:46:38 [WARN] [content] An error was encountered while creating the JSON schema for the "events" collection. Proceeding without it. Error: Error: Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.
19:46:38 [WARN] [content] An error was encountered while creating the JSON schema for the "help" collection. Proceeding without it. Error: Error: Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.
19:46:38 [WARN] [content] An error was encountered while creating the JSON schema for the "news" collection. Proceeding without it. Error: Error: Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.
19:46:38 [WARN] [content] An error was encountered while creating the JSON schema for the "products" collection. Proceeding without it. Error: Error: Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.
Despite these warnings, Astro seems to proceed without the JSON schemas, resulting in the content intellisense feature not working as expected.

What's the expected result?

Astro should be able to generate the JSON schemas for all my content collections without any errors.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-xzn8au?file=src%2Fcontent%2Fconfig.ts

Participation

github-actions[bot] commented 1 month ago

Hello @twodft. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with needs repro will be closed if they have no activity within 3 days.

plushugh commented 1 month ago

I was able to make a reproduction of the issue, for my case whenever I set a non-empty array of reference as default in the zod schema it will give such an error:

https://stackblitz.com/edit/github-xzn8au?file=src%2Fcontent%2Fconfig.ts

Changes from the blog example:

Enable in astro.config.mjs

experimental: {
  contentIntellisense: true,
},

Changed config.ts in content dir:

import { defineCollection, reference, z } from 'astro:content';

const blogCollection = defineCollection({
  type: 'content',
  schema: z.object({
    // relatedArticles: reference('blog'), // WORKS
    // relatedArticles: z.array(reference('blog')), // WORKS
    // relatedArticles: z.array(z.string()).default([]), // WORKS
    // relatedArticles: z.array(z.string()).default(['blog']), // WORKS
    // relatedArticles: z.array(reference('blog')).default([]), // WORKS
    relatedArticles: z.array(reference('blog')).default(['asdf']), // DOSEN'T WORK
  }),
});
export const collections = {
  blog: blogCollection,
};

Then regenerate the json schema by running astro sync or the dev server

~/projects/xoriwwrxjz.github 8s
❯ npm run astro sync

> @example/blog@0.0.1 astro
> astro sync

14:47:51 [WARN] [content] An error was encountered while creating the JSON schema for the "blog" collection. Proceeding without it. Error: Error: Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.