Skip to content

Commit a16b570

Browse files
committed
src: add --pending-deprecation and NODE_PENDING_DEPRECATION
Command line flag and environment variable that can be used to indicate that pending deprecations should be emitted. PR-URL: #11968 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent d3dedb7 commit a16b570

File tree

6 files changed

+100
-0
lines changed

6 files changed

+100
-0
lines changed

doc/api/cli.md

+28
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,20 @@ added: v0.11.14
137137

138138
Throw errors for deprecations.
139139

140+
### `--pending-deprecation`
141+
<!-- YAML
142+
added: REPLACEME
143+
-->
144+
145+
Emit pending deprecation warnings.
146+
147+
*Note*: Pending deprecations are generally identical to a runtime deprecation
148+
with the notable exception that they are turned *off* by default and will not
149+
be emitted unless either the `--pending-deprecation` command line flag, or the
150+
`NODE_PENDING_DEPRECATION=1` environment variable, is set. Pending deprecations
151+
are used to provide a kind of selective "early warning" mechanism that
152+
developers may leverage to detect deprecated API usage.
153+
140154
### `--no-warnings`
141155
<!-- YAML
142156
added: v6.0.0
@@ -383,6 +397,20 @@ added: v7.5.0
383397

384398
When set to `1`, process warnings are silenced.
385399

400+
### `NODE_PENDING_DEPRECATION=1`
401+
<!-- YAML
402+
added: REPLACEME
403+
-->
404+
405+
When set to `1`, emit pending deprecation warnings.
406+
407+
*Note*: Pending deprecations are generally identical to a runtime deprecation
408+
with the notable exception that they are turned *off* by default and will not
409+
be emitted unless either the `--pending-deprecation` command line flag, or the
410+
`NODE_PENDING_DEPRECATION=1` environment variable, is set. Pending deprecations
411+
are used to provide a kind of selective "early warning" mechanism that
412+
developers may leverage to detect deprecated API usage.
413+
386414
### `NODE_PRESERVE_SYMLINKS=1`
387415
<!-- YAML
388416
added: v7.1.0

doc/node.1

+8
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ Print stack traces for deprecations.
115115
.BR \-\-throw\-deprecation
116116
Throw errors for deprecations.
117117

118+
.TP
119+
.BR \-\-pending\-deprecation
120+
Emit pending deprecation warnings.
121+
118122
.TP
119123
.BR \-\-no\-warnings
120124
Silence all process warnings (including deprecations).
@@ -259,6 +263,10 @@ When set to \fI1\fR, process warnings are silenced.
259263
.BR NODE_PATH =\fIpath\fR[:\fI...\fR]
260264
\':\'\-separated list of directories prefixed to the module search path.
261265

266+
.TP
267+
.BR NODE_PENDING_DEPRECATION = \fI1\fR
268+
When set to \fI1\fR, emit pending deprecation warnings.
269+
262270
.TP
263271
.BR NODE_REPL_HISTORY =\fIfile\fR
264272
Path to the file used to store the persistent REPL history. The default path

src/node.cc

+12
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ bool trace_warnings = false;
209209
// that is used by lib/module.js
210210
bool config_preserve_symlinks = false;
211211

212+
// Set by ParseArgs when --pending-deprecation or NODE_PENDING_DEPRECATION
213+
// is used.
214+
bool config_pending_deprecation = false;
215+
212216
// Set in node.cc by ParseArgs when --redirect-warnings= is used.
213217
std::string config_warning_file; // NOLINT(runtime/string)
214218

@@ -3768,6 +3772,8 @@ static void ParseArgs(int* argc,
37683772
short_circuit = true;
37693773
} else if (strcmp(arg, "--zero-fill-buffers") == 0) {
37703774
zero_fill_all_buffers = true;
3775+
} else if (strcmp(arg, "--pending-deprecation") == 0) {
3776+
config_pending_deprecation = true;
37713777
} else if (strcmp(arg, "--v8-options") == 0) {
37723778
new_v8_argv[new_v8_argc] = "--help";
37733779
new_v8_argc += 1;
@@ -4187,6 +4193,12 @@ void Init(int* argc,
41874193
V8::SetFlagsFromString(NODE_V8_OPTIONS, sizeof(NODE_V8_OPTIONS) - 1);
41884194
#endif
41894195

4196+
{
4197+
std::string text;
4198+
config_pending_deprecation =
4199+
SafeGetenv("NODE_PENDING_DEPRECATION", &text) && text[0] == '1';
4200+
}
4201+
41904202
// Allow for environment set preserving symlinks.
41914203
{
41924204
std::string text;

src/node_config.cc

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ static void InitConfig(Local<Object> target,
4949
if (config_preserve_symlinks)
5050
READONLY_BOOLEAN_PROPERTY("preserveSymlinks");
5151

52+
if (config_pending_deprecation)
53+
READONLY_BOOLEAN_PROPERTY("pendingDeprecation");
54+
5255
if (!config_warning_file.empty()) {
5356
Local<String> name = OneByteString(env->isolate(), "warningFile");
5457
Local<String> value = String::NewFromUtf8(env->isolate(),

src/node_internals.h

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ extern bool config_expose_internals;
7676
// it to stderr.
7777
extern std::string config_warning_file; // NOLINT(runtime/string)
7878

79+
// Set in node.cc by ParseArgs when --pending-deprecation or
80+
// NODE_PENDING_DEPRECATION is used
81+
extern bool config_pending_deprecation;
82+
7983
// Tells whether it is safe to call v8::Isolate::GetCurrent().
8084
extern bool v8_initialized;
8185

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
// Tests that --pending-deprecation and NODE_PENDING_DEPRECATION both
4+
// set the process.binding('config').pendingDeprecation flag that is
5+
// used to determine if pending deprecation messages should be shown.
6+
// The test is performed by launching two child processes that run
7+
// this same test script with different arguments. If those exit with
8+
// code 0, then the test passes. If they don't, it fails.
9+
10+
const common = require('../common');
11+
12+
const assert = require('assert');
13+
const config = process.binding('config');
14+
const fork = require('child_process').fork;
15+
16+
function message(name) {
17+
return `${name} did not set the process.binding('config').` +
18+
'pendingDeprecation flag.';
19+
}
20+
21+
switch (process.argv[2]) {
22+
case 'env':
23+
case 'switch':
24+
assert.strictEqual(config.pendingDeprecation, true);
25+
break;
26+
default:
27+
// Verify that the flag is off by default.
28+
assert.strictEqual(config.pendingDeprecation, undefined);
29+
30+
// Test the --pending-deprecation command line switch.
31+
fork(__filename, ['switch'], {
32+
execArgv: ['--pending-deprecation'],
33+
silent: true
34+
}).on('exit', common.mustCall((code) => {
35+
assert.strictEqual(code, 0, message('--pending-deprecation'));
36+
}));
37+
38+
// Test the NODE_PENDING_DEPRECATION environment var.
39+
fork(__filename, ['env'], {
40+
env: {NODE_PENDING_DEPRECATION: 1},
41+
silent: true
42+
}).on('exit', common.mustCall((code) => {
43+
assert.strictEqual(code, 0, message('NODE_PENDING_DEPRECATION'));
44+
}));
45+
}

0 commit comments

Comments
 (0)