Failure in ps1 script results in successful build

Adam Ralph's Avatar

Adam Ralph

06 Sep, 2018 07:48 PM

See this code:

And this build:

It seems that failures in ps1 scripts are not propagated as build failures. The build is marked as successful (screenshot attached).

build.ps1 simply contains one line: foo

  1. Support Staff 1 Posted by Owen McDonnell on 06 Sep, 2018 11:09 PM

    Owen McDonnell's Avatar

    By default $ErrorActionPreference is set to "Continue".
    You can change that to "stop" at the start of the ps1 and it will fail the build.

    $ErrorActionPreference" = "Stop"
  2. 2 Posted by Adam Ralph on 07 Sep, 2018 07:52 AM

    Adam Ralph's Avatar

    By default $ErrorActionPreference is set to "Continue".

    This seems like a strange default. What is the reason for this?

  3. 3 Posted by Adam Ralph on 07 Sep, 2018 08:07 AM

    Adam Ralph's Avatar
  4. 4 Posted by Adam Ralph on 07 Sep, 2018 08:59 AM

    Adam Ralph's Avatar

    OK, I now realise that this is a PowerShell default when running with -file, and not an Appveyor default. This seems like awful design on the part of PowerShell, but there it is.

    I wonder how many projects out there are using a build.ps1 without any knowledge of this problem.

    Has any consideration been given to passing the -ErrorAction Stop parameter to avoid people getting tripped up by this?

  5. 5 Posted by Adam Ralph on 07 Sep, 2018 09:11 AM

    Adam Ralph's Avatar

    Alternatively, this could be called out in the docs more clearly. Currently, in the problem is only hinted at:

    If you need to check execution results of commands in the middle of PowerShell script you can verify $LastExitCode after calling a command and terminate earlier, for example:

    And this appears only under the "Script blocks in build configuration" section.

    This seems like something worth calling out in big and bold, front and centre, in the canonical documentation for the ps: setting.

  6. 6 Posted by Adam Ralph on 07 Sep, 2018 09:19 AM

    Adam Ralph's Avatar

    What's even worse is that when executing a script directly by typing build.ps1 in a PowerShell console window, $ErrorActionPreference = "Stop" has no effect. It has to be executed via powershell.exe -file build.ps1 to get a non-zero exit code.

    This all seems crazy.

  7. Support Staff 7 Posted by Owen McDonnell on 07 Sep, 2018 03:54 PM

    Owen McDonnell's Avatar

    Indeed, the various error handling approaches of powershell have been a source of confusion for many.
    $LastExitCode approach is different from $ErrorActionPreference setting. I've found this document to a be a pretty decent and concise summary.

    Regarding our docs, they are open source and you are welcome to make a pull request here.

  8. 8 Posted by Adam Ralph on 07 Sep, 2018 04:16 PM

    Adam Ralph's Avatar

    Thanks for the info. I'll see if I can send a PR to the docs.

  9. Adam Ralph closed this discussion on 07 Sep, 2018 04:17 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts


? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac