Skip to content

Commit 9ae4dec

Browse files
committed
Mirror entire environment under node
One can still use `-sDETERMINISITIC` to avoid this behaviour. Fixes: #18816
1 parent fbc5d5c commit 9ae4dec

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ See docs/process.md for more on how version tagging works.
2121
3.1.33 (in development)
2222
-----------------------
2323
- Update SDL2_ttf port to 2.20.2 (#18804)
24+
- When running under node the full set of environment variables (i.e.
25+
process.env) are now reflected to running emscripten process (via e.g.
26+
getenv). If you don't want this behaviour `-sDETERMINISTIC` can be used
27+
to disable this can give fake envinonment instead.
2428

2529
3.1.32 - 02/17/23
2630
-----------------

src/library_wasi.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ var WasiLibrary = {
4242
$getEnvStrings: function() {
4343
if (!getEnvStrings.strings) {
4444
// Default values.
45-
#if !DETERMINISTIC
46-
// Browser language detection #8751
47-
var lang = ((typeof navigator == 'object' && navigator.languages && navigator.languages[0]) || 'C').replace('-', '_') + '.UTF-8';
48-
#else
45+
#if DETERMINISTIC
4946
// Deterministic language detection, ignore the browser's language.
5047
var lang = 'C.UTF-8';
48+
#else
49+
// Browser language detection #8751
50+
var lang = ((typeof navigator == 'object' && navigator.languages && navigator.languages[0]) || 'C').replace('-', '_') + '.UTF-8';
5151
#endif
5252
var env = {
5353
'USER': 'web_user',
@@ -58,6 +58,12 @@ var WasiLibrary = {
5858
'LANG': lang,
5959
'_': getExecutableName()
6060
};
61+
#if ENVIRONMENT_MAY_BE_NODE && !DETERMINISTIC
62+
if (ENVIRONMENT_IS_NODE) {
63+
// Under node we mirror then entire outer environment
64+
env = process.env;
65+
}
66+
#endif
6167
// Apply the user-provided values, if any.
6268
for (var x in ENV) {
6369
// x is a key in ENV; if ENV[x] is undefined, that means it was

test/test_other.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7282,21 +7282,33 @@ def test_override_js_execution_environment(self):
72827282
seen = self.run_js('test.js', engine=engine, assert_returncode=NON_ZERO)
72837283
self.assertContained('Module.ENVIRONMENT has been deprecated. To force the environment, use the ENVIRONMENT compile-time option (for example, -sENVIRONMENT=web or -sENVIRONMENT=node', seen)
72847284

7285+
@requires_node
7286+
@with_env_modify({'FOO': 'bar'})
7287+
def test_node_environ(self):
7288+
create_file('src.c', r'''
7289+
#include <stdlib.h>
7290+
#include <stdio.h>
7291+
int main() {
7292+
printf("|%s|\n", getenv("FOO"));
7293+
}
7294+
''')
7295+
self.do_runf('src.c', '|bar|')
7296+
72857297
def test_override_c_environ(self):
72867298
create_file('pre.js', r'''
72877299
var Module = {
72887300
preRun: [function() { ENV.hello = 'world'; ENV.LANG = undefined; }]
72897301
};
72907302
''')
7291-
create_file('src.cpp', r'''
7303+
create_file('src.c', r'''
72927304
#include <stdlib.h>
72937305
#include <stdio.h>
72947306
int main() {
72957307
printf("|%s|\n", getenv("hello"));
72967308
printf("LANG is %s\n", getenv("LANG") ? "set" : "not set");
72977309
}
72987310
''')
7299-
self.run_process([EMXX, 'src.cpp', '--pre-js', 'pre.js'])
7311+
self.run_process([EMCC, 'src.c', '--pre-js', 'pre.js'])
73007312
output = self.run_js('a.out.js')
73017313
self.assertContained('|world|', output)
73027314
self.assertContained('LANG is not set', output)

0 commit comments

Comments
 (0)