我想将Stylecop警告视为错误,但它对我不起作用。
我的项目被配置为将警告视为错误,如果我使用真正的"编译器警告"它确实显示编译器错误。但是有一个" Stylecop警告"它只显示编译器警告。
因此,当有Stylecop警告时,我向TeamCity烦恼地登记并不会破坏CI构建。
我正在使用VS2013和Stylecop 4.7.49。
项目 - >属性 - >构建
项目 - > 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 ==========
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 ==========
答案 0 :(得分:20)
修改csproj文件以添加以下配置:
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
...
<StyleCopTreatErrorsAsWarnings>false</StyleCopTreatErrorsAsWarnings>
</PropertyGroup>
另请参阅this answer,其中解释了为什么某些警告无法提升为错误。
答案 1 :(得分:3)
您可以使用MSBuild轻松配置StyleCop,以便在StyleCop.MSBuild NuGet包的帮助下将警告显示为错误。 您必须修改项目文件,如下所示。
<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
答案 2 :(得分:1)
如果您使用StyleCop.MSBuild nuget包来启用项目的样式警察。 要将stylecop警告启用为错误,只需添加另一个nuget包 StyleCop.Error.MSBuild(https://www.nuget.org/packages/StyleCop.Error.MSBuild/)
由于
答案 3 :(得分:0)
Currently, this is done by using <TreatWarningsAsErrors>
tag:
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<!-- Ideally this is always enabled, but that tends to hurt developer productivity -->
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>