Stop Build on first error in Visual Studio 2010Posted: Last updated:
tl;dr: StopOnFirstBuildError is a Visual Studio 2010 and 2012 extension that cancels the rest of a solution build if a single project fails to compile, thus saving you time. Download it in the Visual Studio Gallery.
At work I often work on a solution that has around 25 projects. When one of the projects fails to build, Visual Studio insists on trying to build the rest of the projects, even though at that point I don't want it to, since I'm never going to run the program when some of the projects have failed. Often the other projects depend on the project that failed, and the error list gets filled with errors from those projects that just obscure the root cause of the problem. So, I figured I could probably do something about it.
I googled around for a solution and found a StopOnFirstFailure property in MSBuild that sounded promising, but I couldn't find any good way to hook into the VS->MSBuild relationship. I'm sure there is some way to do this, so if anyone knows, please let me know. The next thing I found was a tip from Steve Dunn. It's a nice little macro that listens for an event that is fired after each project is built, and calls the Cancel Build command if a project has failed to build. You just open the Macro IDE, open the EnvironmentEvents.vb file and paste the following code in at the bottom:
Private Sub BuildEvents_OnBuildProjConfigDone( _ ByVal Project As String, _ ByVal ProjectConfig As String, _ ByVal Platform As String, _ ByVal SolutionConfig As String, _ ByVal Success As Boolean) Handles BuildEvents.OnBuildProjConfigDone If Success = False Then 'The build failed...cancel any further builds. DTE.ExecuteCommand("Build.Cancel") End If End Sub
This does 95% of what I wanted to do. But still, I'm not a big fan of Visual Studio Macros, I prefer extensions where possible so I can easily see in one place what things I have installed instead of digging through the Macro projects. I also wanted to activate the error window after cancelling the build and be able to turn this on and off easily. So, I made an extension out of it. The main thing in it is still the macro code from Steve Dunn above, but in addition the extension:
- Activates the error list after cancelling the build.
- Prints a message to the Build output window, saying why the build was cancelled.
- Adds a menu item to the Build menu, "Stop Build on first error", which you can use to turn the functionality on and off easily.
- Is context aware, the menu item and functionality are only available in multi-project solutions, since it would be pretty useless in a single project solution.
And that's it. You can download the extension in the Visual Studio Gallery, get the GPL'd source code at https://github.com/einaregilsson/StopOnFirstBuildError and follow me on Twitter @einaregilsson to get notified of updates and other new extensions. Enjoy!