Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

email article
printer friendly

Stop a Solution Build When a Project Build Fails
Don't be vexed by problem project builds. Here's how to build a macro to identify and solve this issue.
by Enrico Sabbadin

October 2003 Issue

Technology Toolbox: VB.NET

Level: Beginner

You might've run into a situation in VS.NET where a solution build finishes compiling all projects, even though one of the project builds failed in the middle. This isn't behavior I like (nor, I'm willing to bet, do you), because in most cases, all the remaining projects will fail as well—they can't find the reference to the offending assembly.

ADVERTISEMENT

Most of the time, you end up with a huge list of errors and warnings in the output debug window, and you scroll through it nervously to find the problem project. (And if you don't think it's a problem, you've probably never built a 27-assembly solution.)

Fortunately, VS.NET comes with a full automation model, which lets you customize its behavior to even the smallest detail using two techniques: macros and add-ins. Although either technique allows you to accomplish most tasks, I recommend an add-in for advanced tasks and ease of deployment.

You can use a simple macro when a project build fails. First open the Macro IDE; go to Tools | Macros | Macro IDE. Even if this is the first time you open the Macro IDE, you'll notice that the environment isn't empty. The default configuration has two macro projects loaded: MyMacro and Samples. The latter contains a handy set of ready-to-use macros, which reduces the amount of VS.NET code for you to write.

Start creating an EnvironmentEvents module in the MyMacro project (if it's not there already), and copy the code in the EnvironmentEvents module present in the Samples project. The EnvironmentEvents contains all the code you need to hook directly into the main events of the VS.NET environment.

You need to intercept the OnBuildProjConfigDone event, which belongs to the BuildEvents group. Open the EnvironmentEvents module, click on the upper-left dropdown to select the BuildEvents object, then select OnBuildProjConfigDone on the upper-right dropdown. The Macro Editor generates automatically the code required to hook into the desired event.

Now, you just have to type a few lines of code so the event handler appears like this:

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
      DTE.ExecuteCommand("Build.Cancel", "")
      Dim win As Window = DTE.Windows.Item( _
         EnvDTE.Constants.vsWindowKindOutput)
      Dim OW As OutputWindow = CType( _
         win.Object, OutputWindow)
      OW.OutputWindowPanes.Item( _
         "Build").OutputString( _
         "ERROR IN " & Project & _
         " Build Stopped" +
         System.Environment.NewLine)
   End If
End Sub 

As you can see, you must test the Success Boolean variable and call the "Build.Cancel" command if the Success variable evaluates to false. To provide feedback to the developer, dump an error message in the build panel of the output window so he or she can spot immediately which project refuses to compile.

About the Author
Enrico Sabbadin is a software architect and developer working on distributed and Internet-based applications. He's the owner of Sabbasoft, a software consulting company; trains and consults for Francesco Balena's Code Architects; and writes articles for Italian programming magazines and VB2TheMax. E-mail him at .


Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home