Skip to content

Commit

Permalink
lib: add option to silence warnings
Browse files Browse the repository at this point in the history
Introduce an option `--suppress-warnings`
to silence experimental and deprecation
warnings for specified features

Fixes: nodejs#30810

Co-Authored-By: Cody Deckard <[email protected]>
  • Loading branch information
PoojaDurgad and codeman869 committed Dec 23, 2020
1 parent e22b751 commit d9a04a9
Show file tree
Hide file tree
Showing 20 changed files with 103 additions and 22 deletions.
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.

### `--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. ` +
'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

0 comments on commit d9a04a9

Please sign in to comment.