Skip to content

Commit

Permalink
Specify LastExit scope (#7465)
Browse files Browse the repository at this point in the history
  • Loading branch information
garath committed Jun 4, 2021
1 parent 130a085 commit 4a02fbe
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
6 changes: 5 additions & 1 deletion Documentation/Policy/PowershellBestPractices.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ if ($LASTEXITCODE -ne 0) {
}
```

*There is a known issue when using `$LASTEXITCODE` in release builds where PowerShell will report that the variable has not been set. As a workaround, simply set `$LASTEXITCODE = 0` at the top of your script.*
To set it in a PowerShell script, e.g. to ensure a known value at the start of a script, reference the variable in the global scope.

```powershell
$global:LASTEXITCODE = 0
```

## Set StrictMode and ErrorActionPreference at the top of every file

Expand Down
2 changes: 1 addition & 1 deletion eng/common/sdl/execute-all-sdl-tools.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ try {
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version 2.0
$disableConfigureToolsetImport = $true
$LASTEXITCODE = 0
$global:LASTEXITCODE = 0

# `tools.ps1` checks $ci to perform some actions. Since the SDL
# scripts don't necessarily execute in the same agent that run the
Expand Down
2 changes: 1 addition & 1 deletion eng/common/sdl/init-sdl.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Param(
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version 2.0
$disableConfigureToolsetImport = $true
$LASTEXITCODE = 0
$global:LASTEXITCODE = 0

# `tools.ps1` checks $ci to perform some actions. Since the SDL
# scripts don't necessarily execute in the same agent that run the
Expand Down
2 changes: 1 addition & 1 deletion eng/common/sdl/run-sdl.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Param(
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version 2.0
$disableConfigureToolsetImport = $true
$LASTEXITCODE = 0
$global:LASTEXITCODE = 0

try {
# `tools.ps1` checks $ci to perform some actions. Since the SDL
Expand Down

0 comments on commit 4a02fbe

Please sign in to comment.