Skip to content

TreatWarningsAsErrors set as true ignored #59

@willbush

Description

@willbush

Looks like line 170 in: https://github.com/aspnet/RoslynCodeDomProvider/blob/707c08cf1ea8947548da316a0891213578b1779a/src/Microsoft.CodeDom.Providers.DotNetCompilerPlatform/Compiler.cs

Is setting the TreatWarningsAsErrors as false and thus setting it to true has no effect.

The following code is an example of some code that should have two warnings that ought to cause an error when TreatWarningsAsErrors is true:

using Microsoft.CodeDom.Providers.DotNetCompilerPlatform;
using System;
using System.CodeDom.Compiler;

namespace ConsoleApp4
{
  internal class Program
  {
    const string Source = @"
namespace Samples {
  using System;

  public class Class1 {

    public static void Main() {
      if (false) {
        int notUsed = 0;
      }
      Console.WriteLine(""Hello World!"");
      Console.WriteLine(""Press the Enter key to continue."");
      Console.ReadLine();
    }
  }
}
";
    static void Main()
    {
      var provider = new CSharpCodeProvider();
      var param = new CompilerParameters
      {
        TreatWarningsAsErrors = true,
        WarningLevel = 4,
        //CompilerOptions = "/warnaserror+"
      };
      var result = provider.CompileAssemblyFromSource(param, Source);

      if (result.Errors.HasErrors)
      {
        foreach (var error in result.Errors)
        {
          Console.WriteLine(error);
        }
      }
      else
      {
        Console.WriteLine("done with no errors");
      }
      Console.WriteLine("Press the Enter key to continue.");
      Console.ReadLine();
    }
  }
}

The commented out line //CompilerOptions = "/warnaserror+" has to be uncommented for it to treat warnings as errors (this is a work around).
Note that if I set a break point after the line:

var result = provider.CompileAssemblyFromSource(param, Source);

And inspect the value of TreatWarningsAsErrors in the params object, it is false, which indicates to me that it was modified.

The nuget packages I have installed in the above Console applications are:

Microsoft.Net.Compilers 2.8.2
Microsoft.CodeDom.Providers.DotNetCompilerPlatform 2.0.0

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions