47

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.

cigien
  • 57,834
  • 11
  • 73
  • 112
Ben Robbins
  • 2,889
  • 2
  • 31
  • 32
  • 5
    I hear ya, its pretty annoying when questions get closed perfunctorily. Aside from that, sorry I don't have your answer. – Brian Gideon Jun 15 '10 at 01:23
  • 9
    If you don't want your question closed a duplicate, you should link to the question that people will be *assuming* is a duplicate and stating exactly why you believe it's not (e.g. "I saw 'this question' and while it provides an answer for Visual Studio 2008, it does not appear to work in Visual Studio 2010"). Remember that people are providing help here for free and nobody gets paid to do this, so you should try to frame your question in a less accusatory tone. – Dean Harding Jun 15 '10 at 01:40
  • 1
    Might as well start upvoting, seeing as to how the original thread was at +6... – Warty Jun 15 '10 at 01:40
  • 6
    I don't know, I actually didn't find anything in what Ben said overly flagrant. I guess the fact that he even apologized for ranting assuaged the implicit indictment handed out. Its actually a pretty interesting question. I wouldn't mind knowing the answer myself so hopefully this one won't get closed. – Brian Gideon Jun 15 '10 at 02:14
  • the original (closed) question is at http://stackoverflow.com/questions/3013209/how-to-stop-c-compile-on-first-error-in-vs-2010-vs-2008-macros-dont-work-cl - voted to reopen – sean e Jun 15 '10 at 02:20
  • 6
    @codeka - I did do exactly as you said. I didn't initially link to the question because I didn't consider it a duplicate. Once comments were left saying it was a duplicate I responded to them saying it wasn't a duplicate because this was a VS 2010 issue. It got closed anyway. – Ben Robbins Jun 15 '10 at 04:15
  • 1
    @Ben: it also got reopened. Don't be so impatient. That's why I'm voting to close this question as a duplicate of that. The two should be merged. – John Saunders Jun 16 '10 at 02:11
  • According to Microsoft, the related idea of stopping after any project fails to build is now on their roadmap for VS https://developercommunity.visualstudio.com/content/idea/355793/add-option-to-stop-projects-building-if-their-depe.html. I certainly hope it gets done. – William Jockusch Jul 20 '20 at 11:24

3 Answers3

30

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

Noah Richards
  • 6,777
  • 2
  • 31
  • 28
  • 1
    Sorry, what was it you wanted it to do instead? – Noah Richards Sep 23 '10 at 15:01
  • 4
    @Will, thanks, I deserved that. To answer your question: Being able to set http://msdn.microsoft.com/en-us/library/microsoft.build.tasks.msbuild.stoponfirstfailure.aspx on the solution? – David Schmitt Sep 24 '10 at 08:06
  • Gotcha. I've never actually tried setting properties like that on the build tasks, and I wouldn't expect it to work very well (from the warning on MSDN that it is internal only). Sorry :( – Noah Richards Sep 24 '10 at 16:06
  • 3
    @Will: I think David's point is that VS2010 should do this by default, or at least as an option. Requiring a macro that attempts to cancel the build after VS2010 starts generating 1000 errors for a missing semicolon in C++ is not "everything I need it to do". – Jon Apr 27 '11 at 04:05
  • The link is invalid; now the plugin is under: https://marketplace.visualstudio.com/items?itemName=NoahRichards.CancelFailedBuild – Patryk Feb 09 '22 at 10:02
7

Edit: See now that Will beat me on this one - For VS2010 there is an add-in available that can do this, and lots more. VSCommands 2010, via http://vscommands.com/features/

Koob
  • 101
  • 1
  • 2
3

Check my reply here.

I know this might be bit late, but if it helps anyone then they should install the extension VSColorOutput

Then go to Tools => Options => VSColorOutput => General => Set Stop Build on First Error to true.

Hope this helps, happy Debugging!

user13309289
  • 217
  • 2
  • 8