【问题标题】:Warnings as Errors - does not apply to Stylecop warnings错误警告 - 不适用于 Stylecop 警告
【发布时间】:2014-09-08 08:53:34
【问题描述】:

我想将 Stylecop 警告视为错误,但它对我不起作用。

我的项目配置为将警告视为错误,如果我使用真正的“编译器警告”进行构建,它确实会显示编译器错误。但是对于“Stylecop 警告”,它只会显示一个编译器警告

因此,当出现 Stylecop 警告时,我对 TeamCity 的签入令人烦恼地不会破坏 CI 构建。

我正在使用带有 Stylecop 4.7.49 的 VS2013。

我的设置:

  • 项目 -> 属性 -> 构建

    • 警告级别:4
    • 禁止警告:1591
    • 将警告视为错误:全部
  • 项目 -> Stylecop 设置 -> 选项

    • 将违规视为错误:已选中

正确中断构建的示例代码,包含真正的编译器警告:

using System;

namespace CodeUsageTest
{
    public class CodeUsage
    {
        private string fff()
        {
            int nobodyLovesMe; //CS0168
            return "";
        }
    }
}

构建输出:

1>------ Build started: Project: CodeUsageTest, Configuration: Debug Any CPU ------
1>D:\Sandbox\CodeUsageTest\CodeUsage.cs(9,17,9,30): error CS0168: Warning as Error: The variable 'nobodyLovesMe' is declared but never used
========== Build: 0 succeeded, 1 failed, 3 up-to-date, 0 skipped ==========

不会破坏构建的示例代码(虽然我想要它),包含 stylecop 警告:

using System;

namespace CodeUsageTest
{
    public class CodeUsage
    {
        private string fff() //SA1300
        {
            return ""; //SA1122
        }
    }
}

构建输出:

1>------ Build started: Project: CodeUsageTest, Configuration: Debug Any CPU ------
1>D:\Sandbox\CodeUsageTest\CodeUsage.cs(7,1): warning : SA1300 : CSharp.Naming : method names begin with an upper-case letter: fff.
1>D:\Sandbox\CodeUsageTest\CodeUsage.cs(9,1): warning : SA1122 : CSharp.Readability : Use string.Empty rather than "".
========== Build: 1 succeeded, 0 failed, 3 up-to-date, 0 skipped ==========

【问题讨论】:

标签: c# visual-studio visual-studio-2013 compiler-warnings stylecop


【解决方案1】:

修改您的 csproj 文件以添加以下配置:

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

另请参阅this answer,它解释了为什么某些警告不能升级为错误。

【讨论】:

    【解决方案2】:

    在 StyleCop.MSBuild NuGet 包的帮助下,您可以使用 MSBuild 轻松配置 StyleCop,以使警告显示为错误。 您必须如下修改您的项目文件。

    <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>
    

    而且要忽略自动生成的文件,您可以修改Settings.StyleCop 文件,如下所示。

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

    在此处查看完整的帖子。 Configure StyleCop with MSBuild to treat Warnings as Errors

    【讨论】:

      【解决方案3】:

      如果您使用 StyleCop.MSBuild nuget 包在您的项目中启用样式警察。 要将 stylecop 警告作为错误启用,只需添加另一个 nuget 包 StyleCop.Error.MSBuild (https://www.nuget.org/packages/StyleCop.Error.MSBuild/)

      谢谢

      【讨论】:

        【解决方案4】:

        Currently, this is done by using &lt;TreatWarningsAsErrors&gt; tag:

         <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
           <!-- Ideally this is always enabled, but that tends to hurt developer productivity --> 
           <TreatWarningsAsErrors>true</TreatWarningsAsErrors> 
         </PropertyGroup> 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-04-20
          • 2011-03-18
          • 2013-04-24
          • 2021-11-20
          • 2012-11-11
          • 1970-01-01
          • 2015-01-12
          相关资源
          最近更新 更多