Skip to content

Commit 28f61ad

Browse files
author
Ethan Arrowood
committed
src: add --disable-warnings option
1 parent 609cd7f commit 28f61ad

File tree

6 files changed

+198
-0
lines changed

6 files changed

+198
-0
lines changed

doc/api/cli.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,59 @@ Affects the default output directory of:
443443
* [`--heap-prof-dir`][]
444444
* [`--redirect-warnings`][]
445445

446+
### `--disable-warnings=code-or-type`
447+
448+
<!-- YAML
449+
added: REPLACEME
450+
-->
451+
452+
Disable specific process warnings by `code` or `type`.
453+
454+
Warnings emitted from [`process.emitWarning()`][emit_warning] may contain a
455+
`code` and a `type`. This option will not-emit warnings that have a matching
456+
`code` or `type`.
457+
458+
List of [deprecation warnings][].
459+
460+
The Node.js core warning types are: `DeprecationWarning` and
461+
`ExperimentalWarning`
462+
463+
For example, the following script will not emit
464+
[DEP0025 `require('node:sys')`][DEP0025 warning] when executed with
465+
`node --disable-warnings=DEP0025`:
466+
467+
<!-- eslint-skip -->
468+
```mjs
469+
import sys from 'node:sys';
470+
```
471+
472+
<!-- eslint-skip -->
473+
```cjs
474+
const sys = require('node:sys');
475+
```
476+
477+
For example, the following script will emit the
478+
[DEP0025 `require('node:sys')`][DEP0025 warning], but not any Experimental
479+
Warnings (such as
480+
[ExperimentalWarning: `vm.measureMemory` is an experimental feature][]
481+
in <=v21) when executed with `node --disable-warnings=ExperimentalWarnings`:
482+
483+
<!-- eslint-skip -->
484+
```mjs
485+
import sys from 'node:sys';
486+
import vm from 'node:vm';
487+
488+
vm.measureMemory();
489+
```
490+
491+
<!-- eslint-skip -->
492+
```cjs
493+
const sys = require('node:sys');
494+
const vm = require('node:vm');
495+
496+
vm.measureMemory();
497+
```
498+
446499
### `--disable-proto=mode`
447500

448501
<!-- YAML
@@ -2327,6 +2380,7 @@ Node.js options that are allowed are:
23272380
* `--conditions`, `-C`
23282381
* `--diagnostic-dir`
23292382
* `--disable-proto`
2383+
* `--disable-warnings`
23302384
* `--dns-result-order`
23312385
* `--enable-fips`
23322386
* `--enable-network-family-autoselection`
@@ -2779,7 +2833,9 @@ done
27792833
[CommonJS]: modules.md
27802834
[CommonJS module]: modules.md
27812835
[CustomEvent Web API]: https://dom.spec.whatwg.org/#customevent
2836+
[DEP0025 warning]: deprecations.md#dep0025-requirenodesys
27822837
[ECMAScript module]: esm.md#modules-ecmascript-modules
2838+
[ExperimentalWarning: `vm.measureMemory` is an experimental feature]: vm.md#vmmeasurememoryoptions
27832839
[Fetch API]: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
27842840
[File System Permissions]: permissions.md#file-system-permissions
27852841
[Module customization hooks]: module.md#customization-hooks
@@ -2835,6 +2891,7 @@ done
28352891
[context-aware]: addons.md#context-aware-addons
28362892
[debugger]: debugger.md
28372893
[debugging security implications]: https://nodejs.org/en/docs/guides/debugging-getting-started/#security-implications
2894+
[deprecation warnings]: deprecations.md#list-of-deprecated-apis
28382895
[emit_warning]: process.md#processemitwarningwarning-options
28392896
[environment_variables]: #environment-variables
28402897
[filtering tests by name]: test.md#filtering-tests-by-name

lib/internal/process/warning.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22

33
const {
44
ArrayIsArray,
5+
ArrayPrototypeIncludes,
56
Error,
67
ErrorPrototypeToString,
78
ErrorCaptureStackTrace,
89
String,
910
} = primordials;
1011

