Exec reports failure when $lastexitcode not set

Showing 1-4 of 4 messages
Exec reports failure when $lastexitcode not set John Price 7/19/11 2:10 PM
I originally came across this post while I was investigating why my nested builds were not failing. The suggestion to wrap calls to Invoke-Psake in "Exec" solved that problem, but I'm wondering if I've now encountered a bug in how exec checks for errors.

Specifically, it appears that if your psake script is "pure" powershell, it's possible for $lastexitcode to never be set to anything. When "exec" then checks "if ($lastexitcode -ne 0)", it fails because $lastexitcode is $null, not 0.

As a very simple example, running the command below in a fresh powershell session (and after loading the psake module) reports a failure for me:

exec { get-childitem }

Obviously you'd never bother to wrap get-childitem in an exec call, but the behavior is the same with "invoke-psake" if the script you're calling doesn't make any non-powershell calls (and no non-powershell calls have yet been made in your current session).

Should exec be checking for 0 or  $null?
Re: Exec reports failure when $lastexitcode not set John Price 7/19/11 2:36 PM
Actually, checking for $null doesn't seem to solve the problem either... it looks like adding that check prevents powershell exceptions in nested builds from triggering a failure.
Re: Exec reports failure when $lastexitcode not set James Kovacs 8/5/11 1:08 PM
The only reason for exec {} is to translate non-zero exit codes into exceptions. It's a hack that I didn't want to use, but was forced to because of PowerShell's limitations in error handling with standard console programs. If you're calling a pure PowerShell script, you should not use exec {} but simply call the command.

James Kovacs, B.Sc., M.Sc.
@jameskovacs (Twitter)
403-397-3177 (mobile)
jameskovacs (Skype)

You received this message because you are subscribed to the Google Groups "psake-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/psake-users/-/VMY6URc24x4J.
To post to this group, send email to psake...@googlegroups.com.
To unsubscribe from this group, send email to psake-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/psake-users?hl=en.

Re: Exec reports failure when $lastexitcode not set John Price 8/7/11 1:00 PM
I understand you wouldn't normally call a powershell command this way, but as I said, I was looking for a solution to the problem that a failure from a nested call to Invoke-Psake doesn't seem to cause the calling psake build to register a failure.

In the thread I linked to above, Jorge has suggested wrapping the nested call in exec, which solved that problem, but introduces the $lastexitcode problem. If someone knows a way to get nested builds to fail the calling build that doesn't involve wrapping Invoke-Psake with exec{}, that would definitely make this issue moot.