From e06bcd001cccf656a4b6e6e0c94df3d9e27d3a40 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 22 Jan 2026 20:19:14 +0100 Subject: [PATCH] module: do not wrap module._load when tracing is not enabled This prevents clobbering the stack traces with another internal frame and removes the unnecessary hoops from step-debugging. --- lib/internal/modules/cjs/loader.js | 16 ++++++---- test/fixtures/console/console.snapshot | 1 - test/fixtures/errors/force_colors.snapshot | 1 - ...promise_unhandled_warn_with_error.snapshot | 1 - .../unhandled_promise_trace_warnings.snapshot | 2 -- .../source_map_assert_source_line.snapshot | 1 - test/message/assert_throws_stack.out | 1 - test/message/internal_assert.out | 1 - test/message/internal_assert_fail.out | 1 - test/message/util-inspect-error-cause.out | 30 +++++++------------ test/message/util_inspect_error.out | 5 ---- test/pseudo-tty/console_colors.out | 4 --- test/pseudo-tty/test-fatal-error.out | 1 - test/pseudo-tty/test-start-trace-sigint.out | 1 - test/pseudo-tty/test-trace-sigint.out | 1 - 15 files changed, 21 insertions(+), 46 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index d9fc92bbc813e8..0ea268bcec7ece 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -238,15 +238,21 @@ let statCache = null; function wrapModuleLoad(request, parent, isMain) { const logLabel = `[${parent?.id || ''}] [${request}]`; const traceLabel = `require('${request}')`; + const channel = onRequire(); startTimer(logLabel, traceLabel); try { - return onRequire().traceSync(Module._load, { - __proto__: null, - parentFilename: parent?.filename, - id: request, - }, Module, request, parent, isMain); + if (channel.hasSubscribers) { + return onRequire().traceSync(Module._load, { + __proto__: null, + parentFilename: parent?.filename, + id: request, + }, Module, request, parent, isMain); + } + // No subscribers, skip the wrapping to avoid clobbering stack traces + // and debugging steps. + return Module._load(request, parent, isMain); } finally { endTimer(logLabel, traceLabel); } diff --git a/test/fixtures/console/console.snapshot b/test/fixtures/console/console.snapshot index 41e7d16fb993a6..4f1cb254811b6d 100644 --- a/test/fixtures/console/console.snapshot +++ b/test/fixtures/console/console.snapshot @@ -7,4 +7,3 @@ Trace: foo at * at * at * - at * diff --git a/test/fixtures/errors/force_colors.snapshot b/test/fixtures/errors/force_colors.snapshot index 93ac005e833ce6..a93eea815b3152 100644 --- a/test/fixtures/errors/force_colors.snapshot +++ b/test/fixtures/errors/force_colors.snapshot @@ -11,6 +11,5 @@ Error: Should include grayed stack trace  at *  at *  at * - at * Node.js * diff --git a/test/fixtures/errors/promise_unhandled_warn_with_error.snapshot b/test/fixtures/errors/promise_unhandled_warn_with_error.snapshot index 4b3ed8640dc221..2a2ec57dc1cfcd 100644 --- a/test/fixtures/errors/promise_unhandled_warn_with_error.snapshot +++ b/test/fixtures/errors/promise_unhandled_warn_with_error.snapshot @@ -7,6 +7,5 @@ at * at * at * - at * (Use `* --trace-warnings ...` to show where the warning was created) (node:*) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https:*nodejs.org*api*cli.html#cli_unhandled_rejections_mode). (rejection id: 1) diff --git a/test/fixtures/errors/unhandled_promise_trace_warnings.snapshot b/test/fixtures/errors/unhandled_promise_trace_warnings.snapshot index 246b82a8d5accb..94c709b17788c4 100644 --- a/test/fixtures/errors/unhandled_promise_trace_warnings.snapshot +++ b/test/fixtures/errors/unhandled_promise_trace_warnings.snapshot @@ -11,7 +11,6 @@ at * at * at * - at * (node:*) Error: This was rejected at * at * @@ -21,7 +20,6 @@ at * at * at * - at * (node:*) PromiseRejectionHandledWarning: Promise rejection was handled asynchronously (rejection id: 1) at * at * diff --git a/test/fixtures/source-map/output/source_map_assert_source_line.snapshot b/test/fixtures/source-map/output/source_map_assert_source_line.snapshot index 62e611f330f97f..d42bc01cf758cf 100644 --- a/test/fixtures/source-map/output/source_map_assert_source_line.snapshot +++ b/test/fixtures/source-map/output/source_map_assert_source_line.snapshot @@ -10,7 +10,6 @@ AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value: * * * - * generatedMessage: true, code: 'ERR_ASSERTION', actual: false, diff --git a/test/message/assert_throws_stack.out b/test/message/assert_throws_stack.out index 2b5587292a2c7a..1a90283b534817 100644 --- a/test/message/assert_throws_stack.out +++ b/test/message/assert_throws_stack.out @@ -17,7 +17,6 @@ AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal: at * at * at * - at * at * { generatedMessage: true, code: 'ERR_ASSERTION', diff --git a/test/message/internal_assert.out b/test/message/internal_assert.out index 87aaab00370d00..e4f67f650f938e 100644 --- a/test/message/internal_assert.out +++ b/test/message/internal_assert.out @@ -13,7 +13,6 @@ Please open an issue with this stack trace at https://github.com/nodejs/node/iss at * at * at * - at * at * { code: 'ERR_INTERNAL_ASSERTION' } diff --git a/test/message/internal_assert_fail.out b/test/message/internal_assert_fail.out index 5f1026791f323d..c15cc8ca542d7c 100644 --- a/test/message/internal_assert_fail.out +++ b/test/message/internal_assert_fail.out @@ -14,7 +14,6 @@ Please open an issue with this stack trace at https://github.com/nodejs/node/iss at * at * at * - at * at * { code: 'ERR_INTERNAL_ASSERTION' } diff --git a/test/message/util-inspect-error-cause.out b/test/message/util-inspect-error-cause.out index 6e2caf7f05102c..344ace1bc94074 100644 --- a/test/message/util-inspect-error-cause.out +++ b/test/message/util-inspect-error-cause.out @@ -6,7 +6,6 @@ Error: Number error cause at * at * at * - at * at * { [cause]: 42 } @@ -18,7 +17,6 @@ Error: Object cause at * at * at * - at * at * { [cause]: { message: 'Unique', @@ -35,7 +33,6 @@ Error: undefined cause at * at * at * - at * at * { [cause]: undefined } @@ -47,7 +44,6 @@ Error: cause that throws at * at * at * - at * at * { [cause]: [Getter] } @@ -57,7 +53,7 @@ RangeError: New Stack Frames [cause]: FoobarError: Individual message at * *[90m at *[39m - *[90m ... 6 lines matching cause stack trace ...*[39m + *[90m ... 5 lines matching cause stack trace ...*[39m *[90m at *[39m { status: *[32m'Feeling good'*[39m, extraProperties: *[32m'Yes!'*[39m, @@ -70,18 +66,17 @@ RangeError: New Stack Frames *[90m at *[39m *[90m at *[39m *[90m at *[39m - *[90m at *[39m } } Error: Stack causes at * *[90m at *[39m -*[90m ... 6 lines matching cause stack trace ...*[39m +*[90m ... 5 lines matching cause stack trace ...*[39m *[90m at *[39m { [cause]: FoobarError: Individual message at * *[90m at *[39m - *[90m ... 6 lines matching cause stack trace ...*[39m + *[90m ... 5 lines matching cause stack trace ...*[39m *[90m at *[39m { status: *[32m'Feeling good'*[39m, extraProperties: *[32m'Yes!'*[39m, @@ -94,7 +89,6 @@ Error: Stack causes *[90m at *[39m *[90m at *[39m *[90m at *[39m - *[90m at *[39m } } RangeError: New Stack Frames @@ -103,12 +97,12 @@ RangeError: New Stack Frames [cause]: Error: Stack causes at * *[90m at *[39m - *[90m ... 6 lines matching cause stack trace ...*[39m + *[90m ... 5 lines matching cause stack trace ...*[39m *[90m at *[39m { [cause]: FoobarError: Individual message at * *[90m at *[39m - *[90m ... 6 lines matching cause stack trace ...*[39m + *[90m ... 5 lines matching cause stack trace ...*[39m *[90m at *[39m { status: *[32m'Feeling good'*[39m, extraProperties: *[32m'Yes!'*[39m, @@ -121,7 +115,6 @@ RangeError: New Stack Frames *[90m at *[39m *[90m at *[39m *[90m at *[39m - *[90m at *[39m } } } @@ -131,7 +124,7 @@ RangeError: New Stack Frames [cause]: FoobarError: Individual message at * at * - ... 6 lines matching cause stack trace ... + ... 5 lines matching cause stack trace ... at * { status: 'Feeling good', extraProperties: 'Yes!', @@ -144,18 +137,17 @@ RangeError: New Stack Frames at * at * at * - at * } } Error: Stack causes at * at * - ... 6 lines matching cause stack trace ... + ... 5 lines matching cause stack trace ... at * { [cause]: FoobarError: Individual message at * at * - ... 6 lines matching cause stack trace ... + ... 5 lines matching cause stack trace ... at * status: 'Feeling good', extraProperties: 'Yes!', @@ -168,7 +160,6 @@ Error: Stack causes at * at * at * - at * } } RangeError: New Stack Frames @@ -177,12 +168,12 @@ RangeError: New Stack Frames [cause]: Error: Stack causes at * at * - ... 6 lines matching cause stack trace ... + ... 5 lines matching cause stack trace ... at * { [cause]: FoobarError: Individual message at * at * - ... 6 lines matching cause stack trace ... + ... 5 lines matching cause stack trace ... at * { status: 'Feeling good', extraProperties: 'Yes!', @@ -195,7 +186,6 @@ RangeError: New Stack Frames at * at * at * - at * } } } diff --git a/test/message/util_inspect_error.out b/test/message/util_inspect_error.out index 0af1853addeb44..31b65eb2e2bf3c 100644 --- a/test/message/util_inspect_error.out +++ b/test/message/util_inspect_error.out @@ -9,7 +9,6 @@ at * at * at * - at * nested: { err: Error: foo @@ -22,7 +21,6 @@ at * at * at * - at * { err: Error: foo bar @@ -34,7 +32,6 @@ at * at * at * - at * nested: { err: Error: foo bar @@ -46,7 +43,6 @@ at * at * at * - at * } } { Error: foo @@ -59,5 +55,4 @@ bar at * at * at * - at * foo: 'bar' } diff --git a/test/pseudo-tty/console_colors.out b/test/pseudo-tty/console_colors.out index 2aaa21fbb7f045..7d191fa63554b4 100644 --- a/test/pseudo-tty/console_colors.out +++ b/test/pseudo-tty/console_colors.out @@ -13,7 +13,6 @@ foobar [90m at *[39m [90m at *[39m [90m at *[39m -[90m at *[39m Error: Should not ever get here. at Object. [90m(*node_modules*[4m*node_modules*[24m*bar.js:*:*[90m)[39m @@ -23,7 +22,6 @@ Error: Should not ever get here. [90m at *[39m [90m at *[39m [90m at *[39m -[90m at *[39m [90m at *[39m at Object. [90m(*console_colors.js:*:*[90m)[39m [90m at *[39m @@ -33,7 +31,6 @@ Error: Should not ever get here. [90m at *[39m [90m at *[39m [90m at *[39m -[90m at *[39m Error at evalmachine.:*:* @@ -47,4 +44,3 @@ Error [90m at *[39m [90m at *[39m [90m at *[39m -[90m at *[39m diff --git a/test/pseudo-tty/test-fatal-error.out b/test/pseudo-tty/test-fatal-error.out index 7809e049fe0773..9fb8df4251a49b 100644 --- a/test/pseudo-tty/test-fatal-error.out +++ b/test/pseudo-tty/test-fatal-error.out @@ -10,7 +10,6 @@ TypeError: foobar [90m at *(node:internal*loader:*:*)[39m [90m at *[39m [90m at *[39m -[90m at *[39m [90m at *[39m { bla: [33mtrue[39m } diff --git a/test/pseudo-tty/test-start-trace-sigint.out b/test/pseudo-tty/test-start-trace-sigint.out index e5e4911f844080..02cd34e786516b 100644 --- a/test/pseudo-tty/test-start-trace-sigint.out +++ b/test/pseudo-tty/test-start-trace-sigint.out @@ -8,4 +8,3 @@ KEYBOARD_INTERRUPT: Script execution was interrupted by `SIGINT` at * at * at * - at * diff --git a/test/pseudo-tty/test-trace-sigint.out b/test/pseudo-tty/test-trace-sigint.out index 6efae6f612f05d..ae6063b74242e4 100644 --- a/test/pseudo-tty/test-trace-sigint.out +++ b/test/pseudo-tty/test-trace-sigint.out @@ -8,4 +8,3 @@ KEYBOARD_INTERRUPT: Script execution was interrupted by `SIGINT` at * at * at * - at *