Skip to content

Commit

Permalink
src: delete proccess.env values set to undefined
Browse files Browse the repository at this point in the history
Before this commit, setting a property of process.env to undefined
would set the value to the string "undefined". This changes the
behavior to instead remove the value, working like the delete operator.

Refs: #15089
  • Loading branch information
targos committed Jan 23, 2018
1 parent 9870b53 commit f88476a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
16 changes: 13 additions & 3 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,11 @@ emitMyWarning();
## process.env
<!-- YAML
added: v0.1.27
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18158
description: Assigning a property to `undefined` now deletes it from the
environment.
-->

* {Object}
Expand Down Expand Up @@ -885,12 +890,13 @@ Example:
process.env.test = null;
console.log(process.env.test);
// => 'null'
process.env.test = undefined;
process.env.test = 42;
console.log(process.env.test);
// => 'undefined'
// => '42'
```

Use `delete` to delete a property from `process.env`.
Use `delete` to delete a property from `process.env` or assign it to
`undefined`.

Example:

Expand All @@ -899,6 +905,10 @@ process.env.TEST = 1;
delete process.env.TEST;
console.log(process.env.TEST);
// => undefined
process.env.TEST = 1;
process.env.TEST = undefined;
console.log('TEST' in process.env);
// => false
```

On Windows operating systems, environment variables are case-insensitive.
Expand Down
26 changes: 18 additions & 8 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2697,6 +2697,18 @@ static void ProcessTitleSetter(Local<Name> property,
}


inline void RemovePropertyFromEnv(Isolate* isolate, Local<Name> property) {
#ifdef __POSIX__
node::Utf8Value key(isolate, property);
unsetenv(*key);
#else
node::TwoByteValue key(isolate, property);
WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
SetEnvironmentVariableW(key_ptr, nullptr);
#endif
}


static void EnvGetter(Local<Name> property,
const PropertyCallbackInfo<Value>& info) {
Isolate* isolate = info.GetIsolate();
Expand Down Expand Up @@ -2731,6 +2743,11 @@ static void EnvGetter(Local<Name> property,
static void EnvSetter(Local<Name> property,
Local<Value> value,
const PropertyCallbackInfo<Value>& info) {
if (value->IsUndefined()) {
RemovePropertyFromEnv(info.GetIsolate(), property);
info.GetReturnValue().Set(value);
return;
}
#ifdef __POSIX__
node::Utf8Value key(info.GetIsolate(), property);
node::Utf8Value val(info.GetIsolate(), value);
Expand Down Expand Up @@ -2780,14 +2797,7 @@ static void EnvQuery(Local<Name> property,
static void EnvDeleter(Local<Name> property,
const PropertyCallbackInfo<Boolean>& info) {
if (property->IsString()) {
#ifdef __POSIX__
node::Utf8Value key(info.GetIsolate(), property);
unsetenv(*key);
#else
node::TwoByteValue key(info.GetIsolate(), property);
WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
SetEnvironmentVariableW(key_ptr, nullptr);
#endif
RemovePropertyFromEnv(info.GetIsolate(), property);
}

// process.env never has non-configurable properties, so always
Expand Down
9 changes: 9 additions & 0 deletions test/parallel/test-process-env.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,12 @@ if (common.isWindows) {
assert.strictEqual(process.env.test, undefined);
assert.strictEqual(process.env.teST, undefined);
}

{
// Setting an environment variable to undefined should act like a delete.
process.env.UNDEF = 'test';
assert.strictEqual(process.env.UNDEF, 'test');
process.env.UNDEF = undefined;
assert.strictEqual(process.env.UNDEF, undefined);
assert.strictEqual('UNDEF' in process.env, false);
}

0 comments on commit f88476a

Please sign in to comment.