Skip to content

Commit 1e2fab5

Browse files
committed
benchmark: use options.filename when, for esm, require.main.filename is undefined
1 parent 4f70132 commit 1e2fab5

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

benchmark/common.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,16 @@ class Benchmark {
1919
this._time = 0n;
2020

2121
// Use the file name as the name of the benchmark
22-
this.name = require.main.filename.slice(__dirname.length + 1);
22+
// For esm, require.main is undefined so probe filename from error stack
23+
this.mainfilename = require.main
24+
? require.main.filename
25+
: (new Error).stack.split('\n')[3]
26+
.match(/^.*(\(|at )(.*):[\d]*:[\d]*.*$/)[2]
27+
.replace(/^file:\/\//, '')
28+
29+
this.name = require.main
30+
? require.main.filename.slice(__dirname.length + 1)
31+
: this.mainfilename.match(/[^\/]*$/)[0]
2332

2433
// Execution arguments i.e. flags used to run the jobs
2534
this.flags = process.env.NODE_BENCHMARK_FLAGS?.split(/\s+/) ?? [];
@@ -224,7 +233,7 @@ class Benchmark {
224233
childArgs.push(`${key}=${value}`);
225234
}
226235

227-
const child = child_process.fork(require.main.filename, childArgs, {
236+
const child = child_process.fork(this.mainfilename, childArgs, {
228237
env: childEnv,
229238
execArgv: this.flags.concat(process.execArgv),
230239
});

0 commit comments

Comments
 (0)