Skip to content

Commit

Permalink
fix(@angular/build): mitigate JS transformer worker execArgv errors
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
clydin committed Dec 20, 2024
1 parent 46306ac commit 5cc62d4
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions packages/angular/build/src/tools/esbuild/javascript-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 5cc62d4

Please sign in to comment.