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

Use require.resolve instead of the resolve package #12439

Merged
merged 3 commits into from Dec 4, 2020

Conversation

nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Dec 2, 2020

Q                       A
Fixed Issues? Closes #12396, fixes vercel/next.js#19334
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes? Removed resolve
License MIT

This PR fixes an incompatibility between Babel, PnP and Next.js.

The problem is that Next.js bundles Babel, and thus it bundles our resolve dependency. resolve doesn't support PnP, but Yarn patches resolve at install time so that it works with PnP: this doesn't work if resolve is inside of a bundle, because Yarn cannot detect it.
(context: #12396)

This PR drops "resolve", in favor or require.resolve (when supported) or a custom small polyfill when it's not.
The other possible fix is that resolve supports PnP natively (rather than being patched at build-time), but it won't likely be implemented because it would unnecessarly ship a lot of code to consumers not using PnP (ref: browserify/resolve#199).

By doing so,

Note that customizing our build process to support older Node.js versions is something we already did: for example, before #12288 we were using a custom plugin to fix resolution of dynamic import paths in some Node.js versions.

@nicolo-ribaudo nicolo-ribaudo added PR: Internal 🏠 A type of pull request used for our changelog categories pkg: core labels Dec 2, 2020
@babel-bot
Copy link
Collaborator

babel-bot commented Dec 2, 2020

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/33892/

@codesandbox-ci
Copy link

codesandbox-ci bot commented Dec 2, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit d5b2aee:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@JLHwung JLHwung self-requested a review December 2, 2020 16:48
@babel babel deleted a comment Dec 3, 2020
: (/* request */ r, { paths: [/* base */ b] }, M = require("module")) => {
let /* filename */ f = M._findPath(r, M._nodeModulePaths(b).concat(b));
if (f) return f;
f = new Error(\`Cannot resolve module '\${r}'\`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
f = new Error(\`Cannot resolve module '\${r}'\`);
f = new Error(\`Cannot find module '\${r}'\`);

To be consistent with the native require.resolve error message.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I put resolve here otherwise all the tests where failing. I think Jest's require.resolve uses "resolve" in its error message.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a blocker.

@SimenB Is it possible to test MODULE_NOT_FOUND error without changing the error message? We want to make assertion about the MODULE_NOT_FOUND error but Jest seems to assume such error means the test can not be run and all tests are failing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

error from jest should match node, if it doesn't it's a bug

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JLHwung The jest version we are using on Node 6 didn't always set MODULE_NOT_FOUND anyway, so we can't test it right now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really follow here, but if Jest 26 does something wrong with MODULE_NOT_FOUND it would be great if you opened up an issue 🙂

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No don't worry! We are using Jest 24 on Node.js 6 and Jest 26 where it's supported. The bug was only with Jest 24, so we cannot rely on the fix but it is fixed in Jest 26!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, good stuff 👍

Copy link
Contributor

@JLHwung JLHwung left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look good to me with a nit.

@nicolo-ribaudo nicolo-ribaudo merged commit 208acb1 into babel:main Dec 4, 2020
@nicolo-ribaudo nicolo-ribaudo deleted the require-resolve-node-6 branch December 4, 2020 07:57
kodiakhq bot pushed a commit to vercel/next.js that referenced this pull request Jan 11, 2021
**What's the problem this PR addresses?**

- ~~#18768 started to ncc babel and thus it's version of resolve which breaks PnP support~~
Babel replaced `resolve` with the builtin `require.resolve` and a polyfill for older node versions in babel/babel#12439 which was upgraded in #20586
- `next` unnecessarily bundles the `resolve` package when `require.resolve` is builtin and can do the same job

**How did you fix it?**

- ~~Avoid running `resolve` through ncc~~
Added a test for #19334 (closes #19334)
- Replace `resolve` with `require.resolve`
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Mar 6, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: core PR: Internal 🏠 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot find module 'next/babel' with Yarn 2 and Next.js 10.0.2
5 participants