jenkinsci / jenkinsci/durable-task-plugin
[JENKINS-63436] PowerShell Pipeline step throws an error when using Set-PSDebug -Strict, and $LASTEXITCODE is not defined
- Dominant language
- Java
- Stars
- 30
- Forks
- 101
- PR merge metrics
- No merged PRs in 30d
Description
The PowerShell command Set-PSDebug -Strict makes PowerShell throw an error if an undefined variable is referenced.
The $LASTEXITCODE automatic variable returns the exit code of the last external executable that was run in the PowerShell session. If no external executables have been run, then $LASTEXITCODE does not exist.
The PowerShell Pipeline step apparently wraps the content of the specified script in some additional code, which checks the $LASTEXITCODE variable.
If no external executables have been run in the script, and Set-PSDebug -Strict is enabled, then the PowerShell Pipeline step throws an error like this:
```
powershell.exe : The variable '$LASTEXITCODE' cannot be retrieved because it has not been set.
At D:\Jenkins.jenkins\workspace\test-pipeline@tmp\durable-0edceab5\powershellWrapper.ps1:3 char:1
+ & powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Comm ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (The variable '$...s not been set.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
At D:\Jenkins.jenkins\workspace\test-pipeline@tmp\durable-0edceab5\powershellScript.ps1:9 char:6
+ exit $LASTEXITCODE;
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (LASTEXITCODE:String) [], RuntimeException
+ FullyQualifiedErrorId : VariableIsUndefined
The variable '$LASTEXITCODE' cannot be retrieved because it has not been set.
At line:1 char:96
+ ... eline@tmp\durable-0edceab5\powershellScript.ps1'; exit $LASTEXITCODE;
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (LASTEXITCODE:String) [], RuntimeException
+ FullyQualifiedErrorId : VariableIsUndefined
```
Here is a demonstration script:
node ('master') {
powershell(label: "test",
script: '''
Set-PSDebug -Strict
"Either of the commented out lines below will make the error below go away." | Out-Host
# cmd.exe /c :: Running cmd.exe will make PowerShell define the LASTEXITCODE variable
# Set-PSDebug -Strict:$false
"Does the LASTEXITCODE variable exist?" | Out-Host
Test-Path Variable:LASTEXITCODE
''')
}
---
Originally reported by langtonben, imported from: PowerShell Pipeline step throws an error when using Set-PSDebug -Strict, and $LASTEXITCODE is not defined
Raw content of original issue
The PowerShell command Set-PSDebug -Strict makes PowerShell throw an error if an undefined variable is referenced.
The $LASTEXITCODE automatic variable returns the exit code of the last external executable that was run in the PowerShell session. If no external executables have been run, then $LASTEXITCODE does not exist.
The PowerShell Pipeline step apparently wraps the content of the specified script in some additional code, which checks the $LASTEXITCODE variable.
If no external executables have been run in the script, and Set-PSDebug -Strict is enabled, then the PowerShell Pipeline step throws an error like this:
powershell.exe : The variable '$LASTEXITCODE' cannot be retrieved because it has not been set.
At D:\Jenkins.jenkins\workspace\test-pipeline@tmp\durable-0edceab5\powershellWrapper.ps1:3 char:1
+ & powershell -NoProfile -NonInteractive -ExecutionPolicy Bypass -Comm ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (The variable '$...s not been set.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
At D:\Jenkins.jenkins\workspace\test-pipeline@tmp\durable-0edceab5\powershellScript.ps1:9 char:6
+ exit $LASTEXITCODE;
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (LASTEXITCODE:String) [], RuntimeException
+ FullyQualifiedErrorId : VariableIsUndefined
The variable '$LASTEXITCODE' cannot be retrieved because it has not been set.
At line:1 char:96
+ ... eline@tmp\durable-0edceab5\powershellScript.ps1'; exit $LASTEXITCODE;
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (LASTEXITCODE:String) [], RuntimeException
+ FullyQualifiedErrorId : VariableIsUndefined
Here is a demonstration script:
node ('master') {
powershell(label: "test",
script: '''
Set-PSDebug -Strict
"Either of the commented out lines below will make the error below go away." | Out-Host
# cmd.exe /c :: Running cmd.exe will make PowerShell define the LASTEXITCODE variable
# Set-PSDebug -Strict:$false
"Does the LASTEXITCODE variable exist?" | Out-Host
Test-Path Variable:LASTEXITCODE
''')
}
environment
```
Jenkins 2.252
Durable Task Plugin 1.3.4
Windows Server 2019
PowerShell 5.1.17763.1007
Adopt OpenJDK 11
```
Contributor guide
Assessment
This issue has not been assessed yet.