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.