Skip to content

Commit

Permalink
process: suggest --trace-warnings when printing warning
Browse files Browse the repository at this point in the history
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 <riclau@uk.ibm.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: David Carlier <devnexen@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
addaleax committed Apr 15, 2020
1 parent 9c25ab1 commit 907ebdd
Show file tree
Hide file tree
Showing 14 changed files with 26 additions and 4 deletions.
8 changes: 8 additions & 0 deletions lib/internal/process/warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function doEmitWarning(warning) {
return () => process.emit('warning', warning);
}

let traceWarningHelperShown = false;
function onWarning(warning) {
if (!(warning instanceof Error)) return;
const isDeprecation = warning.name === 'DeprecationWarning';
Expand All @@ -78,6 +79,13 @@ 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;
}
const warningFile = lazyOption();
if (warningFile) {
return writeToFile(msg);
Expand Down
1 change: 1 addition & 0 deletions test/message/async_error_sync_esm.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(node:*) ExperimentalWarning: The ESM module loader is experimental.
(Use `node --trace-warnings ...` to show where the warning was created)
Error: test
at one (*fixtures*async-error.js:4:9)
at two (*fixtures*async-error.js:17:9)
Expand Down
1 change: 1 addition & 0 deletions test/message/esm_display_syntax_error.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(node:*) ExperimentalWarning: The ESM module loader is experimental.
(Use `node --trace-warnings ...` to show where the warning was created)
file:///*/test/message/esm_display_syntax_error.mjs:2
await async () => 0;
^^^^^
Expand Down
1 change: 1 addition & 0 deletions test/message/esm_display_syntax_error_import.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(node:*) ExperimentalWarning: The ESM module loader is experimental.
(Use `node --trace-warnings ...` to show where the warning was created)
file:///*/test/message/esm_display_syntax_error_import.mjs:5
notfound
^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions test/message/esm_display_syntax_error_import_module.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(node:*) ExperimentalWarning: The ESM module loader is experimental.
(Use `node --trace-warnings ...` to show where the warning was created)
file:///*/test/fixtures/es-module-loaders/syntax-error-import.mjs:1
import { foo, notfound } from './module-named-exports.mjs';
^^^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion test/message/esm_display_syntax_error_module.out
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(node:*) ExperimentalWarning: The ESM module loader is experimental.
(Use `node --trace-warnings ...` to show where the warning was created)
file:///*/test/fixtures/es-module-loaders/syntax-error.mjs:2
await async () => 0;
^^^^^

SyntaxError: Unexpected reserved word
at Loader.moduleStrategy (internal/modules/esm/translators.js:*:*)
at Loader.moduleStrategy (internal/modules/esm/translators.js:*:*)
1 change: 1 addition & 0 deletions test/message/esm_loader_not_found.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(node:*) ExperimentalWarning: The ESM module loader is experimental.
(Use `node --trace-warnings ...` to show where the warning was created)
(node:*) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
internal/modules/run_main.js:*
internalBinding('errors').triggerUncaughtException(
Expand Down
1 change: 1 addition & 0 deletions test/message/esm_loader_syntax_error.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(node:*) ExperimentalWarning: The ESM module loader is experimental.
(Use `node --trace-warnings ...` to show where the warning was created)
(node:*) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
file://*/test/fixtures/es-module-loaders/syntax-error.mjs:2
await async () => 0;
Expand Down
1 change: 1 addition & 0 deletions test/message/v8_warning.out
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
(node:*) V8: *v8_warning.js:* Invalid asm.js: Invalid return type
(Use `node --trace-warnings ...` to show where the warning was created)
7 changes: 5 additions & 2 deletions test/parallel/test-debugger-pid.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ interfacer.on('line', function(line) {
case 1:
expected =
new RegExp(`^\\(node:${pid}\\) \\[DEP0068\\] DeprecationWarning: `);
assert.ok(expected.test(line), `expected regexp match for ${line}`);
assert.match(line, expected);
break;
case 2:
assert.match(line, /Use `node --trace-deprecation \.\.\.` to show where /);
break;
case 3:
// Doesn't currently work on Windows.
if (!common.isWindows) {
expected = "Target process: 655555 doesn't exist.";
Expand All @@ -48,6 +51,6 @@ interfacer.on('line', function(line) {
interfacer.on('exit', function(code, signal) {
assert.strictEqual(code, 1, `Got unexpected code: ${code}`);
if (!common.isWindows) {
assert.strictEqual(lineCount, 2);
assert.strictEqual(lineCount, 3);
}
});
2 changes: 1 addition & 1 deletion test/parallel/test-env-var-no-warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if (process.argv[2] === 'child') {
if (env.NODE_NO_WARNINGS === '1')
assert.strictEqual(stderr, '');
else
assert(/Warning: foo$/.test(stderr.trim()));
assert.match(stderr.trim(), /Warning: foo\n/);
}));
}

Expand Down
1 change: 1 addition & 0 deletions test/pseudo-tty/test-tty-color-support-warning-2.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
(Use `node --trace-warnings ...` to show where the warning was created)
1 change: 1 addition & 0 deletions test/pseudo-tty/test-tty-color-support-warning.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

(node:*) Warning: The 'NODE_DISABLE_COLORS' and 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
(Use `node --trace-warnings ...` to show where the warning was created)
1 change: 1 addition & 0 deletions test/pseudo-tty/test-tty-color-support.out
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
(Use `node --trace-warnings ...` to show where the warning was created)

0 comments on commit 907ebdd

Please sign in to comment.