Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: add option to silence warnings #36137

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -848,6 +848,13 @@ Enables report to be generated on uncaught exceptions. Useful when inspecting
the JavaScript stack in conjunction with native stack and other runtime
environment data.

### `--suppress-warnings`
<!-- YAML
added: REPLACEME
-->

Silence deprecation and experimental warnings for specified codes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the difference between this and the existing --no-warnings flag?

Copy link
Contributor Author

@PoojaDurgad PoojaDurgad Dec 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasnell - I have introduced the new flag --suppress-warnings and also introduced codes for experimental warnings which enables this flag to silence the experimental and deprecation warnings for supplied codes. The idea is to support silencing warnings for experimental and deprecation warnings and enabled the flag to take multiple codes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and what I'm saying is that the --no-warnings flag can just be extended to support this without introducing a new flag. --no-warnings (with no arguments) would continue to work as it does today, while --no-warnings={list of codes} would suppress the named codes. In either case, the documentation here should indicate that the flag takes a list of codes.

Copy link
Contributor Author

@PoojaDurgad PoojaDurgad Jan 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasnell - well instead of introducing new cli option we can extend the existing --no-warnings option right . yeah I understood and thanks for the review.


### `--throw-deprecation`
<!-- YAML
added: v0.11.14
Expand Down Expand Up @@ -1358,6 +1365,7 @@ Node.js options that are allowed are:
* `--report-signal`
* `--report-uncaught-exception`
* `--require`, `-r`
* `--suppress-warnings`
* `--throw-deprecation`
* `--title`
* `--tls-cipher-list`
Expand Down
3 changes: 3 additions & 0 deletions doc/node.1
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,9 @@ Enables
to be generated on un-caught exceptions. Useful when inspecting JavaScript
stack in conjunction with native stack and other runtime environment data.
.
.It Fl -suppress-warnings
Silence warnings for specified codes.
.
.It Fl -throw-deprecation
Throw errors for deprecations.
.
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ translators.set('builtin', async function builtinStrategy(url) {

// Strategy for loading a JSON file
translators.set('json', async function jsonStrategy(url) {
emitExperimentalWarning('Importing JSON modules');
emitExperimentalWarning('Importing JSON modules', 'EXP0003');
debug(`Translating JSONModule ${url}`);
debug(`Loading JSONModule ${url}`);
const pathname = StringPrototypeStartsWith(url, 'file:') ?
Expand Down Expand Up @@ -351,7 +351,7 @@ translators.set('json', async function jsonStrategy(url) {

// Strategy for loading a wasm module
translators.set('wasm', async function(url) {
emitExperimentalWarning('Importing Web Assembly modules');
emitExperimentalWarning('Importing Web Assembly modules', 'EXP0007');
let { source } = await this._getSource(
url, { format: 'wasm' }, defaultGetSource);
assertBufferSource(source, false, 'getSource');
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/process/esm_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async function initializeLoader() {
// If --experimental-loader is specified, create a loader with user hooks.
// Otherwise create the default loader.
const { emitExperimentalWarning } = require('internal/util');
emitExperimentalWarning('--experimental-loader');
emitExperimentalWarning('--experimental-loader', 'EXP0002');
return (async () => {
const hooks =
await ESMLoader.import(userLoader, pathToFileURL(cwd).href);
Expand Down
8 changes: 8 additions & 0 deletions lib/internal/process/warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ function emitWarning(warning, type, code, ctor) {
if (warning.name === 'DeprecationWarning') {
if (process.noDeprecation)
return;
const options = require('internal/options');
const opts = options.getOptionValue('--suppress-warnings');
var list = opts.split(',');
let found = false;
list.forEach((e) => {
if (e === code) found = true;
});
if (found) return;
if (process.throwDeprecation) {
// Delay throwing the error to guarantee that all former warnings were
// properly logged.
Expand Down
15 changes: 12 additions & 3 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,20 @@ function slowCases(enc) {
}
}

function emitExperimentalWarning(feature) {
function emitExperimentalWarning(feature, code) {
if (experimentalWarnings.has(feature)) return;
const msg = `${feature} is an experimental feature. This feature could ` +
'change at any time';
experimentalWarnings.add(feature);
const { getOptionValue } = require('internal/options');
const opts = getOptionValue('--suppress-warnings');
const list = opts.split(',');
let found = false;
list.forEach((e) => {
if (e === code) found = true;
});
if (found) return;

const msg = `[${code}] ${feature} is an experimental feature. ` +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the right way to include the code. The emitWarning api already has an option for including the code correctly. Also, I would add this check into the emitWarning processing so it can be applied generically to all warning types.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why this comment was marked resolved as the issue is still there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without knowing, it was marked as resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jasnell - I have a doubt , can you confirm one thing please , the experimental warning vm.measureMemory API calls emitExperimentalWarning like this emitExperimentalWarning('vm.measureMemory');

node/lib/vm.js

Line 390 in 22293ea

emitExperimentalWarning('vm.measureMemory');
so the function takes a string and it matches with the function emitExperimentalWarning(feature) like this
function emitExperimentalWarning(feature) {
so if I'm not wrong this call does not include an option for codes. If the above file modification is not the right way to include the code, then how to do that? can you suggest me.

'This feature could change at any time';
process.emitWarning(msg, 'ExperimentalWarning');
}

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/vm/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const kLink = Symbol('kLink');

class Module {
constructor(options) {
emitExperimentalWarning('VM Modules');
emitExperimentalWarning('VM Modules', 'EXP0005');

if (new.target === Module) {
// eslint-disable-next-line no-restricted-syntax
Expand Down
2 changes: 1 addition & 1 deletion lib/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ const measureMemoryExecutions = {
};

function measureMemory(options = {}) {
emitExperimentalWarning('vm.measureMemory');
emitExperimentalWarning('vm.measureMemory', 'EXP0004');
validateObject(options, 'options');
const { mode = 'summary', execution = 'default' } = options;
validateOneOf(mode, 'options.mode', ['summary', 'detailed']);
Expand Down
2 changes: 1 addition & 1 deletion lib/wasi.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const kSetMemory = Symbol('kSetMemory');
const kStarted = Symbol('kStarted');
const kInstance = Symbol('kInstance');

emitExperimentalWarning('WASI');
emitExperimentalWarning('WASI', 'EXP0006');


function setupInstance(self, instance) {
Expand Down
4 changes: 4 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
AddOption("--prof-process",
"process V8 profiler output generated using --prof",
&EnvironmentOptions::prof_process);
AddOption("--suppress-warnings",
"silence warnings for specified codes",
&EnvironmentOptions::suppress_warnings,
kAllowedInEnvironment);
// Options after --prof-process are passed through to the prof processor.
AddAlias("--prof-process", { "--prof-process", "--" });
#if HAVE_INSPECTOR
Expand Down
1 change: 1 addition & 0 deletions src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class EnvironmentOptions : public Options {
bool preserve_symlinks = false;
bool preserve_symlinks_main = false;
bool prof_process = false;
std::string suppress_warnings;
#if HAVE_INSPECTOR
std::string cpu_prof_dir;
static const uint64_t kDefaultCpuProfInterval = 1000;
Expand Down
4 changes: 2 additions & 2 deletions test/common/measure-memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ function assertSingleDetailedShape(result) {

function expectExperimentalWarning() {
common.expectWarning('ExperimentalWarning',
'vm.measureMemory is an experimental feature. ' +
'This feature could change at any time');
'[EXP0004] vm.measureMemory is an experimental ' +
'feature. This feature could change at any time');
}

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions test/es-module/test-esm-json.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ child.on('close', (code, signal) => {
strictEqual(code, 0);
strictEqual(signal, null);
ok(stderr.toString().includes(
'ExperimentalWarning: Importing JSON modules is an experimental feature. ' +
'This feature could change at any time'
'ExperimentalWarning: [EXP0003] Importing JSON modules is an ' +
'experimental feature. This feature could change at any time'
));
});
4 changes: 2 additions & 2 deletions test/es-module/test-esm-wasm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ child.on('close', (code, signal) => {
strictEqual(code, 0);
strictEqual(signal, null);
ok(stderr.toString().includes(
'ExperimentalWarning: Importing Web Assembly modules is ' +
'an experimental feature. This feature could change at any time'
'ExperimentalWarning: [EXP0007] Importing Web Assembly modules ' +
'is an experimental feature. This feature could change at any time'
));
});
2 changes: 1 addition & 1 deletion test/message/esm_loader_not_found.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(node:*) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
(node:*) ExperimentalWarning: [EXP0002] --experimental-loader is an experimental feature. This feature could change at any time
(Use `* --trace-warnings ...` to show where the warning was created)
node:internal/process/esm_loader:*
internalBinding('errors').triggerUncaughtException(
Expand Down
2 changes: 1 addition & 1 deletion test/message/esm_loader_not_found_cjs_hint_relative.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(node:*) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
(node:*) ExperimentalWarning: [EXP0002] --experimental-loader is an experimental feature. This feature could change at any time
(Use `* --trace-warnings ...` to show where the warning was created)
node:internal/process/esm_loader:*
internalBinding('errors').triggerUncaughtException(
Expand Down
2 changes: 1 addition & 1 deletion test/message/esm_loader_syntax_error.out
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(node:*) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
(node:*) ExperimentalWarning: [EXP0002] --experimental-loader is an experimental feature. This feature could change at any time
(Use `* --trace-warnings ...` to show where the warning was created)
file://*/test/fixtures/es-module-loaders/syntax-error.mjs:2
await async () => 0;
Expand Down
47 changes: 47 additions & 0 deletions test/parallel/test-suppress-warnings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';
require('../common');
const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const cp = require('child_process');
const fs = require('fs');
const path = require('path');
if (process.argv[2] === 'single-dep-skip') {
new Buffer(10);
} else if (process.argv[2] === 'multi-dep-skip') {
new Buffer(10);
tmpdir.refresh();
const filePath = path.join(tmpdir.path, 'rmdir-recursive.txt');
fs.rmdir(filePath, { recursive: true }, () => {});
} else if (process.argv[2] === 'single-exp-skip') {
const vm = require('vm');
vm.measureMemory();
} else if (process.argv[2] === 'multi-exp-skip') {
const vm = require('vm');
vm.measureMemory();
new AbortController();
} else {
const child_single_dep_skip = cp.spawnSync(process.execPath,
['--suppress-warnings=DEP0005',
__filename, 'single-dep-skip']);
assert.strictEqual(child_single_dep_skip.stdout.toString(), '');
assert.strictEqual(child_single_dep_skip.stderr.toString(), '');
const child_multi_dep_skip = cp.spawnSync(process.execPath,
['--suppress-warnings=' +
'DEP0005,DEP0147',
__filename,
'multi-dep-skip']);
assert.strictEqual(child_multi_dep_skip.stdout.toString(), '');
assert.strictEqual(child_multi_dep_skip.stderr.toString(), '');
const child_single_exp_skip = cp.spawnSync(process.execPath,
['--suppress-warnings=EXP0004',
__filename, 'single-exp-skip']);
assert.strictEqual(child_single_exp_skip.stdout.toString(), '');
assert.strictEqual(child_single_exp_skip.stderr.toString(), '');
const child_multi_exp_skip = cp.spawnSync(process.execPath,
['--suppress-warnings=' +
'EXP0004,EXP0001',
__filename,
'multi-exp-skip']);
assert.strictEqual(child_multi_exp_skip.stdout.toString(), '');
assert.strictEqual(child_multi_exp_skip.stderr.toString(), '');
}
4 changes: 2 additions & 2 deletions test/wasi/test-wasi-symlinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const path = require('path');

if (process.argv[2] === 'wasi-child') {
common.expectWarning('ExperimentalWarning',
'WASI is an experimental feature. This feature could ' +
'change at any time');
'[EXP0006] WASI is an experimental feature. This ' +
'feature could change at any time');

const { WASI } = require('wasi');
const wasmDir = path.join(__dirname, 'wasm');
Expand Down
5 changes: 3 additions & 2 deletions test/wasi/test-wasi.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';

const common = require('../common');

if (process.argv[2] === 'wasi-child') {
Expand All @@ -8,8 +9,8 @@ if (process.argv[2] === 'wasi-child') {
const path = require('path');

common.expectWarning('ExperimentalWarning',
'WASI is an experimental feature. This feature could ' +
'change at any time');
'[EXP0006] WASI is an experimental feature. This ' +
'feature could change at any time');

const { WASI } = require('wasi');
tmpdir.refresh();
Expand Down