C# – VS2010 – How to automatically stop compile on first compile error

cnetvisual studiovisual studio 2010

At work we have a C# solution with over 80 projects. In VS 2008 we use a macro to stop the compile as soon as a project in the solution fails to build (see this question for several options for VS 2005 & VS 2008: Automatically stop Visual C++ 2008 build at first compile error?).

Is it possible to do the same in VS 2010? What we have found is that in VS 2010 the macros don't work (at least I couldn't get them to work) as it appears that the environment events don't fire in VS 2010.

The default behaviour is to continue as far as possible and display a list of errors in the error window. I'm happy for it to stop either as soon as an error is encountered (file-level) or as soon as a project fails to build (project-level).

Answers for VS 2010 only please. If the macros do work then a detailed explanation of how to configure them for VS 2010 would be appreciated.

Best Answer

(You can now download this as an extension, if you don't want to build it yourself)

This answer only works in VS2010 (seems fair :]). I've put the source up on my github page. Before you can build it, you'll need to install the SDK. Once you've done that, just grab the complete source from github (includes project files) and build that. You can install the output into your normal VS instances by finding the VSIX in your build output and opening it.

The important part is:

public void TextViewCreated(IWpfTextView textView)
{
    var dte = GlobalServiceProvider.GetService(typeof(DTE)) as DTE;
    textView.TextBuffer.Changed += (sender, args) =>
    {
        //Output window is friendly and writes full lines at a time, so we only need to look at the changed text.
        foreach (var change in args.Changes)
        {
            string text = args.After.GetText(change.NewSpan);
            if (BuildError.IsMatch(text))
                dte.ExecuteCommand("Build.Cancel");
        };
    }
}

... where BuildError is a regex defined above that you can tweak. If you have any questions about modifying the code, let me know.