10

I have written a batch file, which when executed builds a visual studio solution. The solution comprises of few C# projects. I am using MSBuild utility for this. How can i stop the build from proceeding further when there are compilation errors in any of the projects? Further how can i get the error messages and display them on command prompt?

Ananya
  • 1,503
  • 3
  • 12
  • 8

2 Answers2

5

There's no support for stop on first failure when building a visual studio solution.

You can workaround this by taking the following steps:

  1. Set the environment variable msbuildemitsolution to 1 (set msbuildemitsolution=1);
  2. Invoke MSBuild in order to generate a *.proj file from the target VS solution;
  3. In the generated *.sln.proj file change RunEachTargetSeparately="true" in the target named Build to RunEachTargetSeparately="false";
  4. Invoke MSBuild to build the updated *.sln.proj file.

This answer is based on Dan Moseley answer to a post on MSDN Forums.

João Angelo
  • 56,552
  • 12
  • 145
  • 147
  • I've tried with msbuild in .NET 4.0. Not work, no .proj file is created, I monitored the msbuild acvitity by procmon as well as check the disk file. – zhaorufei Jun 13 '13 at 08:40
  • This is still supported in .NET 4.0; Make sure that you did not have the command prompt window open when setting the environment variable through system settings, if that was the case open a new command prompt window. – João Angelo Jun 13 '13 at 08:52
  • This answer no longer is applicable in Visual Studio 2017. #1) This env-var will cause a `.metaproj` file to be generated instead of a `.sln.proj` file. #2) This file does not contain the property `RunEachTargetSeparately`, which I tested on both .NET C# projects as well as C++. #3) This file doesn't _appear_ to have anything applicable in the sense that it can help prevent more building from occurring after the first error occurs. All error mentions were in regards to NuGet Restore. – kayleeFrye_onDeck Feb 14 '19 at 20:47
4

It would be easier to give you an answer if you would have posted relevant parts of your batch file. Nevertheless, for your second part of the question, here is an example how I solved almost the same issue in one of our build scripts:

msbuild.exe /m /p:Configuration=Release /v:n theSolutionFile.sln >Build.log
if ERRORLEVEL 1 goto :showerror
find "0 Warn" Build.log >nul:
if ERRORLEVEL 1 goto :showerror

goto :EOF

:showerror
echo Build error occurred
exit %ERRORLEVEL%
Doc Brown
  • 19,739
  • 7
  • 52
  • 88
  • Build.log is an empty file for me, this answer might be out of date – reggaeguitar Mar 23 '18 at 19:23
  • @reggaeguitar: no, this is not out of date. Did you run `msbuild.exe /m /p:Configuration=Release /v:n yourSolutionFile.sln` in a VS command prompt and checked what happens? – Doc Brown Mar 23 '18 at 22:53
  • I just tested this again and the Build.log file is empty, it seems to have compiled fine though – reggaeguitar Apr 13 '18 at 14:49
  • @reggaeguitar: if you tested it as I wrote (without `>Build.log`), surely Build.log file will not change, what did you expect? However, you should try to compile the configuration in stake inside Visual Studio first, the output window there should contain the same output as by running msbuild. – Doc Brown Apr 13 '18 at 15:46
  • I tested it with ">Build.log". I originally came to the answer trying to find a way to get the output from MsBuild when it fails into a file because the window closes right away. I realize this is a separate question and I would take back my downvote if I could. I don't see the point of ">Build.log" if it doesn't actually write anything to the file though. – reggaeguitar Apr 13 '18 at 16:31