Unverified Commit f3cb9528 authored by Sukka's avatar Sukka Committed by GitHub

fix(worker): pass env to build worker w/ `config.experimental.workerThreads` (#55257)

**TL; DR**

The PR updates the `jest-worker` to the latest `@27` (which is `jest-worker@27.5.1`) and pre-compile.

---

I use `config.experimental.workerThreads` for my personal website built with Next.js, and I noticed that the react alias (from `require-hook`) is not working, causing `react-dom-server-webpack/client.edge` to fail to resolve.

So I modify the `node_modules/next/dist/build/worker.js` in my personal website project folder to print `process.env.__NEXT_PRIVATE_PREBUNDLED_REACT`:

<img width="761" alt="image" src="https://github.com/vercel/next.js/assets/40715044/f22d72f8-ffd4-49d0-831a-ee3bfd0513ca">

To my surprise, the `process.env.__NEXT_PRIVATE_PREBUNDLED_REACT` prints `undefined` with `config.experimental.workerThreads: true`:

<img width="760" alt="image" src="https://github.com/vercel/next.js/assets/40715044/ad8add14-7ac3-45f5-8f1e-2fec18397410">

But If I disable worker threads with `config.experimental.workerThreads: false`, the `process.env.__NEXT_PRIVATE_PREBUNDLED_REACT` is passed down correctly.

So I dig a little deeper. It turns out to be an old issue of `jest-worker` and has been fixed in https://github.com/jestjs/jest/pull/12069 (in that PR, the custom `env` was passed down to `jest-worker`'s `forkOption`). The bugfix was released in `jest-worker@27.4.0`, but Next.js is still using `jest-worker@27.0.0-next.5`, hence the issue.
parent cf3d73a4
...@@ -244,7 +244,7 @@ ...@@ -244,7 +244,7 @@
"image-size": "1.0.0", "image-size": "1.0.0",
"is-docker": "2.0.0", "is-docker": "2.0.0",
"is-wsl": "2.2.0", "is-wsl": "2.2.0",
"jest-worker": "27.0.0-next.5", "jest-worker": "27.5.1",
"json5": "2.2.3", "json5": "2.2.3",
"jsonwebtoken": "9.0.0", "jsonwebtoken": "9.0.0",
"loader-runner": "4.3.0", "loader-runner": "4.3.0",
......
...@@ -60,6 +60,11 @@ function verifyTypeScriptSetup( ...@@ -60,6 +60,11 @@ function verifyTypeScriptSetup(
typeCheckWorker.end() typeCheckWorker.end()
return result return result
}) })
.catch(() => {
// The error is already logged in the worker, we simply exit the main thread to prevent the
// `Jest worker encountered 1 child process exceptions, exceeding retry limit` from showing up
process.exit(1)
})
} }
export async function startTypeChecking({ export async function startTypeChecking({
......
(()=>{"use strict";var __webpack_modules__={841:(e,r)=>{Object.defineProperty(r,"__esModule",{value:true});r.PARENT_MESSAGE_CUSTOM=r.PARENT_MESSAGE_SETUP_ERROR=r.PARENT_MESSAGE_CLIENT_ERROR=r.PARENT_MESSAGE_OK=r.CHILD_MESSAGE_END=r.CHILD_MESSAGE_CALL=r.CHILD_MESSAGE_INITIALIZE=void 0;const t=0;r.CHILD_MESSAGE_INITIALIZE=t;const _=1;r.CHILD_MESSAGE_CALL=_;const n=2;r.CHILD_MESSAGE_END=n;const o=0;r.PARENT_MESSAGE_OK=o;const s=1;r.PARENT_MESSAGE_CLIENT_ERROR=s;const c=2;r.PARENT_MESSAGE_SETUP_ERROR=c;const i=3;r.PARENT_MESSAGE_CUSTOM=i}};var __webpack_module_cache__={};function __nccwpck_require__(e){var r=__webpack_module_cache__[e];if(r!==undefined){return r.exports}var t=__webpack_module_cache__[e]={exports:{}};var _=true;try{__webpack_modules__[e](t,t.exports,__nccwpck_require__);_=false}finally{if(_)delete __webpack_module_cache__[e]}return t.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var __webpack_exports__={};(()=>{function _types(){const e=__nccwpck_require__(841);_types=function(){return e};return e}let file=null;let setupArgs=[];let initialized=false;const messageListener=e=>{switch(e[0]){case _types().CHILD_MESSAGE_INITIALIZE:const r=e;file=r[2];setupArgs=e[3];break;case _types().CHILD_MESSAGE_CALL:const t=e;execMethod(t[2],t[3]);break;case _types().CHILD_MESSAGE_END:end();break;default:throw new TypeError("Unexpected request from parent process: "+e[0])}};process.on("message",messageListener);function reportSuccess(e){if(!process||!process.send){throw new Error("Child can only be used on a forked process")}process.send([_types().PARENT_MESSAGE_OK,e])}function reportClientError(e){return reportError(e,_types().PARENT_MESSAGE_CLIENT_ERROR)}function reportInitializeError(e){return reportError(e,_types().PARENT_MESSAGE_SETUP_ERROR)}function reportError(e,r){if(!process||!process.send){throw new Error("Child can only be used on a forked process")}if(e==null){e=new Error('"null" or "undefined" thrown')}process.send([r,e.constructor&&e.constructor.name,e.message,e.stack,typeof e==="object"?{...e}:e])}function end(){const main=eval("require")(file);if(!main.teardown){exitProcess();return}execFunction(main.teardown,main,[],exitProcess,exitProcess)}function exitProcess(){process.removeListener("message",messageListener)}function execMethod(method,args){const main=eval("require")(file);let fn;if(method==="default"){fn=main.__esModule?main["default"]:main}else{fn=main[method]}function execHelper(){execFunction(fn,main,args,reportSuccess,reportClientError)}if(initialized||!main.setup){execHelper();return}initialized=true;execFunction(main.setup,main,setupArgs,execHelper,reportInitializeError)}const isPromise=e=>!!e&&(typeof e==="object"||typeof e==="function")&&typeof e.then==="function";function execFunction(e,r,t,_,n){let o;try{o=e.apply(r,t)}catch(e){n(e);return}if(isPromise(o)){o.then(_,n)}else{_(o)}}})();module.exports=__webpack_exports__})(); (()=>{"use strict";var __webpack_modules__={582:(e,r)=>{Object.defineProperty(r,"__esModule",{value:true});r.PARENT_MESSAGE_SETUP_ERROR=r.PARENT_MESSAGE_OK=r.PARENT_MESSAGE_CUSTOM=r.PARENT_MESSAGE_CLIENT_ERROR=r.CHILD_MESSAGE_INITIALIZE=r.CHILD_MESSAGE_END=r.CHILD_MESSAGE_CALL=void 0;const _=0;r.CHILD_MESSAGE_INITIALIZE=_;const t=1;r.CHILD_MESSAGE_CALL=t;const n=2;r.CHILD_MESSAGE_END=n;const o=0;r.PARENT_MESSAGE_OK=o;const s=1;r.PARENT_MESSAGE_CLIENT_ERROR=s;const c=2;r.PARENT_MESSAGE_SETUP_ERROR=c;const i=3;r.PARENT_MESSAGE_CUSTOM=i}};var __webpack_module_cache__={};function __nccwpck_require__(e){var r=__webpack_module_cache__[e];if(r!==undefined){return r.exports}var _=__webpack_module_cache__[e]={exports:{}};var t=true;try{__webpack_modules__[e](_,_.exports,__nccwpck_require__);t=false}finally{if(t)delete __webpack_module_cache__[e]}return _.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var __webpack_exports__={};(()=>{var _types=__nccwpck_require__(582);let file=null;let setupArgs=[];let initialized=false;const messageListener=e=>{switch(e[0]){case _types.CHILD_MESSAGE_INITIALIZE:const r=e;file=r[2];setupArgs=e[3];break;case _types.CHILD_MESSAGE_CALL:const _=e;execMethod(_[2],_[3]);break;case _types.CHILD_MESSAGE_END:end();break;default:throw new TypeError("Unexpected request from parent process: "+e[0])}};process.on("message",messageListener);function reportSuccess(e){if(!process||!process.send){throw new Error("Child can only be used on a forked process")}process.send([_types.PARENT_MESSAGE_OK,e])}function reportClientError(e){return reportError(e,_types.PARENT_MESSAGE_CLIENT_ERROR)}function reportInitializeError(e){return reportError(e,_types.PARENT_MESSAGE_SETUP_ERROR)}function reportError(e,r){if(!process||!process.send){throw new Error("Child can only be used on a forked process")}if(e==null){e=new Error('"null" or "undefined" thrown')}process.send([r,e.constructor&&e.constructor.name,e.message,e.stack,typeof e==="object"?{...e}:e])}function end(){const main=eval("require")(file);if(!main.teardown){exitProcess();return}execFunction(main.teardown,main,[],exitProcess,exitProcess)}function exitProcess(){process.removeListener("message",messageListener)}function execMethod(method,args){const main=eval("require")(file);let fn;if(method==="default"){fn=main.__esModule?main["default"]:main}else{fn=main[method]}function execHelper(){execFunction(fn,main,args,reportSuccess,reportClientError)}if(initialized||!main.setup){execHelper();return}initialized=true;execFunction(main.setup,main,setupArgs,execHelper,reportInitializeError)}const isPromise=e=>!!e&&(typeof e==="object"||typeof e==="function")&&typeof e.then==="function";function execFunction(e,r,_,t,n){let o;try{o=e.apply(r,_)}catch(e){n(e);return}if(isPromise(o)){o.then(t,n)}else{t(o)}}})();module.exports=__webpack_exports__})();
\ No newline at end of file \ No newline at end of file
(()=>{"use strict";var __webpack_modules__={841:(e,r)=>{Object.defineProperty(r,"__esModule",{value:true});r.PARENT_MESSAGE_CUSTOM=r.PARENT_MESSAGE_SETUP_ERROR=r.PARENT_MESSAGE_CLIENT_ERROR=r.PARENT_MESSAGE_OK=r.CHILD_MESSAGE_END=r.CHILD_MESSAGE_CALL=r.CHILD_MESSAGE_INITIALIZE=void 0;const t=0;r.CHILD_MESSAGE_INITIALIZE=t;const _=1;r.CHILD_MESSAGE_CALL=_;const n=2;r.CHILD_MESSAGE_END=n;const o=0;r.PARENT_MESSAGE_OK=o;const s=1;r.PARENT_MESSAGE_CLIENT_ERROR=s;const i=2;r.PARENT_MESSAGE_SETUP_ERROR=i;const a=3;r.PARENT_MESSAGE_CUSTOM=a},267:e=>{e.exports=require("worker_threads")}};var __webpack_module_cache__={};function __nccwpck_require__(e){var r=__webpack_module_cache__[e];if(r!==undefined){return r.exports}var t=__webpack_module_cache__[e]={exports:{}};var _=true;try{__webpack_modules__[e](t,t.exports,__nccwpck_require__);_=false}finally{if(_)delete __webpack_module_cache__[e]}return t.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var __webpack_exports__={};(()=>{function _worker_threads(){const e=__nccwpck_require__(267);_worker_threads=function(){return e};return e}function _types(){const e=__nccwpck_require__(841);_types=function(){return e};return e}let file=null;let setupArgs=[];let initialized=false;const messageListener=e=>{switch(e[0]){case _types().CHILD_MESSAGE_INITIALIZE:const r=e;file=r[2];setupArgs=e[3];break;case _types().CHILD_MESSAGE_CALL:const t=e;execMethod(t[2],t[3]);break;case _types().CHILD_MESSAGE_END:end();break;default:throw new TypeError("Unexpected request from parent process: "+e[0])}};_worker_threads().parentPort.on("message",messageListener);function reportSuccess(e){if(_worker_threads().isMainThread){throw new Error("Child can only be used on a forked process")}_worker_threads().parentPort.postMessage([_types().PARENT_MESSAGE_OK,e])}function reportClientError(e){return reportError(e,_types().PARENT_MESSAGE_CLIENT_ERROR)}function reportInitializeError(e){return reportError(e,_types().PARENT_MESSAGE_SETUP_ERROR)}function reportError(e,r){if(_worker_threads().isMainThread){throw new Error("Child can only be used on a forked process")}if(e==null){e=new Error('"null" or "undefined" thrown')}_worker_threads().parentPort.postMessage([r,e.constructor&&e.constructor.name,e.message,e.stack,typeof e==="object"?{...e}:e])}function end(){const main=eval("require")(file);if(!main.teardown){exitProcess();return}execFunction(main.teardown,main,[],exitProcess,exitProcess)}function exitProcess(){_worker_threads().parentPort.removeListener("message",messageListener)}function execMethod(method,args){const main=eval("require")(file);let fn;if(method==="default"){fn=main.__esModule?main["default"]:main}else{fn=main[method]}function execHelper(){execFunction(fn,main,args,reportSuccess,reportClientError)}if(initialized||!main.setup){execHelper();return}initialized=true;execFunction(main.setup,main,setupArgs,execHelper,reportInitializeError)}const isPromise=e=>!!e&&(typeof e==="object"||typeof e==="function")&&typeof e.then==="function";function execFunction(e,r,t,_,n){let o;try{o=e.apply(r,t)}catch(e){n(e);return}if(isPromise(o)){o.then(_,n)}else{_(o)}}})();module.exports=__webpack_exports__})(); (()=>{"use strict";var __webpack_modules__={582:(e,r)=>{Object.defineProperty(r,"__esModule",{value:true});r.PARENT_MESSAGE_SETUP_ERROR=r.PARENT_MESSAGE_OK=r.PARENT_MESSAGE_CUSTOM=r.PARENT_MESSAGE_CLIENT_ERROR=r.CHILD_MESSAGE_INITIALIZE=r.CHILD_MESSAGE_END=r.CHILD_MESSAGE_CALL=void 0;const t=0;r.CHILD_MESSAGE_INITIALIZE=t;const _=1;r.CHILD_MESSAGE_CALL=_;const n=2;r.CHILD_MESSAGE_END=n;const o=0;r.PARENT_MESSAGE_OK=o;const s=1;r.PARENT_MESSAGE_CLIENT_ERROR=s;const i=2;r.PARENT_MESSAGE_SETUP_ERROR=i;const a=3;r.PARENT_MESSAGE_CUSTOM=a},267:e=>{e.exports=require("worker_threads")}};var __webpack_module_cache__={};function __nccwpck_require__(e){var r=__webpack_module_cache__[e];if(r!==undefined){return r.exports}var t=__webpack_module_cache__[e]={exports:{}};var _=true;try{__webpack_modules__[e](t,t.exports,__nccwpck_require__);_=false}finally{if(_)delete __webpack_module_cache__[e]}return t.exports}if(typeof __nccwpck_require__!=="undefined")__nccwpck_require__.ab=__dirname+"/";var __webpack_exports__={};(()=>{function _worker_threads(){const e=__nccwpck_require__(267);_worker_threads=function(){return e};return e}var _types=__nccwpck_require__(582);let file=null;let setupArgs=[];let initialized=false;const messageListener=e=>{switch(e[0]){case _types.CHILD_MESSAGE_INITIALIZE:const r=e;file=r[2];setupArgs=e[3];process.env.JEST_WORKER_ID=e[4];break;case _types.CHILD_MESSAGE_CALL:const t=e;execMethod(t[2],t[3]);break;case _types.CHILD_MESSAGE_END:end();break;default:throw new TypeError("Unexpected request from parent process: "+e[0])}};_worker_threads().parentPort.on("message",messageListener);function reportSuccess(e){if(_worker_threads().isMainThread){throw new Error("Child can only be used on a forked process")}_worker_threads().parentPort.postMessage([_types.PARENT_MESSAGE_OK,e])}function reportClientError(e){return reportError(e,_types.PARENT_MESSAGE_CLIENT_ERROR)}function reportInitializeError(e){return reportError(e,_types.PARENT_MESSAGE_SETUP_ERROR)}function reportError(e,r){if(_worker_threads().isMainThread){throw new Error("Child can only be used on a forked process")}if(e==null){e=new Error('"null" or "undefined" thrown')}_worker_threads().parentPort.postMessage([r,e.constructor&&e.constructor.name,e.message,e.stack,typeof e==="object"?{...e}:e])}function end(){const main=eval("require")(file);if(!main.teardown){exitProcess();return}execFunction(main.teardown,main,[],exitProcess,exitProcess)}function exitProcess(){_worker_threads().parentPort.removeListener("message",messageListener)}function execMethod(method,args){const main=eval("require")(file);let fn;if(method==="default"){fn=main.__esModule?main["default"]:main}else{fn=main[method]}function execHelper(){execFunction(fn,main,args,reportSuccess,reportClientError)}if(initialized||!main.setup){execHelper();return}initialized=true;execFunction(main.setup,main,setupArgs,execHelper,reportInitializeError)}const isPromise=e=>!!e&&(typeof e==="object"||typeof e==="function")&&typeof e.then==="function";function execFunction(e,r,t,_,n){let o;try{o=e.apply(r,t)}catch(e){n(e);return}if(isPromise(o)){o.then(_,n)}else{_(o)}}})();module.exports=__webpack_exports__})();
\ No newline at end of file \ No newline at end of file
...@@ -5,10 +5,10 @@ import { MissingDependency } from '../has-necessary-dependencies' ...@@ -5,10 +5,10 @@ import { MissingDependency } from '../has-necessary-dependencies'
import { FatalError } from '../fatal-error' import { FatalError } from '../fatal-error'
import { getPkgManager } from '../helpers/get-pkg-manager' import { getPkgManager } from '../helpers/get-pkg-manager'
export async function missingDepsError( export function missingDepsError(
dir: string, dir: string,
missingPackages: MissingDependency[] missingPackages: MissingDependency[]
) { ): never {
const packagesHuman = getOxfordCommaList(missingPackages.map((p) => p.pkg)) const packagesHuman = getOxfordCommaList(missingPackages.map((p) => p.pkg))
const packagesCli = missingPackages.map((p) => p.pkg).join(' ') const packagesCli = missingPackages.map((p) => p.pkg).join(' ')
const packageManager = getPkgManager(dir) const packageManager = getPkgManager(dir)
......
...@@ -7,7 +7,6 @@ import { ...@@ -7,7 +7,6 @@ import {
} from './has-necessary-dependencies' } from './has-necessary-dependencies'
import semver from 'next/dist/compiled/semver' import semver from 'next/dist/compiled/semver'
import { CompileError } from './compile-error' import { CompileError } from './compile-error'
import { FatalError } from './fatal-error'
import * as log from '../build/output/log' import * as log from '../build/output/log'
import { getTypeScriptIntent } from './typescript/getTypeScriptIntent' import { getTypeScriptIntent } from './typescript/getTypeScriptIntent'
...@@ -76,7 +75,7 @@ export async function verifyTypeScriptSetup({ ...@@ -76,7 +75,7 @@ export async function verifyTypeScriptSetup({
if (isCI) { if (isCI) {
// we don't attempt auto install in CI to avoid side-effects // we don't attempt auto install in CI to avoid side-effects
// and instead log the error for installing needed packages // and instead log the error for installing needed packages
await missingDepsError(dir, deps.missing) missingDepsError(dir, deps.missing)
} }
console.log( console.log(
chalk.bold.yellow( chalk.bold.yellow(
...@@ -156,10 +155,24 @@ export async function verifyTypeScriptSetup({ ...@@ -156,10 +155,24 @@ export async function verifyTypeScriptSetup({
console.error(chalk.red('Failed to compile.\n')) console.error(chalk.red('Failed to compile.\n'))
console.error(err.message) console.error(err.message)
process.exit(1) process.exit(1)
} else if (err instanceof FatalError) { }
console.error(err.message)
/**
* verifyTypeScriptSetup can be either invoked directly in the main thread (during next dev / next lint)
* or run in a worker (during next build). In the latter case, we need to print the error message, as the
* parent process will only receive an `Jest worker encountered 1 child process exceptions, exceeding retry limit`.
*/
// we are in a worker, print the error message and exit the process
if (process.env.JEST_WORKER_ID) {
if (err instanceof Error) {
console.error(err.message)
} else {
console.error(err)
}
process.exit(1) process.exit(1)
} }
// we are in the main thread, throw the error and it will be handled by the caller
throw err throw err
} }
} }
...@@ -1214,8 +1214,8 @@ importers: ...@@ -1214,8 +1214,8 @@ importers:
specifier: 2.2.0 specifier: 2.2.0
version: 2.2.0 version: 2.2.0
jest-worker: jest-worker:
specifier: 27.0.0-next.5 specifier: 27.5.1
version: 27.0.0-next.5 version: 27.5.1
json5: json5:
specifier: 2.2.3 specifier: 2.2.3
version: 2.2.3 version: 2.2.3
...@@ -1326,7 +1326,7 @@ importers: ...@@ -1326,7 +1326,7 @@ importers:
version: 0.13.4 version: 0.13.4
sass-loader: sass-loader:
specifier: 12.4.0 specifier: 12.4.0
version: 12.4.0(sass@1.54.0)(webpack@5.86.0) version: 12.4.0(webpack@5.86.0)
schema-utils2: schema-utils2:
specifier: npm:schema-utils@2.7.1 specifier: npm:schema-utils@2.7.1
version: /schema-utils@2.7.1 version: /schema-utils@2.7.1
...@@ -17232,15 +17232,6 @@ packages: ...@@ -17232,15 +17232,6 @@ packages:
supports-color: 7.2.0 supports-color: 7.2.0
dev: true dev: true
/jest-worker@27.0.0-next.5:
resolution: {integrity: sha512-mk0umAQ5lT+CaOJ+Qp01N6kz48sJG2kr2n1rX0koqKf6FIygQV0qLOdN9SCYID4IVeSigDOcPeGLozdMLYfb5g==}
engines: {node: ^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0}
dependencies:
'@types/node': 20.2.5
merge-stream: 2.0.0
supports-color: 8.1.1
dev: true
/jest-worker@27.5.1: /jest-worker@27.5.1:
resolution: {integrity: sha512-7vuh85V5cdDofPyxn58nrPjBktZo0u9x1g8WtjQol+jZDaE+fhN+cIvTj11GndBnMnyfrUOG1sZQxCdjKh+DKg==} resolution: {integrity: sha512-7vuh85V5cdDofPyxn58nrPjBktZo0u9x1g8WtjQol+jZDaE+fhN+cIvTj11GndBnMnyfrUOG1sZQxCdjKh+DKg==}
engines: {node: '>= 10.13.0'} engines: {node: '>= 10.13.0'}
...@@ -23698,7 +23689,7 @@ packages: ...@@ -23698,7 +23689,7 @@ packages:
/safer-buffer@2.1.2: /safer-buffer@2.1.2:
resolution: {integrity: sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==} resolution: {integrity: sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==}
/sass-loader@12.4.0(sass@1.54.0)(webpack@5.86.0): /sass-loader@12.4.0(webpack@5.86.0):
resolution: {integrity: sha512-7xN+8khDIzym1oL9XyS6zP6Ges+Bo2B2xbPrjdMHEYyV3AQYhd/wXeru++3ODHF0zMjYmVadblSKrPrjEkL8mg==} resolution: {integrity: sha512-7xN+8khDIzym1oL9XyS6zP6Ges+Bo2B2xbPrjdMHEYyV3AQYhd/wXeru++3ODHF0zMjYmVadblSKrPrjEkL8mg==}
engines: {node: '>= 12.13.0'} engines: {node: '>= 12.13.0'}
peerDependencies: peerDependencies:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment