1

Could one write a "rule" at project/solution level which tells Visual Studio/msbuild to fail if warnings number is greater then predefined N?

The reason: Solution contains legacy code with 600+ warnings. I'd like to deal them later but would not want new code to increase this number.

dbardakov
  • 651
  • 1
  • 8
  • 22

1 Answers1

2

Force build to fail when warnings > N

According to the Interface IEventSource:

Defines the events raised by the build engine. Loggers receive an object implementing this interface in their Initialize method and use it to subscribe to the events they are interested in receiving.

So we could use a customer logger, which can be written in .NET, such as C#. After searching for a long time, I found a similar problems:

How can I treat MSB3245 (could not resolve reference) warning as an error?

You can refer to the stijn`s answer for detail info.

To resolve this issue, you can try to use class WarningRaisedto calculate the number of warnings:

public class ScanLogger : Logger
{

    private int warnings = 0;
    public override void Initialize(IEventSource eventSource)
    {
       
        eventSource.WarningRaised += (s, e) => ++warnings;
          {
               eventSource.MessageRaised += (s, e) =>
             Common.errorsOccurred |= (warnings >= 600);
          }
    }

See below link for detail info about: IEventSource Interface

Hope this helps.

Community
  • 1
  • 1
Leo Liu
  • 71,098
  • 10
  • 114
  • 135