Skip to content

Commit

Permalink
process: doc-only deprecate non-string env value
Browse files Browse the repository at this point in the history
PR-URL: #18990
Refs: #15089
Refs: #18158
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
  • Loading branch information
TimothyGu committed Mar 7, 2018
1 parent c9b4de5 commit 5826fe4
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 2 deletions.
13 changes: 12 additions & 1 deletion doc/api/deprecations.md
Expand Up @@ -915,7 +915,6 @@ Type: Runtime
This was never a documented feature. This was never a documented feature.
<a id="DEP0101"></a> <a id="DEP0101"></a>
### DEP0101: --with-lttng ### DEP0101: --with-lttng
Expand All @@ -940,6 +939,17 @@ Type: Documentation-only (supports [`--pending-deprecation`][])
Using `process.binding()` in general should be avoided. The type checking Using `process.binding()` in general should be avoided. The type checking
methods in particular can be replaced by using [`util.types`][]. methods in particular can be replaced by using [`util.types`][].
<a id="DEP0104"></a>
### DEP0104: process.env string coercion
Type: Documentation-only (supports [`--pending-deprecation`][])
Currently when assigning a property to [`process.env`][], the assigned value is
implicitly converted to a string if it is not a string. This behavior is
deprecated if the assigned value is not a string, boolean, or number. In the
future, such assignment may result in a thrown error. Please convert the
property to a string before assigning it to `process.env`.
[`--pending-deprecation`]: cli.html#cli_pending_deprecation [`--pending-deprecation`]: cli.html#cli_pending_deprecation
[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size [`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size
[`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array [`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array
Expand Down Expand Up @@ -976,6 +986,7 @@ methods in particular can be replaced by using [`util.types`][].
[`fs.stat()`]: fs.html#fs_fs_stat_path_callback [`fs.stat()`]: fs.html#fs_fs_stat_path_callback
[`os.networkInterfaces`]: os.html#os_os_networkinterfaces [`os.networkInterfaces`]: os.html#os_os_networkinterfaces
[`os.tmpdir()`]: os.html#os_os_tmpdir [`os.tmpdir()`]: os.html#os_os_tmpdir
[`process.env`]: process.html#process_process_env
[`punycode`]: punycode.html [`punycode`]: punycode.html
[`require.extensions`]: modules.html#modules_require_extensions [`require.extensions`]: modules.html#modules_require_extensions
[`setInterval()`]: timers.html#timers_setinterval_callback_delay_args [`setInterval()`]: timers.html#timers_setinterval_callback_delay_args
Expand Down
7 changes: 6 additions & 1 deletion doc/api/process.md
Expand Up @@ -836,6 +836,10 @@ emitMyWarning();
## process.env ## process.env
<!-- YAML <!-- YAML
added: v0.1.27 added: v0.1.27
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18990
description: Implicit conversion of variable value to string is deprecated.
--> -->


* {Object} * {Object}
Expand Down Expand Up @@ -877,7 +881,8 @@ console.log(process.env.foo);
``` ```


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


Example: Example:


Expand Down
1 change: 1 addition & 0 deletions src/env-inl.h
Expand Up @@ -330,6 +330,7 @@ inline Environment::Environment(IsolateData* isolate_data,
trace_sync_io_(false), trace_sync_io_(false),
abort_on_uncaught_exception_(false), abort_on_uncaught_exception_(false),
emit_napi_warning_(true), emit_napi_warning_(true),
emit_env_nonstring_warning_(true),
makecallback_cntr_(0), makecallback_cntr_(0),
should_abort_on_uncaught_toggle_(isolate_, 1), should_abort_on_uncaught_toggle_(isolate_, 1),
#if HAVE_INSPECTOR #if HAVE_INSPECTOR
Expand Down
6 changes: 6 additions & 0 deletions src/env.h
Expand Up @@ -725,6 +725,11 @@ class Environment {
void AddPromiseHook(promise_hook_func fn, void* arg); void AddPromiseHook(promise_hook_func fn, void* arg);
bool RemovePromiseHook(promise_hook_func fn, void* arg); bool RemovePromiseHook(promise_hook_func fn, void* arg);
bool EmitNapiWarning(); bool EmitNapiWarning();
inline bool EmitProcessEnvWarning() {
bool current_value = emit_env_nonstring_warning_;
emit_env_nonstring_warning_ = false;
return current_value;
}


typedef void (*native_immediate_callback)(Environment* env, void* data); typedef void (*native_immediate_callback)(Environment* env, void* data);
// cb will be called as cb(env, data) on the next event loop iteration. // cb will be called as cb(env, data) on the next event loop iteration.
Expand Down Expand Up @@ -779,6 +784,7 @@ class Environment {
bool trace_sync_io_; bool trace_sync_io_;
bool abort_on_uncaught_exception_; bool abort_on_uncaught_exception_;
bool emit_napi_warning_; bool emit_napi_warning_;
bool emit_env_nonstring_warning_;
size_t makecallback_cntr_; size_t makecallback_cntr_;
std::vector<double> destroy_async_id_list_; std::vector<double> destroy_async_id_list_;


Expand Down
11 changes: 11 additions & 0 deletions src/node.cc
Expand Up @@ -2659,6 +2659,17 @@ static void EnvGetter(Local<Name> property,
static void EnvSetter(Local<Name> property, static void EnvSetter(Local<Name> property,
Local<Value> value, Local<Value> value,
const PropertyCallbackInfo<Value>& info) { const PropertyCallbackInfo<Value>& info) {
Environment* env = Environment::GetCurrent(info);
if (config_pending_deprecation && env->EmitProcessEnvWarning() &&
!value->IsString() && !value->IsNumber() && !value->IsBoolean()) {
if (ProcessEmitDeprecationWarning(
env,
"Assigning any value other than a string, number, or boolean to a "
"process.env property is deprecated. Please make sure to convert the "
"value to a string before setting process.env with it.",
"DEP00XX").IsNothing())
return;
}
#ifdef __POSIX__ #ifdef __POSIX__
node::Utf8Value key(info.GetIsolate(), property); node::Utf8Value key(info.GetIsolate(), property);
node::Utf8Value val(info.GetIsolate(), value); node::Utf8Value val(info.GetIsolate(), value);
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-process-env-deprecation.js
@@ -0,0 +1,16 @@
'use strict';
const common = require('../common');
const assert = require('assert');

// Flags: --pending-deprecation

common.expectWarning(
'DeprecationWarning',
'Assigning any value other than a string, number, or boolean to a ' +
'process.env property is deprecated. Please make sure to convert the value ' +
'to a string before setting process.env with it.',
'DEP00XX'
);

process.env.ABC = undefined;
assert.strictEqual(process.env.ABC, 'undefined');

0 comments on commit 5826fe4

Please sign in to comment.