From 603e1264fcdf977816be1517b3d81a7413c19211 Mon Sep 17 00:00:00 2001 From: himself65 Date: Wed, 22 Apr 2020 13:28:35 +0800 Subject: [PATCH 1/2] module: hide internal stack when onWarning --- lib/internal/process/warning.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/internal/process/warning.js b/lib/internal/process/warning.js index 383bbd7e0fe79f..5eafb3e65e8a77 100644 --- a/lib/internal/process/warning.js +++ b/lib/internal/process/warning.js @@ -58,6 +58,17 @@ function doEmitWarning(warning) { return () => process.emit('warning', warning); } +function hideInternalStacks(stacks) { + const newStack = []; + for (const stack of stacks.split('\n')) { + if (/\(internal\/.+\)$/.test(stack)) { + continue; + } + newStack.push(stack); + } + return newStack.join('\n'); +} + let traceWarningHelperShown = false; function onWarning(warning) { if (!(warning instanceof Error)) return; @@ -69,7 +80,8 @@ function onWarning(warning) { if (warning.code) msg += `[${warning.code}] `; if (trace && warning.stack) { - msg += `${warning.stack}`; + // hide internal stack + msg += hideInternalStacks(`${warning.stack}`); } else { const toString = typeof warning.toString === 'function' ? From 92cdf773f1cc5dbf5e9ece0eed89c60a6cf2564b Mon Sep 17 00:00:00 2001 From: himself65 Date: Wed, 22 Apr 2020 13:49:47 +0800 Subject: [PATCH 2/2] fixup! --- lib/internal/process/warning.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/internal/process/warning.js b/lib/internal/process/warning.js index 5eafb3e65e8a77..f2879ad8d4234e 100644 --- a/lib/internal/process/warning.js +++ b/lib/internal/process/warning.js @@ -80,8 +80,7 @@ function onWarning(warning) { if (warning.code) msg += `[${warning.code}] `; if (trace && warning.stack) { - // hide internal stack - msg += hideInternalStacks(`${warning.stack}`); + msg += `${warning.stack}`; } else { const toString = typeof warning.toString === 'function' ? @@ -91,12 +90,16 @@ function onWarning(warning) { if (typeof warning.detail === 'string') { msg += `\n${warning.detail}`; } - if (!trace && !traceWarningHelperShown) { - const flag = isDeprecation ? '--trace-deprecation' : '--trace-warnings'; - const argv0 = require('path').basename(process.argv0 || 'node', '.exe'); - msg += `\n(Use \`${argv0} ${flag} ...\` to show where the warning ` + - 'was created)'; - traceWarningHelperShown = true; + if (!traceWarningHelperShown) { + if (!trace) { + const flag = isDeprecation ? '--trace-deprecation' : '--trace-warnings'; + const argv0 = require('path').basename(process.argv0 || 'node', '.exe'); + msg += `\n(Use \`${argv0} ${flag} ...\` to show where the warning ` + + 'was created)'; + traceWarningHelperShown = true; + } else { + msg = hideInternalStacks(msg); + } } const warningFile = lazyOption(); if (warningFile) {