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

[Bug] Crash when spawning an ES module worker_thread #906

Closed
1 task
aduh95 opened this issue Feb 6, 2020 · 2 comments
Closed
1 task

[Bug] Crash when spawning an ES module worker_thread #906

aduh95 opened this issue Feb 6, 2020 · 2 comments
Labels
bug Something isn't working reproducible This issue can be successfully reproduced

Comments

@aduh95
Copy link
Contributor

aduh95 commented Feb 6, 2020

  • I'd be willing to implement a fix

Describe the bug

Yarn 2 crashes when loading a worker file written as ES module.

To Reproduce

const {promises: {writeFile}} = require(`fs`);

await packageJsonAndInstall({});

const WORKER_PATH = './worker.mjs';

await writeFile(WORKER_PATH, `
import { isMainThread, Worker } from "worker_threads";

if (isMainThread) {
  new Worker("${WORKER_PATH}");
} else {
  // Worker code here
}
`)

await expect(yarn('node', WORKER_PATH))
  .rejects.not.toThrow(`Must use import to load ES Module`);

Environment if relevant (please complete the following information):

  • Node version [e.g. 8.15.0, 10.15.1, ...] 13.7.0
  • Yarn version [e.g. 2.0.0-rc1, ...] master (cae4477)

Additional context

Node.js can load script workers as well as ES modules workers (although ESM is still experimental at time of writing). It uses the same resolution algorithm as non-worker file to pick EMS loader or CJS loader. To qualify as ESM, a file is either:

  • using .mjs as extension.
  • using .js as extension and the closest package.json contains type=module.
@aduh95 aduh95 added the bug Something isn't working label Feb 6, 2020
@yarnbot yarnbot added the reproducible This issue can be successfully reproduced label Feb 6, 2020
@yarnbot
Copy link
Collaborator

yarnbot commented Feb 6, 2020

This issue reproduces on master:

Error: expect(received).rejects.not.toThrow(expected)

Expected substring: not "Must use import to load ES Module"
Received message:       "Command failed: /usr/bin/node /github/workspace/scripts/actions/../run-yarn.js node ./worker.mjs
internal/modules/cjs/loader.js:1029
  throw new ERR_REQUIRE_ESM(filename);
  ^

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /tmp/tmp-27QR87OnDqLgxB/worker.mjs
    at Object.Module._extensions..mjs (internal/modules/cjs/loader.js:1029:9)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.module_1.Module._load (/tmp/tmp-27QR87OnDqLgxB/.pnp.js:13581:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
    at internal/main/run_main_module.js:17:11 {
  code: 'ERR_REQUIRE_ESM'
}

"

      13579 | 
      13580 |     try {
    > 13581 |       module.load(modulePath);
            |              ^
      13582 |       hasThrown = false;
      13583 |     } finally {
      13584 |       if (hasThrown) {

      at Function.module_1.Module._load (.pnp.js:13581:14)
      at internal/main/run_main_module.js:17:11 {
        code: 'ERR_REQUIRE_ESM'
      }
    at Object.toThrow (/github/workspace/.yarn/cache/expect-npm-24.8.0-8c7640c562-2.zip/node_modules/expect/build/index.js:242:20)
    at module.exports (evalmachine.<anonymous>:19:16)
    at async /github/workspace/.yarn/cache/@arcanis-sherlock-npm-1.0.38-d4f5e2dbf3-2.zip/node_modules/@arcanis/sherlock/lib/executeRepro.js:56:13
    at async executeInTempDirectory (/github/workspace/.yarn/cache/@arcanis-sherlock-npm-1.0.38-d4f5e2dbf3-2.zip/node_modules/@arcanis/sherlock/lib/executeRepro.js:17:16)
    at async Object.executeRepro (/github/workspace/.yarn/cache/@arcanis-sherlock-npm-1.0.38-d4f5e2dbf3-2.zip/node_modules/@arcanis/sherlock/lib/executeRepro.js:24:12)
    at async ExecCommand.execute (/github/workspace/.yarn/cache/@arcanis-sherlock-npm-1.0.38-d4f5e2dbf3-2.zip/node_modules/@arcanis/sherlock/lib/commands/exec.js:25:38)
    at async ExecCommand.validateAndExecute (/github/workspace/.yarn/cache/clipanion-npm-2.0.0-rc.16-b9444aaf89-2.zip/node_modules/clipanion/lib/advanced/Command.js:161:26)
    at async Cli.run (/github/workspace/.yarn/cache/clipanion-npm-2.0.0-rc.16-b9444aaf89-2.zip/node_modules/clipanion/lib/advanced/Cli.js:74:24)
    at async Cli.runExit (/github/workspace/.yarn/cache/clipanion-npm-2.0.0-rc.16-b9444aaf89-2.zip/node_modules/clipanion/lib/advanced/Cli.js:83:28)

@merceyz
Copy link
Member

merceyz commented Jun 15, 2020

Tracked in #638

@merceyz merceyz closed this as completed Jun 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working reproducible This issue can be successfully reproduced
Projects
None yet
Development

No branches or pull requests

3 participants