From c8b5f7f2df46ffbde7d4774d9d2c16f57128f926 Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Thu, 19 Dec 2024 19:52:13 -0500 Subject: [PATCH] fix(@angular/build): mitigate JS transformer worker execArgv errors Node.js workers will currently fail to initialize if the `execArgv` option is used and it contains v8 specific options. This is currently problematic for the JS transformer worker because it contains a workaround to remove the SSR `--import` argument that is used to add a loader hook for SSR purposes. The filtering of the argument and subsequent use of the `execArgv` array had the potential to pass custom Node.js options to the worker and cause it to fail. These options can be passed by developers on the command line when invoking the Angular CLI. To mitigate this problem, the `execArgv` option is now only filtered and used if the SSR import argument is present in the array. Otherwise, no value is passed which allows the default Node.js behavior to be used. While this does not fully solve the problem for all projects, it does remove the problem from non-SSR projects. --- .../src/tools/esbuild/javascript-transformer.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/angular/build/src/tools/esbuild/javascript-transformer.ts b/packages/angular/build/src/tools/esbuild/javascript-transformer.ts index 202d922f40ea..8e2d8e31ab8f 100644 --- a/packages/angular/build/src/tools/esbuild/javascript-transformer.ts +++ b/packages/angular/build/src/tools/esbuild/javascript-transformer.ts @@ -9,7 +9,7 @@ import { createHash } from 'node:crypto'; import { readFile } from 'node:fs/promises'; import { IMPORT_EXEC_ARGV } from '../../utils/server-rendering/esm-in-memory-loader/utils'; -import { WorkerPool } from '../../utils/worker-pool'; +import { WorkerPool, WorkerPoolOptions } from '../../utils/worker-pool'; import { Cache } from './cache'; /** @@ -56,12 +56,18 @@ export class JavaScriptTransformer { } #ensureWorkerPool(): WorkerPool { - this.#workerPool ??= new WorkerPool({ + const workerPoolOptions: WorkerPoolOptions = { filename: require.resolve('./javascript-transformer-worker'), maxThreads: this.maxThreads, - // Prevent passing `--import` (loader-hooks) from parent to child worker. - execArgv: process.execArgv.filter((v) => v !== IMPORT_EXEC_ARGV), - }); + }; + + // Prevent passing SSR `--import` (loader-hooks) from parent to child worker. + const filteredExecArgv = process.execArgv.filter((v) => v !== IMPORT_EXEC_ARGV); + if (process.execArgv.length !== filteredExecArgv.length) { + workerPoolOptions.execArgv = filteredExecArgv; + } + + this.#workerPool ??= new WorkerPool(workerPoolOptions); return this.#workerPool; }