Skip to content

Commit 43fabfe

Browse files
TimothyGuMayaLekova
authored andcommitted
process: doc-only deprecate non-string env value
PR-URL: nodejs#18990 Refs: nodejs#15089 Refs: nodejs#18158 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]>
1 parent 91a92f1 commit 43fabfe

File tree

6 files changed

+52
-2
lines changed

6 files changed

+52
-2
lines changed

doc/api/deprecations.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,6 @@ Type: Runtime
915915
916916
This was never a documented feature.
917917
918-
919918
<a id="DEP0101"></a>
920919
### DEP0101: --with-lttng
921920
@@ -940,6 +939,17 @@ Type: Documentation-only (supports [`--pending-deprecation`][])
940939
Using `process.binding()` in general should be avoided. The type checking
941940
methods in particular can be replaced by using [`util.types`][].
942941
942+
<a id="DEP0104"></a>
943+
### DEP0104: process.env string coercion
944+
945+
Type: Documentation-only (supports [`--pending-deprecation`][])
946+
947+
Currently when assigning a property to [`process.env`][], the assigned value is
948+
implicitly converted to a string if it is not a string. This behavior is
949+
deprecated if the assigned value is not a string, boolean, or number. In the
950+
future, such assignment may result in a thrown error. Please convert the
951+
property to a string before assigning it to `process.env`.
952+
943953
[`--pending-deprecation`]: cli.html#cli_pending_deprecation
944954
[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size
945955
[`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array
@@ -976,6 +986,7 @@ methods in particular can be replaced by using [`util.types`][].
976986
[`fs.stat()`]: fs.html#fs_fs_stat_path_callback
977987
[`os.networkInterfaces`]: os.html#os_os_networkinterfaces
978988
[`os.tmpdir()`]: os.html#os_os_tmpdir
989+
[`process.env`]: process.html#process_process_env
979990
[`punycode`]: punycode.html
980991
[`require.extensions`]: modules.html#modules_require_extensions
981992
[`setInterval()`]: timers.html#timers_setinterval_callback_delay_args

doc/api/process.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,10 @@ emitMyWarning();
836836
## process.env
837837
<!-- YAML
838838
added: v0.1.27
839+
changes:
840+
- version: REPLACEME
841+
pr-url: https://github.com/nodejs/node/pull/18990
842+
description: Implicit conversion of variable value to string is deprecated.
839843
-->
840844

841845
* {Object}
@@ -877,7 +881,8 @@ console.log(process.env.foo);
877881
```
878882

879883
Assigning a property on `process.env` will implicitly convert the value
880-
to a string.
884+
to a string. **This behavior is deprecated.** Future versions of Node.js may
885+
throw an error when the value is not a string, number, or boolean.
881886

882887
Example:
883888

src/env-inl.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ inline Environment::Environment(IsolateData* isolate_data,
330330
trace_sync_io_(false),
331331
abort_on_uncaught_exception_(false),
332332
emit_napi_warning_(true),
333+
emit_env_nonstring_warning_(true),
333334
makecallback_cntr_(0),
334335
should_abort_on_uncaught_toggle_(isolate_, 1),
335336
#if HAVE_INSPECTOR

src/env.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,11 @@ class Environment {
725725
void AddPromiseHook(promise_hook_func fn, void* arg);
726726
bool RemovePromiseHook(promise_hook_func fn, void* arg);
727727
bool EmitNapiWarning();
728+
inline bool EmitProcessEnvWarning() {
729+
bool current_value = emit_env_nonstring_warning_;
730+
emit_env_nonstring_warning_ = false;
731+
return current_value;
732+
}
728733

729734
typedef void (*native_immediate_callback)(Environment* env, void* data);
730735
// cb will be called as cb(env, data) on the next event loop iteration.
@@ -779,6 +784,7 @@ class Environment {
779784
bool trace_sync_io_;
780785
bool abort_on_uncaught_exception_;
781786
bool emit_napi_warning_;
787+
bool emit_env_nonstring_warning_;
782788
size_t makecallback_cntr_;
783789
std::vector<double> destroy_async_id_list_;
784790

src/node.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2659,6 +2659,17 @@ static void EnvGetter(Local<Name> property,
26592659
static void EnvSetter(Local<Name> property,
26602660
Local<Value> value,
26612661
const PropertyCallbackInfo<Value>& info) {
2662+
Environment* env = Environment::GetCurrent(info);
2663+
if (config_pending_deprecation && env->EmitProcessEnvWarning() &&
2664+
!value->IsString() && !value->IsNumber() && !value->IsBoolean()) {
2665+
if (ProcessEmitDeprecationWarning(
2666+
env,
2667+
"Assigning any value other than a string, number, or boolean to a "
2668+
"process.env property is deprecated. Please make sure to convert the "
2669+
"value to a string before setting process.env with it.",
2670+
"DEP00XX").IsNothing())
2671+
return;
2672+
}
26622673
#ifdef __POSIX__
26632674
node::Utf8Value key(info.GetIsolate(), property);
26642675
node::Utf8Value val(info.GetIsolate(), value);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
5+
// Flags: --pending-deprecation
6+
7+
common.expectWarning(
8+
'DeprecationWarning',
9+
'Assigning any value other than a string, number, or boolean to a ' +
10+
'process.env property is deprecated. Please make sure to convert the value ' +
11+
'to a string before setting process.env with it.',
12+
'DEP00XX'
13+
);
14+
15+
process.env.ABC = undefined;
16+
assert.strictEqual(process.env.ABC, 'undefined');

0 commit comments

Comments
 (0)