Swallowing errors in pre-build steps in Visual Studio 2010
Asked Answered
P

6

35

My solution has a bunch of projects one of which is a windows service; I have a prebuild step to stop the service and a postbuild step to restart it (this way the windows service exe is not locked when VS is building/overwriting it).

on pre-build: 
net stop myservice

on post-build:
net start myservice

If the service is not running while I'm starting the build, the net stop command fails and that prevents the build from proceeding.

What can I do to still force the build even if the pre-build step failed?

Presidium answered 27/4, 2011 at 15:17 Comment(0)
P
59

I figured it out - you simply need to add the following statement at the end:

SET ERRORLEVEL = 0

or simply:

EXIT 0
Presidium answered 27/4, 2011 at 20:39 Comment(1)
I has to remove the spaces to make the first one work: SET ERRORLEVEL=0Treadway
B
35

I know this is an old post, but I recently had this issue as well. I wanted to kill the process that I was building if it was currently running, and found that I could do:

taskkill /f /im $(TargetName).exe 2>nul 1>nul
Exit 0

2>nul 1>nul without Exit 0, or vice versa, doesn't seem to work. I have to do both.

Also worth noting this is using Visual Studio Express 2012.

I found the solution when looking into this issue as well on this blog

The 2>nul 1>nul will swallow the stderr and stdout from the command. The EXIT 0 will make sure the build event returns 0.

Boylan answered 4/7, 2014 at 21:17 Comment(5)
This is exactly what I was trying to do. One additional bit of info: I find that EXIT 0 in a batch file, called from a VS2015 build action with CALL myscript.bat, will cause the whole action to exit instead of continuing after myscript.bat. But this is fixed by using SET ERRORLEVEL = 0 instead.Refutation
or better just 2>nul &set errorlevel=0 This allows you have an extra lines later. There is no need to redirect stdout 1> nulBuford
Works in VS2017 as wellOberland
Works in VS2019 tooFinkelstein
Oh! Thank you for this! It's the exact same thing (with taskkill) on a prebuild event that's causing me confusion. I thought I've tried all the combinations (i.e. with cmd /c) but your answer works perfectly. Confirmed this is working in VS2022.Eda
B
6

There is no need to redirrect stdout, just stderr. Also, set errorlevel allows you to have extra lines later if you need. Exit will terminate immediately.

taskkill /f /im:$(TargetFileName) 2>nul &set errorlevel=0

Works in VS 2017.

Buford answered 4/4, 2017 at 12:52 Comment(0)
D
4

A bit late, but a good solution, so I'll share it anyway.

The following solution was taken from this blog post.

You simply define your e.g. post build steps as needed. You don't need to wrap them into a batch file, but you could. Then you alter you project file (such as a vbproj or csproj file) and insert the following fragment just before the project closing-tag:

<Target
  Name="PostBuildEvent"
  Condition="'$(PostBuildEvent)'!=''"
  DependsOnTargets="$(PostBuildEventDependsOn)">
  <Exec WorkingDirectory="$(OutDir)" Command="$(PostBuildEvent)" ContinueOnError="true" />
</Target>

This changes the way visual studio executes the post build steps. The ContinueOnError Attributes instructs VS to simply ignore errors. It still issues warnings in the build results, but it does not stop the build.

You can do the same for the PreBuildEvent by changing the above fragment accordingly.

Of course this works only for all of the build steps in full. You can not selectivly ignore errors on certain steps only. If you need to do that you have to stick to the solution with the batch file, but the nice thing about the solution above is you don't need a separate batch.

Sascha

Discontented answered 28/11, 2019 at 9:57 Comment(0)
K
0

Wrap your net commands in a batch file and use exit /B 0

http://ss64.com/nt/exit.html

Kilah answered 27/4, 2011 at 15:26 Comment(0)
C
-3

Would embedding the command in an executable that always returns 0 solve your issue?

in c call

system("net stop myservice")
Classicism answered 27/4, 2011 at 15:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.