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

Barrel optimizer (optimizeImports) breaks RSC Components bundler: Could not find the module in the React Client Manifest #54967

Closed
1 task done
florianliebig opened this issue Sep 4, 2023 · 9 comments · Fixed by #56020
Assignees
Labels
bug Issue was opened via the bug report template. locked SWC Related to minification/transpilation in Next.js.

Comments

@florianliebig
Copy link

florianliebig commented Sep 4, 2023

Link to the code that reproduces this issue or a replay of the bug

https://github.com/florianliebig/next-error-optimize-package-imports

To Reproduce

Checkout example repo

pnpm i && pnpm build

Steps to create error:

  1. Use pnpm or import any external dep with server & client components in same wildcard export (in this case index.tsx exporting simple ClientComponent & ServerComponent
  2. Adjust next.config.json to use experimental optimizePackageImports from Improve barrel optimizer with loader caching and wilcard exports #54695
  3. Import both server & client component in one page from barrel file
  4. Build and / or start and see the error "Could not find the module ... in the React Client Manifest"

Current vs. Expected behavior

Error occurred prerendering page "/". Read more: https://nextjs.org/docs/messages/prerender-error
Error: Could not find the module "/Users/florianliebig/code/next-error-optimize-package-imports/packages/components/src/client/client-component.tsx#ClientComponent" in the React Client Manifest. This is probably a bug in the React Server Components bundler.

I would expect both Server & client components to be optimized properly from #54695

import { ClientComponent, ServerComponent } from '@shared/components';

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

    Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.6.0: Wed Jul  5 22:17:35 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T8112
    Binaries:
      Node: 18.16.0
      npm: 9.6.7
      Yarn: 1.22.19
      pnpm: 8.6.2
    Relevant Packages:
      next: 13.5.3-canary.0
      eslint-config-next: 13.4.19
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.2.2
    Next.js Config:
      output: N/A

Which area(s) are affected? (Select all that apply)

App Router, SWC transpilation

Additional context

No response

@florianliebig florianliebig added the bug Issue was opened via the bug report template. label Sep 4, 2023
@github-actions github-actions bot added the SWC Related to minification/transpilation in Next.js. label Sep 4, 2023
@florianliebig
Copy link
Author

Possibily similar issue as in #55029

@florianliebig florianliebig changed the title Barrel optimizer breaks RSC Components bundler: Could not find the module in the React Client Manifest Barrel optimizer (optimizeImports) breaks RSC Components bundler: Could not find the module in the React Client Manifest Sep 8, 2023
@florianliebig
Copy link
Author

@shuding is this something you already ran into when building the "optimizePackageImports" process? Or are mixed client- & server components not a usecase here?

I was also not able to workaround this by splitting the imports in a file by server- & client components unfortunately.

@florianliebig
Copy link
Author

florianliebig commented Sep 16, 2023

I was able to narrow the issue down to

2023-09-16 at 19 01
"mod.buildInfo.rsc" being undefined

Called from

if (isClientComponentEntryModule(mod)) {

@shuding
Copy link
Member

shuding commented Sep 21, 2023

I'll take a look at this today 🙏

@songhobby
Copy link

songhobby commented Sep 22, 2023

I'll take a look at this today 🙏

Hi @shuding are there any updates or temporary fixes for this issue?

@osehmathias
Copy link

👀

@bartlangelaan
Copy link
Contributor

bartlangelaan commented Sep 24, 2023

Dirty workaround is to hack Next.js so the optimizePackageImports option is fully disabled (if you are encountering this with an automatically-enabled module).

You can do this with the patch-package module. Patch:

patches/next+13.5.2.patch

diff --git a/node_modules/next/dist/server/config.js b/node_modules/next/dist/server/config.js
index efd81cf..10b1d4a 100644
--- a/node_modules/next/dist/server/config.js
+++ b/node_modules/next/dist/server/config.js
@@ -507,7 +507,7 @@ function assignDefaults(dir, userConfig, silent) {
     if (!result.experimental) {
         result.experimental = {};
     }
-    result.experimental.optimizePackageImports = [
+    result.experimental.optimizePackageImports = [] || [
         ...new Set([
             ...userProvidedOptimizePackageImports,
             "lucide-react",

After this, everything started working again. Related: #55566, #55609.

Note: of course this is an unsupported workaround that may also break at any time.

@shuding
Copy link
Member

shuding commented Sep 26, 2023

Apologize for the delay! I just found the root cause to this and will open a PR to fix it today. Meanwhile what @bartlangelaan mentioned can be a temporary workaround.

shuding added a commit that referenced this issue Sep 26, 2023
The barrel optimization loader creates a virtual module to re-export
from the original file, which causes the situation that now there are 2
modules with the same resource but only one of them is the actual code.

When the code contains `"use client"`, the Flight plugin has to collect
its `buildInfo` and generate the manifest and client entry module.
However, we currently deduplicate module by resource in the traversal
logic to avoid unnecessary loops. To make it work together with the
virtual barrel module, we'll need to prefix the resource path to make it
different from the original module.

Closes #54967.
Closes #55609.
Closes #55566.
@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. locked SWC Related to minification/transpilation in Next.js.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants