Skip to content

Commit 78efd64

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

File tree

7 files changed

+217
-0
lines changed

7 files changed

+217
-0
lines changed

doc/api/cli.md

+57
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

+10
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: {
@@ -90,7 +95,12 @@ function doEmitWarning(warning) {
9095
}
9196

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

src/node_options.cc

+3
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

+1
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;
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const path = require('node:path');
2+
const { Worker } = require('node:worker_threads');
3+
new Worker(path.join(__dirname, './disable-warnings.js'));

test/fixtures/disable-warnings.js

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

0 commit comments

Comments
 (0)