Skip to content

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

Closed
@aduh95

Description

@aduh95
  • 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingreproducibleThis issue can be successfully reproduced

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions