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

Don't compile import() in development #12288

Merged
merged 8 commits into from Nov 18, 2020

Conversation

nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Oct 30, 2020

Q                       A
Tests Added + Pass? Yes
License MIT

With this PR, we:

  1. Stop compiling import() to require in development
  2. Align prepublish tests with "normal" tests
  3. Always test the native import() when it's supported, and skip those tests in older Node.js versions

I wanted to use the native Jest support for import(), but I'm getting some segmentation faults so this PR doesn't remove the loadOptionsAsyncInSpawedProcess hack.

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

babel-bot commented Oct 30, 2020

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

@nicolo-ribaudo
Copy link
Member Author

Oh no another segfault 🙃

@codesandbox-ci
Copy link

codesandbox-ci bot commented Oct 30, 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 c12b413:

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

function loadOptionsAsync({ filename, cwd = __dirname }, mjs) {
if (mjs) {
// import() crashes with jest
return loadOptionsAsyncInSpawedProcess({ filename, cwd });
Copy link
Member Author

Choose a reason for hiding this comment

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

@SimenB This is needed to spawn tests using import() in a different process so that Jest doesn't have to handle them.

If you remove this line (and thus run the tests in the current Node.js process managed by Jest), and run node --experimental-vm-modules ./node_modules/.bin/jest babel-core/test/config-chain it crashes.

The file containing import() is https://github.com/babel/babel/blob/main/packages/babel-core/src/config/files/import.js, and you can Ctrl+F for "babel.config.mjs" in this file to find a test using import() internally.

I'll create a branch with a smaller test file to make it easier to isolate the problem (even if I'm not able to reproduce it in a fresh project containing only Jest).

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, this is weird. The importModuleDynamically callback isn't invoked at all - I'm guessing this is some edge case in Node's implementation

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it will be better if I post the reproduction to the Node.js repo then: even if the tests or Jest did something wrong (but from my superficial debugging I don't think so), Node.js shouldn't crash.

Copy link
Contributor

@SimenB SimenB Oct 30, 2020

Choose a reason for hiding this comment

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

From debugging - putting a breakpoint on the import() expression shows it is called with an URL with the following href: file:///var/folders/gj/0mygpdfn6598xh34njlyrqzc0000gn/T/babel-test-load-config-async-babel.config.mjsYKe5KD/babel.config.mjs. This file exists in disk and is valid ESM. Then doing "Step over next function call" crashes with a segfault without stopping the debugger.

PID 43445 received SIGSEGV for address: 0x0
0   segfault-handler.node               0x00000001055e3fa0 _ZL16segfault_handleriP9__siginfoPv + 304
1   libsystem_platform.dylib            0x00007fff6951f5fd _sigtramp + 29
2   ???                                 0x0000000000000007 0x0 + 7
3   node                                0x000000010033df4a _ZN2v88internal7Isolate38RunHostImportModuleDynamicallyCallbackENS0_6HandleINS0_6ScriptEEENS2_INS0_6ObjectEEE + 138
4   node                                0x00000001006f82f4 _ZN2v88internal25Runtime_DynamicImportCallEiPmPNS0_7IsolateE + 340
5   node                                0x0000000100a797b4 Builtins_CEntry_Return1_DontSaveFPRegs_ArgvInRegister_NoBuiltinExit + 52
[1]    43445 segmentation fault  node --experimental-vm-modules --inspect-brk ./node_modules/.bin/jest 

(via https://www.npmjs.com/package/segfault-handler)

Copy link
Member Author

Choose a reason for hiding this comment

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

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.

LGTM with some nits.

// "minNodeVersion": "10.0.0" <-- For Ctrl+F when dropping node 10
const supportsESM = parseInt(process.versions.node) >= 12;

const isMJS = file => file.slice(-4) === ".mjs";
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: String#endsWith is available since Node.js 4

Suggested change
const isMJS = file => file.slice(-4) === ".mjs";
const isMJS = file => file.endsWith(".mjs");

Copy link
Contributor

Choose a reason for hiding this comment

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

can use https://nodejs.org/api/path.html#path_path_extname_path as well, dunno if it's any better, tho

);
return true;
}
if (esm && process.platform === "win32") {

This comment was marked as outdated.

This comment was marked as outdated.

packages/babel-core/test/config-chain.js Show resolved Hide resolved
@nicolo-ribaudo nicolo-ribaudo merged commit 6a0e909 into babel:main Nov 18, 2020
@nicolo-ribaudo nicolo-ribaudo deleted the native-dynamic-import branch November 18, 2020 15:02
@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 Feb 18, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 18, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: tests 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.

None yet

5 participants