Share via


How to make Visual Studio 2005 ignore return codes from pre-build events

While working on some Windows Media Center development scenarios over this past weekend, I ran into an interesting scenario that I could not figure out how to accomplish in Visual Studio 2005 - I wanted to be able to run a pre-build event command line but have Visual Studio 2005 ignore the return code in case of failure.

I searched around in the Visual Studio UI and in the MSDN documentation, but could not find a way to cause Visual Studio to continue if the pre-build event failed. Eventually I found a way to do this, but I had to ask a friend of mine who works on the MSBuild team in Visual Studio. Hopefully anyone else who searches for a way to do this will be able to find this post and figure it out more easily than I did.

I started out by adding the pre-build event in the Visual Studio IDE by doing the following:

  1. Right-click on my project in the Solution Explorer window
  2. Choose Properties from the context menu
  3. Click on the Build Events tab in the property pages
  4. Add the command line in the Pre-build event command line text box

When I built my project, if my pre-build event failed, it would report an error in the Task List and the build stopped at that point.

What I found out from my friend is that the default syntax for the pre-build event is defined in %windir%\Microsoft.NET\Framework\v2.0.50727\Microsoft.Common.targets. This is the file that defines all of the steps in the standard build process for .NET projects. The syntax for the default pre-build event looks like the following:

<Target
Name="PreBuildEvent"
Condition="'$(PreBuildEvent)'!=''"
DependsOnTargets="$(PreBuildEventDependsOn)">
<Exec WorkingDirectory="$(OutDir)" Command="$(PreBuildEvent)" />
</Target>

You will notice in the <Exec> command above that there is not any statement regarding return code processing. That causes Visual Studio to always stop in the case of a non-zero return code from the command line that is executed as a pre-build step.

There are a few options that I found that enabled Visual Studio 2005 to ignore errors in the pre-build event:

Option 1 - Override the PreBuildEvent target in your project file using IgnoreExitCode

In this option, you define your own customized version of the PreBuildEvent <Target> that will override the default version listed in Microsoft.Common.targets. You can add the following to the end of your project file (such as a vbproj or csproj file):

<Target
Name="PreBuildEvent"
Condition="'$(PreBuildEvent)'!=''"
DependsOnTargets="$(PreBuildEventDependsOn)">
<Exec WorkingDirectory="$(OutDir)" Command="$(PreBuildEvent)" IgnoreExitCode="true" />
</Target>

This <Target> section will cause the pre-build event to run if the command line is non-blank in the project file or in the Visual Studio IDE UI. It will ignore the return code from the process and not report it back to you in the Task List, and the build will continue.

If you choose this option, you will have to add the above <Target> section to each project file you want to ignore the pre-build event return code.

Option 2 - Override the PreBuildEvent target in your project file using ContinueOnError

In this option, you also define your own customized version of the PreBuildEvent target that will override the default version listed in Microsoft.Common.targets, but you use different return code processing logic than in option 1. You can add the following to the end of your project file (such as a vbproj or csproj file):

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

This <Target> section will cause the pre-build event to run if the command line is non-blank in the project file or in the Visual Studio IDE UI. It will ignore the return code from the process, but it will report it back to you as a warning in the Task List. The build will also continue even if any errors are encountered.

If you choose this option, you will have to add the above <Target> section to each project file you want to ignore the pre-build event return code.

Option 3 - Change the default PreBuildEvent target

In this option, you directly modify %windir%\Microsoft.NET\Framework\v2.0.50727\Microsoft.Common.targets to add IgnoreExitCode="true" or ContinueOnError="true" to the PreBuildEvent <Target> section as shown above.

If you choose this option, you will cause all .NET projects that you build in Visual Studio 2005 to ignore the pre-build event return code.

Comments

  • Anonymous
    August 08, 2006
    I don't know if this works in VS2005, but in VS2003 I get around this by putting

    exit 0

    at the end of the pre-build batch file.

    Andrew

  • Anonymous
    August 08, 2006
    Hi AndyCherry - That would likely work for Visual Studio 2005 as well.  In my scenario, I was trying to run an executable and did not want to wrap it in a batch file to work around this issue.

  • Anonymous
    August 09, 2006
    Previously, I noted how to cause errors in a multi-step Visual Stuido pre- or post-build event to fail...

  • Anonymous
    September 19, 2007
    PingBack from http://blog.nufer.org/2007/09/19/visual-studio-post-pre-build-events/

  • Anonymous
    December 07, 2007
    Dans Subversion (SVN), le numéro de révision est un identifiant qui est incrémenté automatiquement à chaque fois que des fichiers sont commités dans le référentiel. Ainsi, ce numéro permet d'identifier de manière sûr l'état du projet à un instant T.Il

  • Anonymous
    July 04, 2008
    The comment has been removed

  • Anonymous
    March 17, 2011
    and the cool part is... it works for VS 2010 too

  • Anonymous
    November 21, 2012
    AndyCherry's suggestion is perfect. Worked like a charm. Using it in Visual Studio 2010.