1

General task. My task is to make a build (of a set of C# projects) failed if there exists such DLLs which were not found and thus were not copied to the output folder. It seems that a corresponding warning is not a warning of a compiler, thus I cannot provide a warning code to the "Treat warnings as errors" option in Visual Studio. The workaround I found is to use MsBuild like it is described here. However I still cannot find any solution for building with a Visual Studio.

Question. Is there a way to obtain a build log and especially build warnings in a post-build event in Visual Studio and make the build failed if some exact warnings exist?

What I looked at. Looked here, but suggested macro is not a convinient solution. According to this and this Visual Studio does not save build logs for C# projects.

More specific. I have been asked to provide information about the specific warning which I want to make an error of. This is "MSB3245: Could not resolve this reference. Could not locate the assembly..."

Community
  • 1
  • 1
Hoborg
  • 891
  • 12
  • 21
  • Have you considered creating an msbuild target which runs after the build, scans for missing dlls and raises an error in case something is wrong? Or maybe if you edit your answer and be more spcific about the condition which is to be treated as an error, e.g. add a practical example, someone can come up with a proper solution? – stijn Sep 09 '16 at 09:01
  • @stjin Thanks for the suggestion. How do I know what files are missing? If I had an access to build log I could catch a warning with the specific code or message. But in other case... should I analyze structure of the project files (csproj) and compare reference information with the DLLs located in the output folder? Updated the answer; I want catch MSB3245. – Hoborg Sep 09 '16 at 09:31
  • It is a FAQ, canonical Q+A [is here](http://stackoverflow.com/questions/17533008/treat-all-warnings-as-errors), MSBuild issue [is here](https://github.com/Microsoft/msbuild/issues/68). So no, fix the real problem. – Hans Passant Sep 09 '16 at 09:56
  • @HansPassant Thanks, I have seen these pages. I suppose that the real problem is an inconsistency in the configuration of the project references which leads to the runtime exception (because the needed DLL cannot be found). Thus now I see very few ways to fix this: 1) fail a build process in case of MSB3245, 2) fail a build process after comparison of the project file structure and the output folder files (as stjin suggested). – Hoborg Sep 09 '16 at 11:14
  • What Hans suggest is you fix the real problem, not the diagnosis. There's another way to turn that warning into a hard error btw: add a post-build which runs your program :]. But again, thatdoesn't *fix* much. – stijn Sep 10 '16 at 08:04

2 Answers2

1

You could check files in AfterBuild target. For example:

<Target Name="AfterBuild">
    <Error Condition="Exists('$(MSBuildProjectDirectory)\bin\Debug\ClassLibrary1.pdb')==false" Text="file not exist"/>
  </Target>
starian chen-MSFT
  • 33,174
  • 2
  • 29
  • 53
  • Thank you for the reply. Your solution implies the analysis of the references from the project file (or hardcoding them into such condition attributes). I try to avoid this because of logic duplication: if the tools (MsBuild/Visual Studio) identify the problem, then it is better to utilize this identification process and not to create another. However it is becoming clearer that this duplication is unavoidable. – Hoborg Sep 13 '16 at 08:57
  • Since it doesn't generate a build log file, to get the detail build log, you need to use custom logger, but visual studio haven't the feature to specify msbuild argument, so you need to use MSBuild command with logger argument directly. On the other hand, you may check files in post event. http://geekswithblogs.net/dchestnutt/archive/2006/05/30/80113.aspx – starian chen-MSFT Sep 14 '16 at 05:17
  • Thanks for the clarification. – Hoborg Sep 14 '16 at 05:30
  • @Hoborg So, what's your final way to do it? You can make a conclusion and post an answer. – starian chen-MSFT Sep 14 '16 at 05:35
0

I have been asked to post a conclusion from the discussion of the current question and my company's experience. So here it is.

Answer. There is no way "to access build warnings in a post-build event in Visual Studio" for managed code projects (excluding some very specific cases, see suggestion about TFS).

Workaround. Currently we use MsBuild to catch warnings and transform them into errors as it is described here. So developers do not have access to this functionality while they use standard Visual Studio (or ReSharper) build process. However our continuous integration server uses exactly MsBuild and thus it informs us about warnings and errors which we are interested in.

Other considerations.

  1. If the "Treat warnings as errors" functionality in MsBuild were introduced, one could utilize it for similar purposes.
  2. One could create a special MsBuild task analyzing the references in project files (.csproj) and comparing this refernces with the files copied to the output folder. This task must be injected in every necessary project file.
  3. Some components of TFS probably save build logs (not familiar with this).
Community
  • 1
  • 1
Hoborg
  • 891
  • 12
  • 21
  • You can mark it as answer after 24 hours. On the other hand, with TFS build system, you could specify MSBuild arguments, also it supports continue integration build (https://www.visualstudio.com/docs/build/get-started/dot-net), so you just need to specify custom logger MSBuild argument and add custom task to project file (csproj) to achieve your requirement. – starian chen-MSFT Sep 15 '16 at 06:42