The Exec problem

46 views
Skip to first unread message

Robin Clowers

unread,
Dec 30, 2009, 1:58:26 PM12/30/09
to psake-users
So I noticed in the 2.0 changes that the exec task was removed. Does
something in powershell 2.0 make this unnecessary? My build still
succeeds if my tests fail, so it seems like this is still a problem.
What is the suggested way to handle the problem now?

Chris Bilson

unread,
Dec 30, 2009, 6:03:04 PM12/30/09
to psake...@googlegroups.com
I don't know if this is why exec got got, but in powershell 1 & 2, "&" and "." kind of do the same thing as exec. I always wondered why there was an exec function in psake:

$testsSucceeded = & RunTestsSomehow.exe  # or .cmd, .ps1
if ($testsSucceeded -ne 0) {
    # bail out or something
}


--c



--

You received this message because you are subscribed to the Google Groups "psake-users" group.
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.



Robin Clowers

unread,
Dec 30, 2009, 7:41:27 PM12/30/09
to psake-users
Yeah it just seems lame to have to do that for every call to a script
or exe you make...

On Dec 30, 3:03 pm, Chris Bilson <cbil...@pobox.com> wrote:
> I don't know if this is why exec got got, but in powershell 1 & 2, "&" and
> "." kind of do the same thing as exec. I always wondered why there was an
> exec function in psake:
>
> $testsSucceeded = & RunTestsSomehow.exe  # or .cmd, .ps1
> if ($testsSucceeded -ne 0) {
>     # bail out or something
>
> }
>
> --c
>

> On Wed, Dec 30, 2009 at 10:58, Robin Clowers <robin.clow...@gmail.com>wrote:
>
>
>
> > So I noticed in the 2.0 changes that the exec task was removed.  Does
> > something in powershell 2.0 make this unnecessary?  My build still
> > succeeds if my tests fail, so it seems like this is still a problem.
> > What is the suggested way to handle the problem now?
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "psake-users" group.
> > To post to this group, send email to psake...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > psake-users...@googlegroups.com<psake-users%2Bunsubscribe@googlegr oups.com>

James Kovacs

unread,
Jan 3, 2010, 10:20:20 PM1/3/10
to psake...@googlegroups.com
I haven't liked PowerShell's behaviour with respect to errors. By default, $ErrorActionPreference is set to Continue, which means the script prints the error and continues executing. In psake, we change $ErrorActionPreference to Stop, which terminates execution. Unfortunately this only works for PowerShell commands and not external executables that return non-zero error codes. That means that if msbuild returns a non-zero error code, the script merrily continues along and tries to execute unit tests against binaries that don't exist. We tried the exec approach to try to encapsulate checking for a non-zero error code and throwing, but that had problems too. I've used the $succeeded technique, but don't like it because it is ugly and feels like COM HRESULTs. If anyone has suggestions on how to fix this, I would love to hear them as it is a major problem with PowerShell in my opinion. Suggestions?

James
--
James Kovacs, B.Sc., M.Sc., MCSD, MCT
Microsoft MVP
http://www.jameskovacs.com
jko...@post.harvard.edu
403-397-3177 (mobile)


To unsubscribe from this group, send email to psake-users...@googlegroups.com.

Jorge Matos

unread,
Jan 5, 2010, 2:02:18 AM1/5/10
to psake...@googlegroups.com
I've been using a version of exec lately that I can add to psake if no one objects:
 
function exec([scriptblock]$cmd, [string]$errorMessage = "Error executing command: " + $cmd)
{
     & $cmd
     if ($lastexitcode -gt 0)
     {
          throw $errorMessage
     }
}
 
Here are some examples of how I use this version of exec:
----------------------------------------------------------------------------------------------------------------
$msg = "Error executing SVN.  Please check that SVN client is installed or that the following URL is valid:`n[$repository_ver]"
exec { $x = (svn info $repository_ver) } $msg
----------------------------------------------------------------------------------------------------------------
exec { svn co $repository_ver $workingcopy_dir --force -q }
----------------------------------------------------------------------------------------------------------------
 
I submit that using a scriptblock makes the code readable and is as close to calling the command directly. 
There isn't much difference in my opinion between the following 2 lines:
 
svn co $repository_ver $workingcopy_dir --force -q
 
exec { svn co $repository_ver $workingcopy_dir --force -q }

Ayende Rahien

unread,
Jan 5, 2010, 2:27:37 AM1/5/10
to psake...@googlegroups.com
Oh, that looks really nice!

James Kovacs

unread,
Jan 5, 2010, 12:14:33 PM1/5/10
to psake...@googlegroups.com
That's excellent and I think about as unobtrusive as you can get with PowerShell's handling (or lack of handling) of exit codes. Please incorporate this into psake.

Thanks!

James
--
James Kovacs, B.Sc., M.Sc., MCSD, MCT
Microsoft MVP
http://www.jameskovacs.com
jko...@post.harvard.edu
403-397-3177 (mobile)


Jorge Matos

unread,
Jan 9, 2010, 3:02:28 AM1/9/10
to psake...@googlegroups.com
James,
 
I added the exec() function but I'm a noob at using git so I'm not sure if I updated the git repo correctly.  It appears I added a new branch called "psake_jorge" which probably needs to be merged into the main master branch. 
Reply all
Reply to author
Forward
0 new messages