diff --git a/src/node.cc b/src/node.cc index 8ab9e4d1f45f51..be82939e00b708 100644 --- a/src/node.cc +++ b/src/node.cc @@ -2799,6 +2799,7 @@ static void EnvGetter(Local property, #else // _WIN32 String::Value key(property); WCHAR buffer[32767]; // The maximum size allowed for environment variables. + SetLastError(ERROR_SUCCESS); DWORD result = GetEnvironmentVariableW(reinterpret_cast(*key), buffer, arraysize(buffer)); @@ -2846,6 +2847,7 @@ static void EnvQuery(Local property, #else // _WIN32 String::Value key(property); WCHAR* key_ptr = reinterpret_cast(*key); + SetLastError(ERROR_SUCCESS); if (GetEnvironmentVariableW(key_ptr, nullptr, 0) > 0 || GetLastError() == ERROR_SUCCESS) { rc = 0; diff --git a/test/parallel/test-process-env-windows-error-reset.js b/test/parallel/test-process-env-windows-error-reset.js new file mode 100644 index 00000000000000..59e5f287d82346 --- /dev/null +++ b/test/parallel/test-process-env-windows-error-reset.js @@ -0,0 +1,22 @@ +'use strict'; +require('../common'); +const assert = require('assert'); + +// This checks that after accessing a missing env var, a subsequent +// env read will succeed even for empty variables. + +{ + process.env.FOO = ''; + process.env.NONEXISTENT_ENV_VAR; + const foo = process.env.FOO; + + assert.strictEqual(foo, ''); +} + +{ + process.env.FOO = ''; + process.env.NONEXISTENT_ENV_VAR; + const hasFoo = 'FOO' in process.env; + + assert.strictEqual(hasFoo, true); +}