Skip to content

Commit b4c9194

Browse files
authored
Unset emsdk-related environment variable from inactive tools (#801)
When we deactivate a tool we also want to remove its environment variables. One driver for this is that modern sdks don't set `EM_CACHE` whereas old ones did and we want to make sure that `EM_CACHE` gets unset when folks upgrade (and then re-set if they downgrade). See #797.
1 parent 3510ab2 commit b4c9194

File tree

2 files changed

+60
-30
lines changed

2 files changed

+60
-30
lines changed

emsdk.py

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2711,43 +2711,59 @@ def construct_env(tools_to_activate, system, user):
27112711
return construct_env_with_vars(get_env_vars_to_add(tools_to_activate, system, user))
27122712

27132713

2714+
def unset_env(key):
2715+
if POWERSHELL:
2716+
return 'Remove-Item env:%s\n' % key
2717+
if CMD:
2718+
return 'set %s=\n' % key
2719+
if CSH:
2720+
return 'unsetenv %s;\n' % key
2721+
if BASH:
2722+
return 'unset %s;\n' % key
2723+
assert False
2724+
2725+
27142726
def construct_env_with_vars(env_vars_to_add):
27152727
env_string = ''
27162728
if env_vars_to_add:
27172729
errlog('Setting environment variables:')
27182730

27192731
for key, value in env_vars_to_add:
27202732
# Don't set env vars which are already set to the correct value.
2721-
if key not in os.environ or to_unix_path(os.environ[key]) != to_unix_path(value):
2722-
errlog(key + ' = ' + value)
2723-
if POWERSHELL:
2724-
env_string += '$env:' + key + '="' + value + '"\n'
2725-
elif CMD:
2726-
env_string += 'SET ' + key + '=' + value + '\n'
2727-
elif CSH:
2728-
env_string += 'setenv ' + key + ' "' + value + '"\n'
2729-
elif BASH:
2730-
env_string += 'export ' + key + '="' + value + '"\n'
2731-
else:
2732-
assert False
2733-
if 'EMSDK_PYTHON' in env_vars_to_add:
2734-
# When using our bundled python we never want the user's
2735-
# PYTHONHOME or PYTHONPATH
2736-
# See https://github.com/emscripten-core/emsdk/issues/598
2737-
if POWERSHELL:
2738-
env_string += 'Remove-Item env:PYTHONHOME\n'
2739-
env_string += 'Remove-Item env:PYTHONPATH\n'
2740-
elif CMD:
2741-
env_string += 'set PYTHONHOME=\n'
2742-
env_string += 'set PYTHONPATH=\n'
2743-
elif CSH:
2744-
env_string += 'unsetenv PYTHONHOME\n'
2745-
env_string += 'unsetenv PYTHONPATH\n'
2746-
elif BASH:
2747-
env_string += 'unset PYTHONHOME\n'
2748-
env_string += 'unset PYTHONPATH\n'
2749-
else:
2750-
assert False
2733+
if key in os.environ and to_unix_path(os.environ[key]) == to_unix_path(value):
2734+
continue
2735+
errlog(key + ' = ' + value)
2736+
if POWERSHELL:
2737+
env_string += '$env:' + key + '="' + value + '"\n'
2738+
elif CMD:
2739+
env_string += 'SET ' + key + '=' + value + '\n'
2740+
elif CSH:
2741+
env_string += 'setenv ' + key + ' "' + value + '";\n'
2742+
elif BASH:
2743+
env_string += 'export ' + key + '="' + value + '";\n'
2744+
else:
2745+
assert False
2746+
2747+
if 'EMSDK_PYTHON' in env_vars_to_add:
2748+
# When using our bundled python we never want the user's
2749+
# PYTHONHOME or PYTHONPATH
2750+
# See https://github.com/emscripten-core/emsdk/issues/598
2751+
env_string += unset_env('PYTHONHOME')
2752+
env_string += unset_env('PYTHONPATH')
2753+
2754+
# Remove any environment variables that might have been set by old or
2755+
# inactive tools/sdks. For example, we set EM_CACHE for older versions
2756+
# of the SDK but we want to remove that from the current environment
2757+
# if no such tool is active.
2758+
# Ignore certain keys that are inputs to emsdk itself.
2759+
ignore_keys = set(['EMSDK_POWERSHELL', 'EMSDK_CSH', 'EMSDK_CMD', 'EMSDK_BASH',
2760+
'EMSDK_NUM_CORES', 'EMSDK_TTY'])
2761+
env_keys_to_add = set(pair[0] for pair in env_vars_to_add)
2762+
for key in os.environ:
2763+
if key.startswith('EMSDK_') or key.startswith('EM_'):
2764+
if key not in env_keys_to_add and key not in ignore_keys:
2765+
errlog('Clearing existing environment variable: %s' % key)
2766+
env_string += unset_env(key)
27512767

27522768
return env_string
27532769

test/test.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,23 @@ source ./emsdk_env.sh
1313
which emcc
1414
emcc -v
1515

16+
# Install an older version of the SDK that requires EM_CACHE to be
17+
# set in the environment, so that we can test it is later removed
18+
./emsdk install sdk-fastcomp-3b8cff670e9233a6623563add831647e8689a86b
19+
./emsdk activate sdk-fastcomp-3b8cff670e9233a6623563add831647e8689a86b
20+
source ./emsdk_env.sh
21+
which emcc
22+
emcc -v
23+
test -n "$EM_CACHE"
24+
25+
# Install the latest version of the SDK which is the expected precondition
26+
# of test.py.
1627
./emsdk install latest
1728
./emsdk activate latest
1829
source ./emsdk_env.sh --build=Release
30+
# Test that EM_CACHE was unset
31+
test -z "$EM_CACHE"
32+
1933
# On mac and windows python3 should be in the path and point to the
2034
# bundled version.
2135
which python3

0 commit comments

Comments
 (0)