Stop that Build! Don’t waste time building projects in Visual Studio

Some time ago, I ran across this conundrum:  Why does Visual Studio continue to build projects when a dependent project failed to compile?

So, I took it upon myself to figure out how to stop the Visual Studio compiler from building projects unnecessarily.  That is:  stop building immediately after it encounters an error.

My solution took the form of a Visual Studio macro.  

  For the uninitiated: that means writing some Visual Basic code to manage the IDE.  

  For the initiated:  You can cringe with me…  😛

So, I opened the Visual Studio Macros IDE and attached an event handler to the BuildEvents object in the EnvironmentEvents module with the following code snippet:

    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

        '-- When the build of a project fails
        If (Not Success) Then

            '-- Cancel the remaining builds in the Solution
            DTE.ExecuteCommand("Build.Cancel")

            '-- Write a polite message to the OutputWindow that's logging all information about this build
            DTE.ToolWindows.OutputWindow.ActivePane.OutputString(vbCrLf & vbCrLf & "Build failed while compiling " & Project)

        End If

    End Sub

Pretty simple, and prevents Visual Studio from running through all of the projects.

Next time, I’m going to attach a timer and start recording my build times to see how the solution has grown.