VS2010 – How to automatically stop compile on first compile error

(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.

Leave a Comment