Skip to content
This repository has been archived by the owner on Feb 19, 2019. It is now read-only.

Handle failures when running NuGet (exitCode != 0, output to stderr) #248

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

webcoyote
Copy link

Previously the script Run-NuGet.ps1 ignored the process exit code so that errors were not propagated to the caller.

This change throws an error when either the process exits with a non-zero value or if errors are written to stderr, which makes Chocolatey more suitable for being called from other scripts.

For example, I'm using Chocolatey to install and build a Linux virtual machine on Windows with a single PowerShell command (https://github.com/webcoyote/linux-vm). Chocolatey is awesome for projects like this!

Previously the script Run-NuGet.ps1 ignored the process exit code so that errors were not propagated to the caller.  This change throws an error when either the process exits with a non-zero value or if errors are written to stderr, which makes Chocolatey more suitable for being called from other scripts.
@rismoney
Copy link
Contributor

had you seen #210? I tried to deal with process exits throughout all of chocolatey.

btw- linux-vm looks really really awesome.

@webcoyote
Copy link
Author

I looked at #210 before I submitted my pull request; I was hoping that it already included
the change that I needed. In my (uncommitted) changes on my linux-vm project I skipped over all the batch files and called directly into chocolatey.ps1 exactly because the batch files didn't propagate errors, so I'm glad you fixed that. I did note that it was two months back that your changes were made; I hope they get merged.

I am a bit conflicted about my solution, which uses "throw". It doesn't create a very nice error for end-users, but it's great for developers. Hopefully someone will provide clarity on how errors should be handled, because I was waffling between "throw" and "write-host & exit".

And thanks for your comments on linux-vm. A bit more error checking and I will be happy with it! Let me know if you have any comments on the project -- I'm glad to do fixes :)

@rismoney
Copy link
Contributor

I completely relate. My main concern was to always exit 1 if there was a failure on any of the functions. I recently delved into puppet/ruby so my main concern was to never return an exit 0 if something went wrong which is the current behavior. It's funny, when I use chocolatey interactively i never use the batch files. when i use it programmatically via puppet i used the bat files. its because i never use cmd, so its silly for me to call a batch to call powershell if i am already in it.

unrelated, but calling posh from ruby strangely irks me with powershell.exe, maybe for the opposite reason. If I can run an exec, and call the bat, then i avoid the powershell.exe

I haven't found a great method to interact with powershell via ruby, particularly for multi-line code blocks.

@ferventcoder
Copy link
Contributor

I believe at some point when nuget was throwing non-zero exit codes for any exit, so it was unreliable to be used in that manner. I think they may have fixed this in more recent versions.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants