Skip to content

Commit 988542c

Browse files
Ethan ArrowoodGeoffreyBoothaduh95
authored andcommitted
src: add --disable-warning option
Co-authored-by: Geoffrey Booth <webadmin@geoffreybooth.com> Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> PR-URL: #50661 Fixes: #30810 Fixes: #47478 Fixes: #46862 Fixes: #40940 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Richard Lau <rlau@redhat.com>
1 parent 2e0c11b commit 988542c

File tree

7 files changed

+260
-0
lines changed

7 files changed

+260
-0
lines changed

doc/api/cli.md

+55
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,57 @@ Affects the default output directory of:
443443
* [`--heap-prof-dir`][]
444444
* [`--redirect-warnings`][]
445445

446+
### `--disable-warning=code-or-type`
447+
448+
> Stability: 1.1 - Active development
449+
450+
<!-- YAML
451+
added: REPLACEME
452+
-->
453+
454+
Disable specific process warnings by `code` or `type`.
455+
456+
Warnings emitted from [`process.emitWarning()`][emit_warning] may contain a
457+
`code` and a `type`. This option will not-emit warnings that have a matching
458+
`code` or `type`.
459+
460+
List of [deprecation warnings][].
461+
462+
The Node.js core warning types are: `DeprecationWarning` and
463+
`ExperimentalWarning`
464+
465+
For example, the following script will not emit
466+
[DEP0025 `require('node:sys')`][DEP0025 warning] when executed with
467+
`node --disable-warning=DEP0025`:
468+
469+
```mjs
470+
import sys from 'node:sys';
471+
```
472+
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-warning=ExperimentalWarnings`:
482+
483+
```mjs
484+
import sys from 'node:sys';
485+
import vm from 'node:vm';
486+
487+
vm.measureMemory();
488+
```
489+
490+
```cjs
491+
const sys = require('node:sys');
492+
const vm = require('node:vm');
493+
494+
vm.measureMemory();
495+
```
496+
446497
### `--disable-proto=mode`
447498

448499
<!-- YAML
@@ -2289,6 +2340,7 @@ Node.js options that are allowed are:
22892340
* `--conditions`, `-C`
22902341
* `--diagnostic-dir`
22912342
* `--disable-proto`
2343+
* `--disable-warning`
22922344
* `--dns-result-order`
22932345
* `--enable-fips`
22942346
* `--enable-network-family-autoselection`
@@ -2740,7 +2792,9 @@ done
27402792
[CommonJS]: modules.md
27412793
[CommonJS module]: modules.md
27422794
[CustomEvent Web API]: https://dom.spec.whatwg.org/#customevent
2795+
[DEP0025 warning]: deprecations.md#dep0025-requirenodesys
27432796
[ECMAScript module]: esm.md#modules-ecmascript-modules
2797+
[ExperimentalWarning: `vm.measureMemory` is an experimental feature]: vm.md#vmmeasurememoryoptions
27442798
[Fetch API]: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
27452799
[File System Permissions]: permissions.md#file-system-permissions
27462800
[Module customization hooks]: module.md#customization-hooks
@@ -2795,6 +2849,7 @@ done
27952849
[context-aware]: addons.md#context-aware-addons
27962850
[debugger]: debugger.md
27972851
[debugging security implications]: https://nodejs.org/en/docs/guides/debugging-getting-started/#security-implications
2852+
[deprecation warnings]: deprecations.md#list-of-deprecated-apis
27982853
[emit_warning]: process.md#processemitwarningwarning-options
27992854
[environment_variables]: #environment-variables
28002855
[filtering tests by name]: test.md#filtering-tests-by-name

lib/internal/process/warning.js

+18
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ const {
66
ErrorPrototypeToString,
77
ErrorCaptureStackTrace,
88
String,
9+
SafeSet,
910
} = primordials;
1011

12+
const {
13+
getOptionValue,
14+
} = require('internal/options');
15+
1116
const assert = require('internal/assert');
1217
const {
1318
codes: {
@@ -89,8 +94,21 @@ function doEmitWarning(warning) {
8994
process.emit('warning', warning);
9095
}
9196

97+
let disableWarningSet;
98+
9299
function onWarning(warning) {
100+
if (!disableWarningSet) {
101+
disableWarningSet = new SafeSet();
102+
const disableWarningValues = getOptionValue('--disable-warning');
103+
for (let i = 0; i < disableWarningValues.length; i++) {
104+
disableWarningSet.add(disableWarningValues[i]);
105+
}
106+
}
107+
if ((warning?.code && disableWarningSet.has(warning.code)) ||
108+
(warning?.name && disableWarningSet.has(warning.name))) return;
109+
93110
if (!(warning instanceof Error)) return;
111+
94112
const isDeprecation = warning.name === 'DeprecationWarning';
95113
if (isDeprecation && process.noDeprecation) return;
96114
const trace = process.traceProcessWarnings ||

src/node_options.cc

+4
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser() {
522522
&EnvironmentOptions::warnings,
523523
kAllowedInEnvvar,
524524
true);
525+
AddOption("--disable-warning",
526+
"silence specific process warnings",
527+
&EnvironmentOptions::disable_warnings,
528+
kAllowedInEnvvar);
525529
AddOption("--force-context-aware",
526530
"disable loading non-context-aware addons",
527531
&EnvironmentOptions::force_context_aware,

src/node_options.h

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ class EnvironmentOptions : public Options {
138138
bool allow_native_addons = true;
139139
bool global_search_paths = true;
140140
bool warnings = true;
141+
std::vector<std::string> disable_warnings;
141142
bool force_context_aware = false;
142143
bool pending_deprecation = false;
143144
bool preserve_symlinks = false;
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
const path = require('node:path');
3+
const { Worker } = require('node:worker_threads');
4+
new Worker(path.join(__dirname, './disable-warning.js'));

test/fixtures/disable-warning.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
process.emitWarning('Deprecation Warning 1', {
4+
code: 'DEP1',
5+
type: 'DeprecationWarning'
6+
});
7+
8+
process.emitWarning('Deprecation Warning 2', {
9+
code: 'DEP2',
10+
type: 'DeprecationWarning'
11+
});
12+
13+
process.emitWarning('Experimental Warning', {
14+
type: 'ExperimentalWarning'
15+
});
+163
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import { spawnPromisified } from '../common/index.mjs';
2+
import * as fixtures from '../common/fixtures.mjs';
3+
import { describe, it } from 'node:test';
4+
import assert from 'node:assert';
5+
6+
const fixturePath = fixtures.path('disable-warning.js');
7+
const fixturePathWorker = fixtures.path('disable-warning-worker.js');
8+
const dep1Message = /\(node:\d+\) \[DEP1\] DeprecationWarning/;
9+
const dep2Message = /\(node:\d+\) \[DEP2\] DeprecationWarning/;
10+
const experimentalWarningMessage = /\(node:\d+\) ExperimentalWarning/;
11+
12+
describe('process warnings', { concurrency: true }, () => {
13+
14+
it('should emit all warnings by default', async () => {
15+
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
16+
fixturePath,
17+
]);
18+
19+
assert.strictEqual(stdout, '');
20+
assert.match(stderr, dep1Message);
21+
assert.match(stderr, dep2Message);
22+
assert.match(stderr, experimentalWarningMessage);
23+
assert.strictEqual(code, 0);
24+
assert.strictEqual(signal, null);
25+
});
26+
27+
describe('--no-warnings', { concurrency: true }, () => {
28+
it('should silence all warnings by default', async () => {
29+
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
30+
'--no-warnings',
31+
fixturePath,
32+
]);
33+
34+
assert.strictEqual(stdout, '');
35+
assert.doesNotMatch(stderr, dep1Message);
36+
assert.doesNotMatch(stderr, dep2Message);
37+
assert.doesNotMatch(stderr, experimentalWarningMessage);
38+
assert.strictEqual(code, 0);
39+
assert.strictEqual(signal, null);
40+
});
41+
});
42+
43+
describe('--no-deprecation', { concurrency: true }, () => {
44+
it('should silence all deprecation warnings', async () => {
45+
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
46+
'--no-deprecation',
47+
fixturePath,
48+
]);
49+
50+
assert.strictEqual(stdout, '');
51+
assert.doesNotMatch(stderr, dep1Message);
52+
assert.doesNotMatch(stderr, dep2Message);
53+
assert.match(stderr, experimentalWarningMessage);
54+
assert.strictEqual(code, 0);
55+
assert.strictEqual(signal, null);
56+
});
57+
});
58+
59+
describe('--disable-warning', { concurrency: true }, () => {
60+
it('should silence deprecation warning DEP1', async () => {
61+
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
62+
'--disable-warning=DEP1',
63+
fixturePath,
64+
]);
65+
66+
assert.strictEqual(stdout, '');
67+
assert.doesNotMatch(stderr, dep1Message);
68+
assert.match(stderr, dep2Message);
69+
assert.match(stderr, experimentalWarningMessage);
70+
assert.strictEqual(code, 0);
71+
assert.strictEqual(signal, null);
72+
});
73+
74+
it('should silence deprecation warnings DEP1 and DEP2', async () => {
75+
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
76+
'--disable-warning=DEP1',
77+
'--disable-warning=DEP2',
78+
fixturePath,
79+
]);
80+
81+
assert.strictEqual(stdout, '');
82+
assert.doesNotMatch(stderr, dep1Message);
83+
assert.doesNotMatch(stderr, dep2Message);
84+
assert.match(stderr, experimentalWarningMessage);
85+
assert.strictEqual(code, 0);
86+
assert.strictEqual(signal, null);
87+
});
88+
89+
it('should silence all deprecation warnings using type DeprecationWarning', async () => {
90+
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
91+
'--disable-warning=DeprecationWarning',
92+
fixturePath,
93+
]);
94+
95+
assert.strictEqual(stdout, '');
96+
assert.doesNotMatch(stderr, dep1Message);
97+
assert.doesNotMatch(stderr, dep2Message);
98+
assert.match(stderr, experimentalWarningMessage);
99+
assert.strictEqual(code, 0);
100+
assert.strictEqual(signal, null);
101+
});
102+
103+
it('should silence all experimental warnings using type ExperimentalWarning', async () => {
104+
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
105+
'--disable-warning=ExperimentalWarning',
106+
fixturePath,
107+
]);
108+
109+
assert.strictEqual(stdout, '');
110+
assert.match(stderr, dep1Message);
111+
assert.match(stderr, dep2Message);
112+
assert.doesNotMatch(stderr, experimentalWarningMessage);
113+
assert.strictEqual(code, 0);
114+
assert.strictEqual(signal, null);
115+
});
116+
117+
it('should pass down option to worker', async () => {
118+
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
119+
'--disable-warning=DEP2',
120+
fixturePathWorker,
121+
]);
122+
123+
assert.strictEqual(stdout, '');
124+
assert.match(stderr, dep1Message);
125+
assert.doesNotMatch(stderr, dep2Message);
126+
assert.match(stderr, experimentalWarningMessage);
127+
assert.strictEqual(code, 0);
128+
assert.strictEqual(signal, null);
129+
});
130+
131+
it('should not support a comma separated list', async () => {
132+
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
133+
'--disable-warning=DEP1,DEP2',
134+
fixturePathWorker,
135+
]);
136+
137+
assert.strictEqual(stdout, '');
138+
assert.match(stderr, dep1Message);
139+
assert.match(stderr, dep2Message);
140+
assert.match(stderr, experimentalWarningMessage);
141+
assert.strictEqual(code, 0);
142+
assert.strictEqual(signal, null);
143+
});
144+
145+
it('should be specifiable in NODE_OPTIONS', async () => {
146+
const { stdout, stderr, code, signal } = await spawnPromisified(process.execPath, [
147+
fixturePath,
148+
], {
149+
env: {
150+
...process.env,
151+
NODE_OPTIONS: '--disable-warning=DEP2'
152+
}
153+
});
154+
155+
assert.strictEqual(stdout, '');
156+
assert.match(stderr, dep1Message);
157+
assert.doesNotMatch(stderr, dep2Message);
158+
assert.match(stderr, experimentalWarningMessage);
159+
assert.strictEqual(code, 0);
160+
assert.strictEqual(signal, null);
161+
});
162+
});
163+
});

0 commit comments

Comments
 (0)