Skip to content

Commit 3b590d4

Browse files
addaleaxBridgeAR
authored andcommitted
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 46cafad commit 3b590d4

14 files changed

+26
-4
lines changed

lib/internal/process/warning.js

+8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ function doEmitWarning(warning) {
5757
return () => process.emit('warning', warning);
5858
}
5959

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

test/message/async_error_sync_esm.out

+1
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

+1
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

+1
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

+1
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
^^^^^^^^
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

+1
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

+1
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

+1
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

+5-2
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
});

test/parallel/test-env-var-no-warnings.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ if (process.argv[2] === 'child') {
1717
if (env.NODE_NO_WARNINGS === '1')
1818
assert.strictEqual(stderr, '');
1919
else
20-
assert(/Warning: foo$/.test(stderr.trim()));
20+
assert.match(stderr.trim(), /Warning: foo\n/);
2121
}));
2222
}
2323

Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11

22
(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
3+
(Use `node --trace-warnings ...` to show where the warning was created)
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11

22
(node:*) Warning: The 'NODE_DISABLE_COLORS' and 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
3+
(Use `node --trace-warnings ...` to show where the warning was created)
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
2+
(Use `node --trace-warnings ...` to show where the warning was created)

0 commit comments

Comments
 (0)