From 99999cc8d609895de6b2d5021c2b619924b7699a Mon Sep 17 00:00:00 2001 From: chris Date: Fri, 20 Sep 2024 04:20:04 -0700 Subject: [PATCH] benchmark: for esm when require.main is undefined, probe filename from error stack --- benchmark/common.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/benchmark/common.js b/benchmark/common.js index b4978e8e140b18..9bb9c91b73ea1b 100644 --- a/benchmark/common.js +++ b/benchmark/common.js @@ -19,7 +19,16 @@ class Benchmark { this._time = 0n; // Use the file name as the name of the benchmark - this.name = require.main.filename.slice(__dirname.length + 1); + // For esm, require.main is undefined so probe filename from error stack + this.mainfilename = require.main + ? require.main.filename + : (new Error).stack.split('\n')[3] + .match(/^.*(\(|at )(.*):[\d]*:[\d]*.*$/)[2] + .replace(/^file:\/\//, '') + + this.name = require.main + ? require.main.filename.slice(__dirname.length + 1) + : this.mainfilename.match(/[^\/]*$/)[0] // Execution arguments i.e. flags used to run the jobs this.flags = process.env.NODE_BENCHMARK_FLAGS?.split(/\s+/) ?? []; @@ -224,7 +233,7 @@ class Benchmark { childArgs.push(`${key}=${value}`); } - const child = child_process.fork(require.main.filename, childArgs, { + const child = child_process.fork(this.mainfilename, childArgs, { env: childEnv, execArgv: this.flags.concat(process.execArgv), });