12+
const {
13+
getOptionValue,
14+
} = require('internal/options');
15+
1116
const assert = require('internal/assert');
1217
const {
1318
codes: {
@@ -91,6 +96,11 @@ function doEmitWarning(warning) {
9196

9297
function onWarning(warning) {
9398
if (!(warning instanceof Error)) return;
99+
100+
const disableWarnings = getOptionValue('--disable-warnings');
101+
if ((warning.code && ArrayPrototypeIncludes(disableWarnings, warning.code)) ||
102+
(warning.name && ArrayPrototypeIncludes(disableWarnings, warning.name))) return;
103+
94104
const isDeprecation = warning.name === 'DeprecationWarning';
95105
if (isDeprecation && process.noDeprecation) return;
96106
const trace = process.traceProcessWarnings ||

src/node_options.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,9 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
527527
&EnvironmentOptions::warnings,
528528
kAllowedInEnvvar,
529529
true);
530+
AddOption("--disable-warnings",
531+
"silence specific process warnings",
532+
&EnvironmentOptions::disable_warnings);
530533
AddOption("--force-context-aware",
531534
"disable loading non-context-aware addons",
532535
&EnvironmentOptions::force_context_aware,

src/node_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ class EnvironmentOptions : public Options {
139139
bool allow_native_addons = true;
140140
bool global_search_paths = true;
141141
bool warnings = true;
142+
std::vector<std::string> disable_warnings;
142143
bool force_context_aware = false;
143144
bool pending_deprecation = false;
144145
bool preserve_symlinks = false;

test/fixtures/disable-warnings.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
const sys = require('sys');
4+
const assert = require('assert');
5+
const vm = require('vm');
6+
7+
try {
8+
assert.fail('a', 'b');
9+
} catch (e) {}
10+
11+
vm.measureMemory();
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { spawnPromisified } from '../common/index.mjs';
2+
import * as fixtures from '../common/fixtures.mjs';
3+
import { describe, it } from 'node:test';
4+
import { strictEqual, match, doesNotMatch } from 'node:assert';
5+
6+
const fixturePath = fixtures.path('disable-warnings.js');
7+
const dep0025Message = /\(node:\d+\) \[DEP0025\] DeprecationWarning: sys is deprecated\. Use util instead\./;
8+
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\./;
9+
const experimentalWarningMessage = /\(node:\d+\) ExperimentalWarning: vm\.measureMemory is an experimental feature and might change at any time/;
10+
11+
describe('process warnings', { concurrency: true }, () => {
12+
13+
it('should emit all warnings by default', async () => {
14+
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
15+
fixturePath,
16+
]);
17+
18+
strictEqual(stdout, '');
19+
match(stderr, dep0025Message);
20+
match(stderr, dep0094Message);
21+
match(stderr, experimentalWarningMessage);
22+
strictEqual(code, 0);
23+
strictEqual(signal, null);
24+
});
25+
26+
describe('--no-warnings', { concurrency: true }, () => {
27+
it('should silence all warnings by default', async () => {
28+
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
29+
'--no-warnings',
30+
fixturePath,
31+
]);
32+
33+
strictEqual(stdout, '');
34+
doesNotMatch(stderr, dep0025Message);
35+
doesNotMatch(stderr, dep0094Message);
36+
doesNotMatch(stderr, experimentalWarningMessage);
37+
strictEqual(code, 0);
38+
strictEqual(signal, null);
39+
});
40+
});
41+
42+
describe('--no-deprecation', { concurrency: true }, () => {
43+
it('should silence all deprecation warnings', async () => {
44+
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
45+
'--no-deprecation',
46+
fixturePath,
47+
]);
48+
49+
strictEqual(stdout, '');
50+
doesNotMatch(stderr, dep0025Message);
51+
doesNotMatch(stderr, dep0094Message);
52+
match(stderr, experimentalWarningMessage);
53+
strictEqual(code, 0);
54+
strictEqual(signal, null);
55+
});
56+
});
57+
58+
describe('--disable-warnings', { concurrency: true }, () => {
59+
it('should silence deprecation warning DEP0025', async () => {
60+
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
61+
'--disable-warnings=DEP0025',
62+
fixturePath,
63+
]);
64+
65+
strictEqual(stdout, '');
66+
doesNotMatch(stderr, dep0025Message);
67+
match(stderr, dep0094Message);
68+
match(stderr, experimentalWarningMessage);
69+
strictEqual(code, 0);
70+
strictEqual(signal, null);
71+
});
72+
73+
it('should silence deprecation warnings DEP0025 and DEP0094', async () => {
74+
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
75+
'--disable-warnings=DEP0025',
76+
'--disable-warnings=DEP0094',
77+
fixturePath,
78+
]);
79+
80+
strictEqual(stdout, '');
81+
doesNotMatch(stderr, dep0025Message);
82+
doesNotMatch(stderr, dep0094Message);
83+
match(stderr, experimentalWarningMessage);
84+
strictEqual(code, 0);
85+
strictEqual(signal, null);
86+
});
87+
88+
it('should silence all deprecation warnings using type DeprecationWarning', async () => {
89+
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
90+
'--disable-warnings=DeprecationWarning',
91+
fixturePath,
92+
]);
93+
94+
strictEqual(stdout, '');
95+
doesNotMatch(stderr, dep0025Message);
96+
doesNotMatch(stderr, dep0094Message);
97+
match(stderr, experimentalWarningMessage);
98+
strictEqual(code, 0);
99+
strictEqual(signal, null);
100+
});
101+
102+
it('should silence all experimental warnings using type ExperimentalWarning', async () => {
103+
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
104+
'--disable-warnings=ExperimentalWarning',
105+
fixturePath,
106+
]);
107+
108+
strictEqual(stdout, '');
109+
match(stderr, dep0025Message);
110+
match(stderr, dep0094Message);
111+
doesNotMatch(stderr, experimentalWarningMessage);
112+
strictEqual(code, 0);
113+
strictEqual(signal, null);
114+
});
115+
});
116+
});

0 commit comments

Comments
 (0)