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

Not working with Yarn pnpm strategy #65757

Open
coreyward opened this issue May 14, 2024 · 12 comments
Open

Not working with Yarn pnpm strategy #65757

coreyward opened this issue May 14, 2024 · 12 comments
Labels
bug Issue was opened via the bug report template. Module Resolution Module resolution (CJS / ESM, module resolving). Runtime Related to Node.js or Edge Runtime with Next.js.

Comments

@coreyward
Copy link

coreyward commented May 14, 2024

Link to the code that reproduces this issue

https://github.com/coreyward/next-yarn-pnpm-issue

To Reproduce

Run yarn dev in the linked repo root, then open the app at localhost:3000. This will trigger the following error:

Error: Invariant: Expected relative import to start with "next/", found "package/dist/server/future/route-modules/pages/module.compiled"
at [Symbol.replace] ()
at String.replaceAll ()

Screenshot 2024-05-14 at 2 01 12 PM

Current vs. Expected behavior

Next works fine when using the legacy node_modules approach for installing dependencies, and seems to have support for the Plug-N-Play strategy of pnpm, but it does not seem to account for Yarn’s hybrid pnpm strategy which is described as follows:

node-modules will be created using symlinks and hardlinks to a global content-addressable store.

The issue seems to come about in the load-entrypoint, which seems to make unsafe assumptions about where dependencies are located on disk relative to the project. The specific error is coming from Next.js here:

// Verify that the relative import is relative to the `next` package. This
// will catch cases where the constants at the top of the file were not
// updated after the file was moved.
if (!relative.startsWith('next/')) {
throw new Error(
`Invariant: Expected relative import to start with "next/", found "${relative}"`
)
}

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.4.0
Binaries:
  Node: 20.11.0
  npm: 10.2.4
  Yarn: 4.2.2
  pnpm: N/A
Relevant Packages:
  next: 14.3.0-canary.62 // Latest available version is detected (14.3.0-canary.62).
  eslint-config-next: N/A
  react: 19.0.0-beta-4508873393-20240430
  react-dom: 19.0.0-beta-4508873393-20240430
  typescript: 5.1.3
Next.js Config:
  output: N/A

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

Module Resolution, Runtime

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

next dev (local), next build (local)

Additional context

This may be related to #16471.

@coreyward coreyward added the bug Issue was opened via the bug report template. label May 14, 2024
@github-actions github-actions bot added Module Resolution Module resolution (CJS / ESM, module resolving). Runtime Related to Node.js or Edge Runtime with Next.js. labels May 14, 2024
@Yukigamine
Copy link

I have been seeing this issue with Yarn 4.x–also using pnpm.

Downgrading to Yarn 3.x is how I fixed the issue in my projects.

@coreyward
Copy link
Author

@Yukigamine If you're able to use one of the other strategies it works (node-modules or pnp), but they both have other drawbacks (especially pnp).

@Yukigamine
Copy link

So the issue seems to be related specifically to the pnpm strategy using Yarn 4.x in Next.JS projects.

@nmilcoff
Copy link

+1, currently stuck at yarn 3 because of this, unfortunately node-modules and pnp strategies are not acceptable for my use case

@Setmaster
Copy link

Same issue. Yarn 4.3.1 with pmpm, tried wiping the files and redoing the install, but the problem persisted.

@vtgen
Copy link

vtgen commented Jul 28, 2024

I think something like this would work #68244

@vtgen
Copy link

vtgen commented Jul 28, 2024

issue filed with yarn here: yarnpkg/berry#6269

@vtgen vtgen mentioned this issue Jul 29, 2024
@zeptonaut
Copy link

zeptonaut commented Aug 2, 2024

Our team is also blocked by this: we currently use yarn in pnp mode, but are trying to migrate to yarn in pnpm mode as a step on the way to leverage turbopack.

My basic summary of @coreyward and @vtgen's research:

  • It looks like load-entrypoint.ts makes some checks here in order to verify that relative imports are always relative to the next package:

// Verify that the relative import is relative to the `next` package. This
// will catch cases where the constants at the top of the file were not
// updated after the file was moved.
if (!relative.startsWith('next/')) {
throw new Error(
`Invariant: Expected relative import to start with "next/", found "${relative}"`
)
}

  • ... but it looks like yarn in pnpm mode doesn't make any guarantees about the next package actually having that name, resulting in that check failing when yarn is running in pnpm mode. This gives an error like:
Error: Invariant: Expected relative import to start with "next/", found "package/dist/server/future/route-modules/pages/module.compiled"
at [Symbol.replace] ()
at String.replaceAll ()

It looks like the failing check was added by @wyattjoh in 03a09ba.

@wyattjoh, any chance you could offer some insight on what the right path forward is here? @coreyward offers a clear repro case in the original issue above and it'd be great to get this fixed, as it's blocking turbopack migration for yarn users using pnpm mode linking in yarn 4.x.

In terms of "who should fix this, Next.js or yarn?" -- unless there's solid documentation otherwise, it feels to me like Next.js is trying to rely on an implementation detail that isn't actually always the case, and that it's probably Next.js's bug to fix rather than yarn's.

@sebastienlabine

This comment has been minimized.

@UpstartMPotnick
Copy link

I've submitted a PR to yarn/berry to address the issue that @vtgen referenced. If y'all want to test it out while I wait for reviews/merge (I'm a first-time contributor, so I'm not sure how long the review cycle usually takes), you can use the yarn set version from sources CLI command, i.e.:

yarn set version from sources --repository https://github.com/UpstartMPotnick/berry.git  --branch mpotnick/more-specific-pnpm-paths

@yordis
Copy link
Contributor

yordis commented Oct 16, 2024

@UpstartMPotnick unless I did something wrong, your yarn version does not solve the --turbo issue:

❯ yarn dev                                                                                                                          
  ▲ Next.js 14.2.14 (turbo)
  - Local:        http://localhost:3000
  - Environments: .env.local
  - Experiments (use with caution):
    · instrumentationHook

 ✓ Starting...
[Error: Next.js package not found

Debug info:
- Execution of get_entrypoints_with_issues failed
- Execution of Project::entrypoints failed
- Execution of PagesProject::to_endpoint failed
- Execution of PagesStructureItem::new failed
- Execution of FileSystemPath::join failed
- Execution of get_next_package failed
- Next.js package not found] {
  code: 'GenericFailure'
}
❯ yarn set version from sources --repository https://github.com/UpstartMPotnick/berry.git --branch mpotnick/more-specific-pnpm-paths
➤ YN0000: Fetching the latest commits

  $ git fetch origin --depth=1 mpotnick/more-specific-pnpm-paths --force
  $ git reset --hard FETCH_HEAD
  $ git clean -dfx -e packages/yarnpkg-cli/bundles

➤ YN0000: Building a fresh bundle

➤ YN0000: Saving the new release in ../../.yarn/releases/yarn-4.5.0-git.20241011.hash-8c16144.cjs
➤ YN0000: Done in 1s 915ms

@vtgen
Copy link

vtgen commented Oct 30, 2024

@UpstartMPotnick thanks for that.

@yordis -- fwiw, at first it was not working for me, but after blowing out .yarn/ and starting fresh with @UpstartMPotnick 's build, it worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Module Resolution Module resolution (CJS / ESM, module resolving). Runtime Related to Node.js or Edge Runtime with Next.js.
Projects
None yet
Development

No branches or pull requests

9 participants