Skip to content

Commit 79ef39b

Browse files
Ethan ArrowoodGeoffreyBoothaduh95
authored andcommitted
src: add --disable-warning option
Co-authored-by: Geoffrey Booth <[email protected]> Co-authored-by: Antoine du Hamel <[email protected]> PR-URL: #50661Fixes: #30810Fixes: #47478Fixes: #46862Fixes: #40940 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Richard Lau <[email protected]>
1 parent 3ff2bda commit 79ef39b

File tree

7 files changed

+260
-0
lines changed

7 files changed

+260
-0
lines changed

‎doc/api/cli.md‎

Lines changed: 55 additions & 0 deletions
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+
importsysfrom'node:sys';
471+
```
472+
473+
```cjs
474+
constsys=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+
importsysfrom'node:sys';
485+
importvmfrom'node:vm';
486+
487+
vm.measureMemory();
488+
```
489+
490+
```cjs
491+
constsys=require('node:sys');
492+
constvm=require('node:vm');
493+
494+
vm.measureMemory();
495+
```
496+
446497
### `--disable-proto=mode`
447498

448499
<!-- YAML
@@ -2327,6 +2378,7 @@ Node.js options that are allowed are:
23272378
*`--conditions`, `-C`
23282379
*`--diagnostic-dir`
23292380
*`--disable-proto`
2381+
*`--disable-warning`
23302382
*`--dns-result-order`
23312383
*`--enable-fips`
23322384
*`--enable-network-family-autoselection`
@@ -2779,7 +2831,9 @@ done
27792831
[CommonJS]: modules.md
27802832
[CommonJS module]: modules.md
27812833
[CustomEvent Web API]: https://dom.spec.whatwg.org/#customevent
2834+
[DEP0025 warning]: deprecations.md#dep0025-requirenodesys
27822835
[ECMAScript module]: esm.md#modules-ecmascript-modules
2836+
[ExperimentalWarning: `vm.measureMemory` is an experimental feature]: vm.md#vmmeasurememoryoptions
27832837
[Fetch API]: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
27842838
[File System Permissions]: permissions.md#file-system-permissions
27852839
[Module customization hooks]: module.md#customization-hooks
@@ -2835,6 +2889,7 @@ done
28352889
[context-aware]: addons.md#context-aware-addons
28362890
[debugger]: debugger.md
28372891
[debugging security implications]: https://nodejs.org/en/docs/guides/debugging-getting-started/#security-implications
2892+
[deprecation warnings]: deprecations.md#list-of-deprecated-apis
28382893
[emit_warning]: process.md#processemitwarningwarning-options
28392894
[environment_variables]: #environment-variables
28402895
[filtering tests by name]: test.md#filtering-tests-by-name

‎lib/internal/process/warning.js‎

Lines changed: 18 additions & 0 deletions
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
constassert=require('internal/assert');
1217
const{
1318
codes: {
@@ -89,8 +94,21 @@ function doEmitWarning(warning){
8994
process.emit('warning',warning);
9095
}
9196

97+
letdisableWarningSet;
98+
9299
functiononWarning(warning){
100+
if(!disableWarningSet){
101+
disableWarningSet=newSafeSet();
102+
constdisableWarningValues=getOptionValue('--disable-warning');
103+
for(leti=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(!(warninginstanceofError))return;
111+
94112
constisDeprecation=warning.name==='DeprecationWarning';
95113
if(isDeprecation&&process.noDeprecation)return;
96114
consttrace=process.traceProcessWarnings||

‎src/node_options.cc‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,10 @@ EnvironmentOptionsParser::EnvironmentOptionsParser(){
527527
&EnvironmentOptions::warnings,
528528
kAllowedInEnvvar,
529529
true);
530+
AddOption("--disable-warning",
531+
"silence specific process warnings",
532+
&EnvironmentOptions::disable_warnings,
533+
kAllowedInEnvvar);
530534
AddOption("--force-context-aware",
531535
"disable loading non-context-aware addons",
532536
&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;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
constpath=require('node:path');
3+
const{ Worker }=require('node:worker_threads');
4+
newWorker(path.join(__dirname,'./disable-warning.js'));

‎test/fixtures/disable-warning.js‎

Lines changed: 15 additions & 0 deletions
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+
});
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import{spawnPromisified}from'../common/index.mjs';
2+
import*asfixturesfrom'../common/fixtures.mjs';
3+
import{describe,it}from'node:test';
4+
importassertfrom'node:assert';
5+
6+
constfixturePath=fixtures.path('disable-warning.js');
7+
constfixturePathWorker=fixtures.path('disable-warning-worker.js');
8+
constdep1Message=/\(node:\d+\)\[DEP1\]DeprecationWarning/;
9+
constdep2Message=/\(node:\d+\)\[DEP2\]DeprecationWarning/;
10+
constexperimentalWarningMessage=/\(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 }=awaitspawnPromisified(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 }=awaitspawnPromisified(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 }=awaitspawnPromisified(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 }=awaitspawnPromisified(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 }=awaitspawnPromisified(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 }=awaitspawnPromisified(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 }=awaitspawnPromisified(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 }=awaitspawnPromisified(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 }=awaitspawnPromisified(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 }=awaitspawnPromisified(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)