withastro/astro

`astro build` fails with pnpm (but works with npm)

eikowagenknecht opened this issue · 4 comments

Astro Info

Astro                    v5.0.3
Node                     v20.11.1
System                   Windows (x64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             @astrojs/tailwind
                         @astrojs/mdx
                         @astrojs/react
                         @astrojs/sitemap

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

No response

Describe the Bug

After upgrading to Astro 5, i noticed the following:

npm run astro build works:

PS C:\***\***\***.de> npm i

> ***.de@1.0.0 postinstall
> lefthook install

sync hooks: ✔️ (commit-msg, pre-commit)

added 667 packages, and audited 668 packages in 38s

236 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
PS C:\***\***\***.de> npm run astro build

> ***.de@1.0.0 astro
> astro build

16:12:33 [content] Syncing content
16:12:33 [content] Content config changed
16:12:33 [content] Astro version changed
16:12:33 [content] Clearing content store
[Shiki] The language "hex" doesn't exist, falling back to "plaintext".
16:12:33 [content] Synced content
16:12:33 [types] Generated 1.45s
16:12:33 [build] output: "static"
16:12:33 [build] directory: C:\***\***\***.de\dist\
16:12:33 [build] Collecting build info...
16:12:33 [build] ✓ Completed in 1.49s.
16:12:33 [build] Building static entrypoints...
16:12:36 [vite] ✓ built in 2.35s
16:12:36 [build] ✓ Completed in 2.37s.

 building client (vite) 
16:12:36 [vite] ✓ 83 modules transformed.
16:12:36 [vite] dist/_astro/client.DNpcs3m7.js            1.82 kB │ gzip:  0.91 kB
16:12:36 [vite] dist/_astro/ImageWithModal.C6dIqcxn.js   33.08 kB │ gzip: 11.58 kB
16:12:36 [vite] dist/_astro/index.EISe-aAG.js           141.72 kB │ gzip: 45.60 kB
16:12:36 [vite] ✓ built in 354ms

 generating static routes 
16:12:36 ▶ src/pages/404.astro
16:12:36   └─ /404.html (+10ms)
16:12:36 ▶ src/pages/blog.astro
16:12:36   └─ /blog/index.html (+7ms)
16:12:36 ▶ src/pages/consulting.astro
16:12:36   └─ /consulting/index.html (+2ms)
16:12:36 ▶ src/pages/datenschutz.astro
16:12:36   └─ /datenschutz/index.html (+2ms)
16:12:36 ▶ src/pages/impressum.astro
16:12:36   └─ /impressum/index.html (+2ms)
16:12:36 ▶ src/pages/posts/[...slug].astro
...
16:12:36   └─ /posts/markdown-test/index.html (+1ms)
16:12:36 ▶ src/pages/privacy-policy.astro
16:12:36   └─ /privacy-policy/index.html (+2ms)
16:12:36 ▶ src/pages/projects.astro
16:12:36   └─ /projects/index.html (+2ms)
16:12:36 λ src/pages/robots.txt.ts
16:12:36   └─ /robots.txt (+1ms)
16:12:36 λ src/pages/rss.xml.js
16:12:36   └─ /rss.xml (+9ms)
16:12:36 ▶ src/pages/simple-game-clock.astro
16:12:36   └─ /simple-game-clock/index.html (+1ms)
16:12:36 ▶ src/pages/tags/[tag].astro
16:12:36   ├─ /tags/blog/index.html (+2ms)
16:12:36   ├─ /tags/tech/index.html (+1ms)
16:12:36   ├─ /tags/home-automation/index.html (+1ms)
16:12:36   ├─ /tags/home-assistant/index.html (+1ms)
16:12:36   ├─ /tags/esp32/index.html (+1ms)
16:12:36   ├─ /tags/web-development/index.html (+1ms)
16:12:36   ├─ /tags/astro/index.html (+1ms)
16:12:36   ├─ /tags/obsidian/index.html (+1ms)
16:12:36   ├─ /tags/raspberry-pi/index.html (+1ms)
16:12:36   └─ /tags/games/index.html (+1ms)
16:12:36 ▶ src/pages/tags/index.astro
16:12:36   └─ /tags/index.html (+2ms)
16:12:36 ▶ src/pages/index.astro
16:12:36   └─ /index.html (+2ms)
16:12:36 ▶ /atom.xml
16:12:36   └─ /atom.xml/index.html (+2ms)
16:12:36 ▶ /cv
16:12:36   └─ /cv/index.html (+1ms)
16:12:36 ▶ /feed
16:12:36   └─ /feed/index.html (+1ms)
16:12:36 ✓ Completed in 324ms.

16:12:36 [@astrojs/sitemap] `sitemap-index.xml` created at `dist`
16:12:36 [build] 36 page(s) built in 4.58s
16:12:36 [build] Complete!
PS C:\***\***\***.de>

pnpm run astro build fails:

PS C:\***\***\***.de> pnpm i
Lockfile is up to date, resolution step is skipped
Packages: +642
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Progress: resolved 642, reused 642, downloaded 0, added 642, done
node_modules/.pnpm/sharp@0.33.5/node_modules/sharp: Running install script, done in 76ms
node_modules/.pnpm/lefthook@1.9.0/node_modules/lefthook: Running postinstall script, done in 271ms

dependencies:
+ @astrojs/mdx 4.0.1
+ @astrojs/react 4.0.0
+ @astrojs/rss 4.0.10
+ @astrojs/sitemap 3.2.1
+ @astrojs/tailwind 5.1.3
+ @r4ai/remark-callout 0.6.2
+ @radix-ui/react-dialog 1.1.2
+ @types/mdast 4.0.4
+ astro 5.0.3
+ clsx 2.1.1
+ hast-util-from-html-isomorphic 2.0.0
+ react 18.3.1
+ react-dom 18.3.1
+ rehype-autolink-headings 7.1.0
+ rehype-slug 6.0.0
+ remark-github-blockquote-alert 1.3.0
+ remark-toc 9.0.0
+ sanitize-html 2.13.1
+ tailwind-merge 2.5.5
+ unified 11.0.5
+ unist-util-visit 5.0.0

devDependencies:
+ @astrojs/check 0.9.4
+ @biomejs/biome 1.9.4
+ @commitlint/cli 19.6.0
+ @commitlint/config-conventional 19.6.0
+ @commitlint/types 19.5.0
+ @tailwindcss/forms 0.5.9
+ @tailwindcss/typography 0.5.15
+ @types/react 18.3.13
+ @types/react-dom 18.3.1
+ lefthook 1.9.0
+ prettier 3.4.2
+ prettier-plugin-astro 0.14.1
+ prettier-plugin-tailwindcss 0.6.9
+ tailwindcss 3.4.16
+ typescript 5.7.2

> ***.de@1.0.0 postinstall C:\***\***\***.de
> lefthook install

sync hooks: ✔️ (pre-commit, commit-msg)

Done in 4.9s
PS C:\***\***\***.de> pnpm run astro build

> ***.de@1.0.0 astro C:\***\***\***.de
> astro "build"

16:13:23 [content] Syncing content
16:13:23 [content] Content config changed
16:13:23 [content] Astro version changed
16:13:23 [content] Clearing content store
[Shiki] The language "hex" doesn't exist, falling back to "plaintext".
16:13:24 [content] Synced content
16:13:24 [types] Generated 1.13s
16:13:24 [build] output: "static"
16:13:24 [build] directory: C:\***\***\***.de\dist\
16:13:24 [build] Collecting build info...
16:13:24 [build] ✓ Completed in 1.18s.
16:13:24 [build] Building static entrypoints...
16:13:29 [ERROR] [vite] x Build failed in 4.50s
[vite]: Rollup failed to resolve import "fsevents" from "C:/***/***/***.de/node_modules/.pnpm/rollup@4.28.0/node_modules/rollup/dist/es/shared/node-entry.js".
This is most likely unintended because it can break your application at runtime.
If you do want to externalize this module explicitly add it to
`build.rollupOptions.external`
  Stack trace:
    at viteWarn (file:///C:/***/***/***.de/node_modules/.pnpm/vite@6.0.3_@types+node@22.10.1_jiti@1.21.6_yaml@2.6.1/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:50728:17)
    at onRollupWarning (file:///C:/***/***/***.de/node_modules/.pnpm/vite@6.0.3_@types+node@22.10.1_jiti@1.21.6_yaml@2.6.1/node_modules/vite/dist/node/chunks/dep-yUJfKD1i.js:50758:5)
    at file:///C:/***/***/***.de/node_modules/.pnpm/rollup@4.28.0/node_modules/rollup/dist/es/shared/node-entry.js:19475:13
    at ModuleLoader.handleInvalidResolvedId (file:///C:/***/***/***.de/node_modules/.pnpm/rollup@4.28.0/node_modules/rollup/dist/es/shared/node-entry.js:20090:26)  
    at async file:///C:/***/***/***.de/node_modules/.pnpm/rollup@4.28.0/node_modules/rollup/dist/es/shared/node-entry.js:20036:32
 ELIFECYCLE  Command failed with exit code 1.
PS C:\***\***\***.de> 

The culprit seems to be the rss config. As soon as I remove the file, it works with pnpm as well:

import { getCollection } from "astro:content";
import { SITE } from "@/config";
import mdxRenderer from "@astrojs/mdx/server.js";
import reactRenderer from "@astrojs/react/server.js";
import rss from "@astrojs/rss";
import { experimental_AstroContainer as AstroContainer } from "astro/container";
import sanitizeHtml from "sanitize-html";

export async function GET(context) {
  const container = await AstroContainer.create();
  container.addServerRenderer({ renderer: mdxRenderer });
  container.addServerRenderer({ renderer: reactRenderer });
  const publishedBlogEntries = await getCollection("posts", ({ data }) => {
    return Boolean(data.published);
  });

  const sortedPosts = Object.values(publishedBlogEntries).sort((a, b) => {
    if (!a.data.published || !b.data.published) {
      return -1;
    }
    return (
      new Date(b.data.published).valueOf() -
      new Date(a.data.published).valueOf()
    );
  });

  return rss({
    title: SITE.title,
    description: SITE.description,
    site: context.site,
    items: await Promise.all(
      sortedPosts.map(async (post) => {
        const rssEntry = {
          title: post.data.title,
          description: post.data.description,
          pubDate: post.data.published,
          link: `/posts/${post.slug}/`,
        };

        // For now, only add the content when it can be rendered.
        // Should be fixed soon, see https://github.com/withastro/astro/issues/11407
        try {
          const { Content } = await post.render();
          const postHtml = await container.renderToString(Content);
          const sanitizedHtml = sanitizeHtml(postHtml, {
            allowedTags: sanitizeHtml.defaults.allowedTags.concat(["img"]),
          });

          rssEntry.content = sanitizedHtml;
        } catch (error) {
          console.log(`Can't render feed content for ${post.slug}, skipping.`);
        }

        return rssEntry;
      }),
    ),
    customData: "<language>en-us</language>",
    stylesheet: "/assets/styles/rss.xsl",
  });
}

Removing the import { experimental_AstroContainer as AstroContainer } from "astro/container"; fixes the hangup.

Reminds me of #11128, but since that one has been fixed apparently, it might be something else as well.

What's the expected result?

Compilation working with pnpm as well.

Link to Minimal Reproducible Example

No link because Stackblitz doesn't allow using pnpm as far as I can tell.

Participation

  • I am willing to submit a pull request for this issue.

I'm also encountering this issue since Astro 5 with the same use. In dev I have no errors, that's why I didn't noticed yet. I tried to build after seeing this issue and I get the same error.

I tried to make a reproduction as minimal as possible: https://github.com/ArmandPhilippot/astro5-container-pnpm-build-error

  • I defined a posts collection with a Markdown post
  • I added the site property in astro.config.mjs
  • I created src/pages/feed.xml.ts to create a RSS feed using astro/container

When you use pnpm build you should see the same error.

If I add rollupOptions in astro.config.mjs like this:

vite: {
    build: {
      rollupOptions: {
        external: "fsevents",
      },
    },
},

I get the following error:

__dirname is not defined in ES module scope
  Location:
    /home/armand/Dev/tmp/astro-build-pnpm-error/node_modules/.pnpm/astro@5.0.3_rollup@4.28.1_typescript@5.7.2/node_modules/astro/dist/core/build/pipeline.js:221:15
  Stack trace:
    at requireNative (file:///home/armand/Dev/tmp/astro-build-pnpm-error/dist/chunks/feed_sEdlxitk.mjs:23031:24)
    at ModuleJob.run (node:internal/modules/esm/module_job:234:25)
    at async BuildPipeline.retrieveSsrEntry (file:///home/armand/Dev/tmp/astro-build-pnpm-error/node_modules/.pnpm/astro@5.0.3_rollup@4.28.1_typescript@5.7.2/node_modules/astro/dist/core/build/pipeline.js:221:15)
    at async staticBuild (file:///home/armand/Dev/tmp/astro-build-pnpm-error/node_modules/.pnpm/astro@5.0.3_rollup@4.28.1_typescript@5.7.2/node_modules/astro/dist/core/build/static-build.js:89:5)
    at async AstroBuilder.run (file:///home/armand/Dev/tmp/astro-build-pnpm-error/node_modules/.pnpm/astro@5.0.3_rollup@4.28.1_typescript@5.7.2/node_modules/astro/dist/core/build/index.js:183:7)

So I guess it's more complicated to find a workaround on the user side.

I know it's not the primary use for astro/container so maybe it's not really a "bug". However, it was working with v4 so if Astro wants to support this usage I hope the reproduction can help to debug...

Oh and I'm not on Windows so:

Astro                    v5.0.3
Node                     v20.17.0
System                   Linux (x64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             none

I've found a workaround...
I had to install fsevents menually.

Never mind... It failed on runtime
Though it happened for me as well...

Astro                    v5.0.3
Node                     v22.12.0
System                   Linux (x64)
Package Manager          pnpm
Output                   server
Adapter                  node
Integrations             none

+1