Skip to content

Commit

Permalink
src: add --disable-warnings option
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Arrowood committed Nov 10, 2023
1 parent 609cd7f commit 37ae57e
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 0 deletions.
46 changes: 46 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,48 @@ Affects the default output directory of:
* [`--heap-prof-dir`][]
* [`--redirect-warnings`][]

### `--disable-warnings=code-or-type`

<!-- YAML
added: REPLACEME
-->

Disable specific process warnings by `code` or `type`.

Warnings emitted from [`process.emitWarning()`][emit_warning] may contain a
`code` and a `type`. This option will not-emit warnings that have a matching
`code` or `type`.

List of [deprecation warnings][].

The Node.js core warning types are: `DeprecationWarning` and
`ExperimentalWarning`

For example, the following script will not emit
[DEP0025 `require('node:sys')`][DEP0025 warning] when executed with
`node --disable-warnings=DEP0025`:

```js
'use strict';

const sys = require('sys'); // eslint-disable-line no-restricted-modules
```

For example, the following script will emit the
[DEP0025 `require('node:sys')`][DEP0025 warning], but not any Experimental
Warnings (such as
[ExperimentalWarning: `vm.measureMemory` is an experimental feature][]
in <=v21) when executed with `node --disable-warnings=ExperimentalWarnings`:

```js
'use strict';

const sys = require('sys'); // eslint-disable-line no-restricted-modules
const vm = require('vm');

vm.measureMemory();
```

### `--disable-proto=mode`

<!-- YAML
Expand Down Expand Up @@ -2327,6 +2369,7 @@ Node.js options that are allowed are:
* `--conditions`, `-C`
* `--diagnostic-dir`
* `--disable-proto`
* `--disable-warnings`
* `--dns-result-order`
* `--enable-fips`
* `--enable-network-family-autoselection`
Expand Down Expand Up @@ -2779,7 +2822,9 @@ done
[CommonJS]: modules.md
[CommonJS module]: modules.md
[CustomEvent Web API]: https://dom.spec.whatwg.org/#customevent
[DEP0025 warning]: deprecations.md#dep0025-requirenodesys
[ECMAScript module]: esm.md#modules-ecmascript-modules
[ExperimentalWarning: `vm.measureMemory` is an experimental feature]: vm.md#vmmeasurememoryoptions
[Fetch API]: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
[File System Permissions]: permissions.md#file-system-permissions
[Module customization hooks]: module.md#customization-hooks
Expand Down Expand Up @@ -2835,6 +2880,7 @@ done
[context-aware]: addons.md#context-aware-addons
[debugger]: debugger.md
[debugging security implications]: https://nodejs.org/en/docs/guides/debugging-getting-started/#security-implications
[deprecation warnings]: deprecations.md#list-of-deprecated-apis
[emit_warning]: process.md#processemitwarningwarning-options
[environment_variables]: #environment-variables
[filtering tests by name]: test.md#filtering-tests-by-name
Expand Down
10 changes: 10 additions & 0 deletions lib/internal/process/warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@

const {
ArrayIsArray,
ArrayPrototypeIncludes,
Error,
ErrorPrototypeToString,
ErrorCaptureStackTrace,
String,
} = primordials;

const {
getOptionValue,
} = require('internal/options');

