Fix PowerShell scripts to support StrictMode Add `Set-StrictMode -Version latest` to PowerShell scripts. Initialize `$EMSDK_PY` to `$null` and use `-ErrorAction SilentlyContinue` when removing environment variables so execution under strict mode does not fail on uninitialized variables or missing env items. Fixes: #1337
diff --git a/emsdk.ps1 b/emsdk.ps1 index 111047f..31201d2 100644 --- a/emsdk.ps1 +++ b/emsdk.ps1
@@ -1,5 +1,9 @@ +Set-StrictMode -Version latest + $ScriptDirectory = Split-Path -parent $PSCommandPath +$EMSDK_PY = $null + $PythonLocations = $( "python\3.13.3_64bit\python.exe", "python\3.9.2_64bit\python.exe", @@ -32,9 +36,9 @@ # to set up the environment variables if ($ExitCode -eq 0 -and (Test-Path $ScriptDirectory/emsdk_set_env.ps1)) { & $ScriptDirectory/emsdk_set_env.ps1 - Remove-Item $ScriptDirectory/emsdk_set_env.ps1 + Remove-Item $ScriptDirectory/emsdk_set_env.ps1 -ErrorAction SilentlyContinue } -Remove-Item Env:\EMSDK_POWERSHELL +Remove-Item Env:\EMSDK_POWERSHELL -ErrorAction SilentlyContinue exit $ExitCode
diff --git a/emsdk.py b/emsdk.py index 9fda3a2..1137d97 100644 --- a/emsdk.py +++ b/emsdk.py
@@ -2764,7 +2764,7 @@ def unset_env(key): if POWERSHELL: - return 'Remove-Item env:%s\n' % key + return 'Remove-Item env:%s -ErrorAction SilentlyContinue\n' % key if CMD: return 'set %s=\n' % key if CSH:
diff --git a/emsdk_env.ps1 b/emsdk_env.ps1 index ab5fc4d..3dc92f8 100644 --- a/emsdk_env.ps1 +++ b/emsdk_env.ps1
@@ -1,2 +1,4 @@ +Set-StrictMode -Version latest + $ScriptDirectory = Split-Path -parent $PSCommandPath & "$ScriptDirectory/emsdk.ps1" construct_env
diff --git a/test/test_activation.ps1 b/test/test_activation.ps1 index 31a2c8e..7ecbd59 100644 --- a/test/test_activation.ps1 +++ b/test/test_activation.ps1
@@ -2,6 +2,8 @@ # and checks if the environment variables and PATH are correctly updated. Set $env:SYSTEM_FLAG and $env:PERMANENT_FLAG to test each. # If no flag is provided the process/shell values are tested. See the CI file for an example. +Set-StrictMode -Version latest + refreshenv $repo_root = [System.IO.Path]::GetDirectoryName((resolve-path "$PSScriptRoot"))
diff --git a/test/test_bazel.ps1 b/test/test_bazel.ps1 index dc0393a..6688067 100644 --- a/test/test_bazel.ps1 +++ b/test/test_bazel.ps1
@@ -1,4 +1,5 @@ $ErrorActionPreference = 'Stop' +Set-StrictMode -Version latest Set-Location bazel
diff --git a/test/test_path_preservation.ps1 b/test/test_path_preservation.ps1 index 0391b63..5e4619a 100644 --- a/test/test_path_preservation.ps1 +++ b/test/test_path_preservation.ps1
@@ -2,6 +2,8 @@ # and checks if parts of PATH are lost or overwritten. Set $env:SYSTEM_FLAG and $env:PERMANENT_FLAG to test each. # If no flag is provided the process/shell values are tested. See the CI file for an example. +Set-StrictMode -Version latest + refreshenv $repo_root = [System.IO.Path]::GetDirectoryName((resolve-path "$PSScriptRoot"))