Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content collections build: "Vite module runner has been closed." #12689

Open
1 task done
crummy opened this issue Dec 9, 2024 · 15 comments
Open
1 task done

Content collections build: "Vite module runner has been closed." #12689

crummy opened this issue Dec 9, 2024 · 15 comments
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)

Comments

@crummy
Copy link

crummy commented Dec 9, 2024

Astro Info

Astro                    v5.0.3
Node                     v23.2.0
System                   macOS (arm64)
Package Manager          npm
Output                   static
Adapter                  none
Integrations             none

Describe the Bug

I read files to generate a content collection. Works fine during development, but fails during build with this output:

❯ astro build
17:09:47 [content] Syncing content
17:09:47 [content] Content config changed
17:09:47 [content] Clearing content store
Vite module runner has been closed.
  Location:
    /home/projects/withastro-astro-7t9i3ppv/node_modules/vite/dist/node/module-runner.js:1217:13
  Stack trace:
    at SSRCompatModuleRunner.getModuleInformation (file:///home/projects/withastro-astro-7t9i3ppv/node_modules/vite/dist/node/module-runner.js:1217:13)
    at request (file:///home/projects/withastro-astro-7t9i3ppv/node_modules/vite/dist/node/module-runner.js:1243:99)
    at ./examples/csv/grammar.txt (/home/projects/withastro-astro-7t9i3ppv/src/content.config.ts:7:89)
    at eval (file:///home/projects/withastro-astro-7t9i3ppv/node_modules/zod/lib/index.mjs:3492:40)
    at eval (file:///home/projects/withastro-astro-7t9i3ppv/node_modules/astro/dist/content/content-layer.js:188:18)

What's the expected result?

Should behave identically to dev.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/withastro-astro-7t9i3ppv?file=src%2Fcontent.config.ts

In the console, run astro build.

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Dec 9, 2024
@Princesseuh
Copy link
Member

Ran into this issue updating an old website of mine, if an additional reproduction is needed: Princesseuh/astro-issues#1

Note that I do not use content collections

@Princesseuh Princesseuh added - P3: minor bug An edge case that only affects very specific usage (priority) and removed needs triage Issue needs to be triaged labels Dec 11, 2024
@crummy
Copy link
Author

crummy commented Dec 11, 2024

It happens to me any time I import a file in a collection. Here's the smallest repro I've come up with:

export const examples = defineCollection({
  loader: async () => {
    const file = await import('/src/examples/csv/grammar.txt');
    return [];
  },
});

Commenting out const file... means build will succeed.

@crummy
Copy link
Author

crummy commented Dec 12, 2024

Ran into this issue updating an old website of mine, if an additional reproduction is needed: Princesseuh/astro-issues#1

On your repo I replaced this line:

const allStats = import.meta.glob("/src/data/*.json");

With this line:

const allStats = {}

And the error disappeared (it was replaced with a later error but presumably that's because this object is not expected to be empty).

I wondered if dynamic imports were broken entirely but I put this at the top of an astro file and it worked fine:

const f = await import("/src/examples/csv/grammar.txt?raw")

crummy added a commit to crummy/ppegjs-demo that referenced this issue Dec 12, 2024
Simplifies content collection parsing, and there's this:
withastro/astro#12689
@Adammatthiesen
Copy link

can also confirm that this issue is happening with the following vite dynamic import functions from an integration hook (astro:config:setup) as well... we (StudioCMS team) had to disable our config file loading system (Load a users studiocms.config.{mjs|js|ts|mts|cjs|cts} in order to upgrade to v5). hopefully there will be a fix soon!

const mod = await import(configPath)

@bluwy
Copy link
Member

bluwy commented Dec 16, 2024

Maybe same as vitejs/vite#18962

@crummy
Copy link
Author

crummy commented Dec 19, 2024

I have a workaround, reading files with fs.readFile. Here is my loader:

    loader: async () => {
        const files = await fastGlob("src/examples/*.json");
        return await Promise.all(files.map(async f => {
            const contents = await fs.readFile(f, "utf-8")
            const json = JSON.parse(contents);
            return {
                id: json.title,
                title: json.title,
                grammar: json.grammar,
                input: json.input,
                highlighted: json.highlighted
            }
        }));
    },

@bluwy

This comment was marked as outdated.

@bluwy bluwy closed this as completed Jan 2, 2025
@bluwy

This comment was marked as outdated.

@mhagendoorn-9292
Copy link

This issue is still present in [email protected].

@smac89
Copy link

smac89 commented Jan 24, 2025

The issue occurs in a custom astro plugin, when a dynamic import occurs. I get the following inside the astro:build:setup hook:

Vite module runner has been closed.
Location:
/home/chigozirim/Development/website/node_modules/.pnpm/vite@6.0.11_@types[email protected][email protected][email protected][email protected][email protected]/node_modules/vite/dist/node/module-runner.js:1188:13
Stack trace:
at SSRCompatModuleRunner.getModuleInformation (file:///home/chigozirim/Development/website/node_modules/.pnpm/vite@6.0.11_@types[email protected][email protected][email protected][email protected][email protected]/node_modules/vite/dist/node/module-runner.js:1188:13)
at request (file:///home/chigozirim/Development/website/node_modules/.pnpm/vite@6.0.11_@types[email protected][email protected][email protected][email protected][email protected]/node_modules/vite/dist/node/module-runner.js:1214:99)
at astro:build:setup (/home/chigozirim/Development/website/plugins/astro-css-renamer.ts:30:36)
at ssrBuild (file:///home/chigozirim/Development/website/node_modules/.pnpm/astro@5.1.9_@types[email protected][email protected][email protected][email protected][email protected][email protected][email protected]/node_modules/astro/dist/core/build/static-build.js:174:40)
at async AstroBuilder.build (file:///home/chigozirim/Development/website/node_modules/.pnpm/astro@5.1.9_@types[email protected][email protected][email protected][email protected][email protected][email protected][email protected]/node_modules/astro/dist/core/build/index.js:144:48)
 ELIFECYCLE  Command failed with exit code 1

The line that triggers the error is:

const cssRenamer = await import("postcss-rename").then((p) => p.default);

➜  website git:(main) ✗ pnpm astro info
Astro                    v5.1.9
Node                     v20.15.1
System                   Linux (x64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             @astrojs/sitemap
                         @astrojs/tailwind
                         astro-tailwind-config-viewer
                         css-renamer
                         purge-files
                         astro-compress

Downgrading to v4 solves the issue. Framework maintainers should pay attention to https://vite.dev/guide/api-environment-frameworks.html.

Thanks

@Adammatthiesen
Copy link

Coming back with an update for the integration's standpoint... but i was able to get it working properly for resolving a user's config (studiocms.config.mjs) file by implementing a build step to convert my .ts files to .js. After some of us were discussing in the Discord the v5 change that effects this issue is this

So this seems to be related to .ts files in certain steps that are not in the normal Astro runtime

@KiritoKing
Copy link

I ran into this issue in my website, too.
I started a new blog framework with latest AstroWind template with [email protected].

When I was using notion-astro-loader to build my content layer collection with notion, the same error happened.

I looked up the loader's repo and find the related code. It just uses the getImage API.

#fetchImage = async (imageFileObject) => {
        try {
            const fetchedImageData = await fileToImageAsset(imageFileObject);
            this.#imagePaths.push(fetchedImageData.src);
            return fetchedImageData.src;
        }
        catch (error) {
            console.error(error)
            this.#logger.error(`Failed to fetch image when rendering page.
Have you added \`image: { remotePatterns: [{ protocol: "https", hostname: "*.amazonaws.com" }] }\` to your Astro config file?\n
Error: ${getErrorMessage(error)}`);
            // Fall back to using the remote URL directly.
            return fileToUrl(imageFileObject);
        }
    };

/**
 * Extract and locally cache the image from a file object.
 * @see https://developers.notion.com/reference/file-object
 */
export async function fileToImageAsset(
  file: FileObject,
): Promise<GetImageResult> {
  return getImage({
    src: fileToUrl(file),
    inferSize: true,
  });
}

export function fileToUrl(file: FileObject): string;
export function fileToUrl(file: FileObject | null): string | undefined;
export function fileToUrl(file: FileObject | null): string | undefined {
  switch (file?.type) {
    case "external":
      return file.external.url;
    case "file":
      return file.file.url;
    default:
      return undefined;
  }
}

Here is the stack trace. It seems that it is caused by dynamic import, which is mentioned at a closed Vite issue.

20:36:43 [ERROR] [page 02f9d03e-aa37-4ad3-b889-d6b4c20bfcb7 (Name 前端开发要知道的网络基础)] Failed to fetch image when rendering page.
Have you added `image: { remotePatterns: [{ protocol: "https", hostname: "*.amazonaws.com" }] }` to your Astro config file?

Error: There was an error loading the configured image service. Please see the stack trace for more information.
InvalidImageService: There was an error loading the configured image service. Please see the stack trace for more information.
    at eval (/Users/bytedance/Code/personal/notion-astro-rev/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected][email protected][email protected][email protected]/node_modules/astro/dist/assets/internal.js:25:21)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async getConfiguredImageService (/Users/bytedance/Code/personal/notion-astro-rev/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected][email protected][email protected][email protected]/node_modules/astro/dist/assets/internal.js:21:34)
    at async getImage (/Users/bytedance/Code/personal/notion-astro-rev/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected][email protected][email protected][email protected]/node_modules/astro/dist/assets/internal.js:55:19)
    at async getImage (astro:assets:32:42)
    at async #fetchImage (/Users/bytedance/Code/personal/notion-astro-rev/node_modules/.pnpm/[email protected][email protected]_@[email protected][email protected][email protected]_roll_2ibjhaoyvb5densitsoeosfrn4/node_modules/notion-astro-loader/dist/render.js:187:38)
    at async listBlocks (/Users/bytedance/Code/personal/notion-astro-rev/node_modules/.pnpm/[email protected][email protected]_@[email protected][email protected][email protected]_roll_2ibjhaoyvb5densitsoeosfrn4/node_modules/notion-astro-loader/dist/render.js:79:25)
    at async awaitAll (/Users/bytedance/Code/personal/notion-astro-rev/node_modules/.pnpm/[email protected][email protected]_@[email protected][email protected][email protected]_roll_2ibjhaoyvb5densitsoeosfrn4/node_modules/notion-astro-loader/dist/render.js:54:22)
    at async NotionPageRenderer.render (/Users/bytedance/Code/personal/notion-astro-rev/node_modules/.pnpm/[email protected][email protected]_@[email protected][email protected][email protected]_roll_2ibjhaoyvb5densitsoeosfrn4/node_modules/notion-astro-loader/dist/render.js:163:28)
    at async Promise.all (index 19) {
  loc: undefined,
  title: 'Error while loading image service.',
  hint: undefined,
  frame: undefined,
  type: 'AstroError',
  cause: Error: Vite module runner has been closed.
      at SSRCompatModuleRunner.getModuleInformation (file:///Users/bytedance/Code/personal/notion-astro-rev/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected][email protected]/node_modules/vite/dist/node/module-runner.js:1196:13)
      at SSRCompatModuleRunner.cachedModule (file:///Users/bytedance/Code/personal/notion-astro-rev/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected][email protected]/node_modules/vite/dist/node/module-runner.js:1186:21)
      at request (file:///Users/bytedance/Code/personal/notion-astro-rev/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected][email protected]/node_modules/vite/dist/node/module-runner.js:1222:99)
      at dynamicRequest (file:///Users/bytedance/Code/personal/notion-astro-rev/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected][email protected]/node_modules/vite/dist/node/module-runner.js:1224:124)
      at getConfiguredImageService (/Users/bytedance/Code/personal/notion-astro-rev/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected][email protected][email protected][email protected]/node_modules/astro/dist/assets/internal.js:21:40)
      at getImage (/Users/bytedance/Code/personal/notion-astro-rev/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected][email protected][email protected][email protected]/node_modules/astro/dist/assets/internal.js:55:25)
      at getImage (astro:assets:32:82)
      at fileToImageAsset (/Users/bytedance/Code/personal/notion-astro-rev/node_modules/.pnpm/[email protected][email protected]_@[email protected][email protected][email protected]_roll_2ibjhaoyvb5densitsoeosfrn4/node_modules/notion-astro-loader/dist/format.js:33:46)
      at #fetchImage (/Users/bytedance/Code/personal/notion-astro-rev/node_modules/.pnpm/[email protected][email protected]_@[email protected][email protected][email protected]_roll_2ibjhaoyvb5densitsoeosfrn4/node_modules/notion-astro-loader/dist/render.js:187:86)
      at listBlocks (/Users/bytedance/Code/personal/notion-astro-rev/node_modules/.pnpm/[email protected][email protected]_@[email protected][email protected][email protected]_roll_2ibjhaoyvb5densitsoeosfrn4/node_modules/notion-astro-loader/dist/render.js:79:31)
}

@KiritoKing
Copy link

@vitejs/vite#18962

@KiritoKing
Copy link

Hey dude, I've found out the root cause of this issue.

It seems that astro doesn't actually start a Vite server when handling content layer loader's syncing process. So, any calls for astro APIs are illegal since no Vite server can handle those 'virtual requests'.

Checkout these codes:

The code lines above indicate that Vite server is only started for content collection initialization, and when it's time for layers to run their load() functions, the server is disposed, so we can see "module runner is closed" in the error message.

I've found out a stupid way to workaround this sh*t, but it causes memory leak. Just remove the dispose!

Hopefully, I can find out the final way to solve this during my vacation haha.

@KiritoKing
Copy link

KiritoKing commented Jan 27, 2025

Unbelievably! It really works!!!

I am completely new to Astro. I do NOT know how to make lifecycles for the vite servers more reasonable.

So, I'm looking for help fro Astro experts or someone from core team.

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

No branches or pull requests

7 participants