const assert = require('internal/assert');
const {
codes: {
Expand Down Expand Up @@ -91,6 +96,11 @@ function doEmitWarning(warning) {

function onWarning(warning) {
if (!(warning instanceof Error)) return;

const disableWarnings = getOptionValue('--disable-warnings');
if ((warning.code && ArrayPrototypeIncludes(disableWarnings, warning.code)) ||
(warning.name && ArrayPrototypeIncludes(disableWarnings, warning.name))) return;

const isDeprecation = warning.name === 'DeprecationWarning';
if (isDeprecation && process.noDeprecation) return;
const trace = process.traceProcessWarnings ||
Expand Down
3 changes: 3 additions & 0 deletions src/node_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,9 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
&EnvironmentOptions::warnings,
kAllowedInEnvvar,
true);
AddOption("--disable-warnings",
"silence specific process warnings",
&EnvironmentOptions::disable_warnings);
AddOption("--force-context-aware",
"disable loading non-context-aware addons",
&EnvironmentOptions::force_context_aware,
Expand Down
1 change: 1 addition & 0 deletions src/node_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ class EnvironmentOptions : public Options {
bool allow_native_addons = true;
bool global_search_paths = true;
bool warnings = true;
std::vector<std::string> disable_warnings;
bool force_context_aware = false;
bool pending_deprecation = false;
bool preserve_symlinks = false;
Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/disable-warnings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

const sys = require('sys');
const assert = require('assert');
const vm = require('vm');

try {
assert.fail('a', 'b');
} catch (e) {}

vm.measureMemory();
116 changes: 116 additions & 0 deletions test/parallel/test-process-warnings.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { spawnPromisified } from '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
import { describe, it } from 'node:test';
import { strictEqual, match, doesNotMatch } from 'node:assert';

const fixturePath = fixtures.path('disable-warnings.js');
const dep0025Message = /\(node:\d+\) \[DEP0025\] DeprecationWarning: sys is deprecated\. Use util instead\./;
const dep0094Message = /\(node:\d+\) \[DEP0094\] DeprecationWarning: assert\.fail\(\) with more than one argument is deprecated\. Please use assert\.strictEqual\(\) instead or only pass a message\./;
const experimentalWarningMessage = /\(node:\d+\) ExperimentalWarning: vm\.measureMemory is an experimental feature and might change at any time/;

describe('process warnings', { concurrency: true }, () => {

it('should emit all warnings by default', async () => {
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
fixturePath,
]);

strictEqual(stdout, '');
match(stderr, dep0025Message);
match(stderr, dep0094Message);
match(stderr, experimentalWarningMessage);
strictEqual(code, 0);
strictEqual(signal, null);
});

describe('--no-warnings', { concurrency: true }, () => {
it('should silence all warnings by default', async () => {
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
'--no-warnings',
fixturePath,
]);

strictEqual(stdout, '');
doesNotMatch(stderr, dep0025Message);
doesNotMatch(stderr, dep0094Message);
doesNotMatch(stderr, experimentalWarningMessage);
strictEqual(code, 0);
strictEqual(signal, null);
});
});

describe('--no-deprecation', { concurrency: true }, () => {
it('should silence all deprecation warnings', async () => {
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
'--no-deprecation',
fixturePath,
]);

strictEqual(stdout, '');
doesNotMatch(stderr, dep0025Message);
doesNotMatch(stderr, dep0094Message);
match(stderr, experimentalWarningMessage);
strictEqual(code, 0);
strictEqual(signal, null);
});
});

describe('--disable-warnings', { concurrency: true }, () => {
it('should silence deprecation warning DEP0025', async () => {
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
'--disable-warnings=DEP0025',
fixturePath,
]);

strictEqual(stdout, '');
doesNotMatch(stderr, dep0025Message);
match(stderr, dep0094Message);
match(stderr, experimentalWarningMessage);
strictEqual(code, 0);
strictEqual(signal, null);
});

it('should silence deprecation warnings DEP0025 and DEP0094', async () => {
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
'--disable-warnings=DEP0025',
'--disable-warnings=DEP0094',
fixturePath,
]);

strictEqual(stdout, '');
doesNotMatch(stderr, dep0025Message);
doesNotMatch(stderr, dep0094Message);
match(stderr, experimentalWarningMessage);
strictEqual(code, 0);
strictEqual(signal, null);
});

it('should silence all deprecation warnings using type DeprecationWarning', async () => {
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
'--disable-warnings=DeprecationWarning',
fixturePath,
]);

strictEqual(stdout, '');
doesNotMatch(stderr, dep0025Message);
doesNotMatch(stderr, dep0094Message);
match(stderr, experimentalWarningMessage);
strictEqual(code, 0);
strictEqual(signal, null);
});

it('should silence all experimental warnings using type ExperimentalWarning', async () => {
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
'--disable-warnings=ExperimentalWarning',
fixturePath,
]);

strictEqual(stdout, '');
match(stderr, dep0025Message);
match(stderr, dep0094Message);
doesNotMatch(stderr, experimentalWarningMessage);
strictEqual(code, 0);
strictEqual(signal, null);
});
});
});

0 comments on commit 37ae57e

Please sign in to comment.