src: add `--disable-warning` option · nodejs/node@79ef39b · GitHub
Skip to content

Commit 79ef39b

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 3ff2bda commit 79ef39b

7 files changed

Lines changed: 260 additions & 0 deletions

File tree

doc/api/cli.md

Lines changed: 55 additions & 0 deletions

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

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

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

0 commit comments

Comments
 (0)