Warnings as Errors - does not apply to Stylecop warnings

You can easily configure StyleCop with MSBuild to make warnings appear as errors with the help of StyleCop.MSBuild NuGet package. You have to modify your project file as below.

<PropertyGroup>
  <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
  <StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
</PropertyGroup>

And also to ignore auto generated files you can modify Settings.StyleCop file as below.

<CollectionProperty Name="GeneratedFileFilters">
  <Value>\.g\.cs$</Value>
  <Value>\.generated\.cs$</Value>
  <Value>\.g\.i\.cs$</Value>
  <Value>TemporaryGeneratedFile_.*\.cs$</Value>
</CollectionProperty>

See the complete post here. Configure StyleCop with MSBuild to treat Warnings as Errors


If you are using StyleCop.MSBuild nuget package to enable style cop on your projects. To enable stylecop warnings as errors just add another nuget package StyleCop.Error.MSBuild (https://www.nuget.org/packages/StyleCop.Error.MSBuild/)

Thanks


Modify your csproj file to add the following configuration:

  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    ...
    <StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
  </PropertyGroup>

Also see this answer that explains why some warnings cannot be promoted to errors.