[windows] Use `os.environ.get` in `win_get_environment_variable` Use `os.environ.get` instead of `os.environ[key]` in both the non-registry shell path and the `fallback` path so missing variables return `None` quietly instead of raising `KeyError`. Previously, the `fallback` lookup raised `KeyError(key)` into the outer `except` block when an unset variable (such as `EMSDK_NODE` on initial `activate --permanent`) was missing from both the registry and `os.environ`. Because `KeyError.args[0]` is `key` rather than `2`, the `e.args[0] != 2` check misidentified it as a registry read failure and logged `Failed to read environment variable`. See: #1816
diff --git a/emsdk.py b/emsdk.py index aee5bfa..b0fe676 100644 --- a/emsdk.py +++ b/emsdk.py
@@ -355,7 +355,7 @@ def win_get_environment_variable(key, system=True, user=True, fallback=True): if (not system and not user and fallback): # if no --system or --permanent flag is provided use shell's value - return os.environ[key] + return os.environ.get(key) try: folder = None try: @@ -372,14 +372,14 @@ # need to be precise not to set these back to system registry, or # expansion items would be lost. if fallback: - return os.environ[key] + return os.environ.get(key) return None finally: if folder is not None: folder.Close() except Exception as e: - # this catch is if both the registry key threw an exception and the key is not in os.environ + # Catch any unexpected error (e.g. closing the registry key). if e.args[0] != 2: # 'The system cannot find the file specified.' errlog(f'Failed to read environment variable {key}:')