Skip to content

Commit c9437b5

Browse files
committed
Always restore old env variables
When we invoke the script block it could throw an exception so we should wrap it in a try finally block to ensure that we can restore the old environment variables, otherwise you can end up back in a shell that has an incorrect set of variables set. This is most noticable if an exception is thrown when entering the vs dev shell since without resetting the old vars you will have invalid Visual Studio env variables set meaning you need to manually reset your shell environment.
1 parent 3b18849 commit c9437b5

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

utils/build.ps1

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -799,16 +799,18 @@ function Invoke-IsolatingEnvVars([scriptblock]$Block) {
799799
foreach ($Var in (Get-ChildItem env:*).GetEnumerator()) {
800800
$OldVars.Add($Var.Key, $Var.Value)
801801
}
802-
803-
& $Block
804-
805-
Remove-Item env:*
806-
foreach ($Var in $OldVars.GetEnumerator()) {
807-
New-Item -Path "env:\$($Var.Key)" -Value $Var.Value -ErrorAction Ignore | Out-Null
802+
try {
803+
& $Block
808804
}
805+
finally {
806+
Remove-Item env:*
807+
foreach ($Var in $OldVars.GetEnumerator()) {
808+
New-Item -Path "env:\$($Var.Key)" -Value $Var.Value -ErrorAction Ignore | Out-Null
809+
}
809810

810-
if ($ToBatch) {
811-
Write-Output "endlocal"
811+
if ($ToBatch) {
812+
Write-Output "endlocal"
813+
}
812814
}
813815
}
814816

0 commit comments

Comments
 (0)