Skip to content

Commit 907ebdd

Browse files
committed
process: suggest --trace-warnings when printing warning
Suggest using `--trace-warnings` or `--trace-deprecation` the first time a warning is emitted without a stack trace, similar to how we suggest `--trace-uncaught` when printing uncaught exceptions without a stack trace. PR-URL: #32797 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: David Carlier <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 9c25ab1 commit 907ebdd

14 files changed

+26
-4
lines changed

lib/internal/process/warning.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ function doEmitWarning(warning) {
5858
return () => process.emit('warning', warning);
5959
}
6060

61+
let traceWarningHelperShown = false;
6162
function onWarning(warning) {
6263
if (!(warning instanceof Error)) return;
6364
const isDeprecation = warning.name === 'DeprecationWarning';
@@ -78,6 +79,13 @@ function onWarning(warning) {
7879
if (typeof warning.detail === 'string') {
7980
msg += `\n${warning.detail}`;
8081
}
82+
if (!trace && !traceWarningHelperShown) {
83+
const flag = isDeprecation ? '--trace-deprecation' : '--trace-warnings';
84+
const argv0 = require('path').basename(process.argv0 || 'node', '.exe');
85+
msg += `\n(Use \`${argv0} ${flag} ...\` to show where the warning ` +
86+
'was created)';
87+
traceWarningHelperShown = true;
88+
}
8189
const warningFile = lazyOption();
8290
if (warningFile) {
8391
return writeToFile(msg);

test/message/async_error_sync_esm.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
(node:*) ExperimentalWarning: The ESM module loader is experimental.
2+
(Use `node --trace-warnings ...` to show where the warning was created)
23
Error: test
34
at one (*fixtures*async-error.js:4:9)
45
at two (*fixtures*async-error.js:17:9)

test/message/esm_display_syntax_error.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
(node:*) ExperimentalWarning: The ESM module loader is experimental.
2+
(Use `node --trace-warnings ...` to show where the warning was created)
23
file:///*/test/message/esm_display_syntax_error.mjs:2
34
await async () => 0;
45
^^^^^

test/message/esm_display_syntax_error_import.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
(node:*) ExperimentalWarning: The ESM module loader is experimental.
2+
(Use `node --trace-warnings ...` to show where the warning was created)
23
file:///*/test/message/esm_display_syntax_error_import.mjs:5
34
notfound
45
^^^^^^^^

test/message/esm_display_syntax_error_import_module.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
(node:*) ExperimentalWarning: The ESM module loader is experimental.
2+
(Use `node --trace-warnings ...` to show where the warning was created)
23
file:///*/test/fixtures/es-module-loaders/syntax-error-import.mjs:1
34
import { foo, notfound } from './module-named-exports.mjs';
45
^^^^^^^^
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
(node:*) ExperimentalWarning: The ESM module loader is experimental.
2+
(Use `node --trace-warnings ...` to show where the warning was created)
23
file:///*/test/fixtures/es-module-loaders/syntax-error.mjs:2
34
await async () => 0;
45
^^^^^
56

67
SyntaxError: Unexpected reserved word
7-
at Loader.moduleStrategy (internal/modules/esm/translators.js:*:*)
8+
at Loader.moduleStrategy (internal/modules/esm/translators.js:*:*)

test/message/esm_loader_not_found.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
(node:*) ExperimentalWarning: The ESM module loader is experimental.
2+
(Use `node --trace-warnings ...` to show where the warning was created)
23
(node:*) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
34
internal/modules/run_main.js:*
45
internalBinding('errors').triggerUncaughtException(

test/message/esm_loader_syntax_error.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
(node:*) ExperimentalWarning: The ESM module loader is experimental.
2+
(Use `node --trace-warnings ...` to show where the warning was created)
23
(node:*) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
34
file://*/test/fixtures/es-module-loaders/syntax-error.mjs:2
45
await async () => 0;

test/message/v8_warning.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
(node:*) V8: *v8_warning.js:* Invalid asm.js: Invalid return type
2+
(Use `node --trace-warnings ...` to show where the warning was created)

test/parallel/test-debugger-pid.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ interfacer.on('line', function(line) {
2929
case 1:
3030
expected =
3131
new RegExp(`^\\(node:${pid}\\) \\[DEP0068\\] DeprecationWarning: `);
32-
assert.ok(expected.test(line), `expected regexp match for ${line}`);
32+
assert.match(line, expected);
3333
break;
3434
case 2:
35+
assert.match(line, /Use `node --trace-deprecation \.\.\.` to show where /);
36+
break;
37+
case 3:
3538
// Doesn't currently work on Windows.
3639
if (!common.isWindows) {
3740
expected = "Target process: 655555 doesn't exist.";
@@ -48,6 +51,6 @@ interfacer.on('line', function(line) {
4851
interfacer.on('exit', function(code, signal) {
4952
assert.strictEqual(code, 1, `Got unexpected code: ${code}`);
5053
if (!common.isWindows) {
51-
assert.strictEqual(lineCount, 2);
54+
assert.strictEqual(lineCount, 3);
5255
}
5356
});

0 commit comments

Comments
 (0)