将所有警告视为错误 [英] Treat all warnings as errors

查看:319
本文介绍了将所有警告视为错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是显而易见的事,但我就是不能使它工作...

我试图做的是简单的:我想我的编译失败与错误,如果有一个警告。是的,著名的 TreatWarningsAsErrors ...

我在我的C#项目属性设置为

这导致我的csproj的除外 TreatWarningsAsErrors 部分:

 < TreatWarningsAsErrors>真< / TreatWarningsAsErrors>
 

到目前为止好,如果我在code增加一个无用的私人变量,它会导致编译错误:

 私人诠释未使用的;
 

  

错误3警告如错误:字段'XXXX.unused从未使用过

但整个问题是,我不能让它对于集引用问题工作。如果我有一个引用一个未知的组装,编译器(或devenv的或的MSBuild)抛出一个警告,但我想要一个错误信息。

最后,我想配置一门控签入TFS构建配置,所以TFS会拒绝提交的情况下,有一个的引用的组件XXXX无法找到。的 警告。东西比修改构建流程模板简单的将是巨大的。

解决方案

的MSBuild警告(全部以MSB *),而不是CSC警告不能pssed也提升到错误燮$ P $。对于原因,<一个href="http://msdn.microsoft.com/en-us/library/9ad3f294.aspx"><$c$c>ResolveAssemblyReference任务输出它的消息对飞,不聚合任何人。

唯一可行的解​​决方案正在读的MSBuild记录在TFS生成创建的文件。 我觉得最优雅的解决方案是实现一个自定义生成<一href="http://msdn.microsoft.com/en-us/library/system.activities.$c$cactivity"><$c$c>$c$cActivity.下面是一个简单的活动,将输出结果中包含的任何文件给定 SearchString

 使用系统;
使用System.Activities;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq的;
使用Microsoft.TeamFoundation.Build.Client;

命名空间MyBuildActivities.FileSystem
{
    [BuildActivity(HostEnvironmentOption.Agent)
    公共密封类ReadStringFromFile:codeActivity
    {
        [RequiredArgument]
        公共InArgument&LT; IEnumerable的&LT;字符串&GT;&GT;文件{获得;组; }

        [RequiredArgument]
        公共InArgument&LT;字符串&GT; SearchString {获得;组; }

        公共OutArgument&LT;字符串&GT;结果{获得;组; }

        保护覆盖无效执行(codeActivityContext上下文)
        {
            var中的文件= context.GetValue(文件);
            VAR searchString = context.GetValue(SearchString);

            VAR列表=
                (files.Where(文件=&GT; File.ReadAllText(文件)。载(searchString))
                    。选择(文件=&GT;的String.Format({0}被发现在{1},searchString,文件)))了ToList()。

            如果(list.Count大于0)
                Result.Set(背景下,的string.join(Environment.NewLine,列表));
        }
    }
}
 

声明中,像这样的构建过程模板:

 的xmlns:CFS =CLR的命名空间:MyBuildActivities.FileSystem;装配= MyBuildActivities
 

只是在编译的结束和测试配置调用序:

 &LT;序列显示名称=处理的MSBuild错误&GT;
         &LT; Sequence.Variables&GT;
                 &LT;变量x:TypeArguments =SCG:IEnumerable的(X:字符串)NAME =日志文件/&GT;
                 &LT;变量x:TypeArguments =X:字符串NAME =readStringFromFileResult/&GT;
         &LT; /Sequence.Variables>
         &LT; mtbwa:FindMatchingFiles显示名称=查找日志文件MatchPattern =[的String.Format(安培; QUOT; {0} \ ** \ *日志和放大器; QUOT;,logFileDropLocation。)结果=[日志文件]mtbwt :BuildTrackingParticipant.Importance =低/&GT;
         &LT; CFS:ReadStringFromFile文件=[日志文件]SearchString =MSB3245结果=[readStringFromFileResult]/&GT;
         &LT; mtbwa:WriteBuildMessage显示名称=写入结果消息=[readStringFromFileResult]重要性=[Microsoft.TeamFoundation.Build.Client.BuildMessageImportance.High]/&GT;
         &LT;如果条件=[readStringFromFileResult.Count&GT; 0]显示名称=如果SearchString发现mtbwt:BuildTrackingParticipant.Importance =低&GT;
                 &LT; If.Then&GT;
                          &LT;投掷显示名称=抛出异常异常=[新的异常(readStringFromFileResult)]mtbwt:BuildTrackingParticipant.Importance =低/&GT;
                 &LT; /If.Then>
         &LT; /如果&GT;
&LT; /序列&GT;
 

我测试过这对TFS 2012尽管它应该适用于TFS 2010也是如此。

This should be obvious to do, but I just couldn't make it work...

What I'm trying to do is simple: I would like my compilation to fail with an error if there is a warning. Yes, the famous TreatWarningsAsErrors...

I configured it on my C# project properties

This results in the excepted TreatWarningsAsErrors section in my csproj:

<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

So far so good, if I add an useless private variable in my code, it results in a compilation error:

private int unused;

Error 3 Warning as Error: The field 'XXXX.unused' is never used

But the whole problem is, I can't make it work for assembly reference issues. If I have a reference to an unknown assembly, the compiler (either devenv or msbuild) throws a warning, but I want an error instead.

Ultimately, I'm trying to configure a gated check-in TFS build configuration, so TFS would reject a commit in case there is a "The referenced component 'XXXX' could not be found." warning. Something simpler than modifying the build process template would be great.

解决方案

MSBuild warnings (all start with MSB*) as opposed to CSC warnings cannot be suppressed nor promoted to errors. For the reason the ResolveAssemblyReference task prints its messages on the fly and does not aggregate any of them.

The only feasible solution is reading the MSBuild log files created during the TFS build. I think the most elegant solution is to implement a custom Build CodeActivity. The following is a simple activity that will output to results any files containing a given SearchString:

using System;
using System.Activities;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.TeamFoundation.Build.Client;

namespace MyBuildActivities.FileSystem
{
    [BuildActivity(HostEnvironmentOption.Agent)]
    public sealed class ReadStringFromFile : CodeActivity
    {
        [RequiredArgument]
        public InArgument<IEnumerable<string>> Files { get; set; }

        [RequiredArgument]
        public InArgument<string> SearchString { get; set; }

        public OutArgument<string> Result { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            var files = context.GetValue(Files);
            var searchString = context.GetValue(SearchString);

            var list =
                (files.Where(file => File.ReadAllText(file).Contains(searchString))
                    .Select(file => string.Format("{0} was found at {1}", searchString, file))).ToList();

            if(list.Count > 0)
                Result.Set(context, string.Join(Environment.NewLine, list));
        }
    }
}

Declared in the build process template like so:

xmlns:cfs="clr-namespace:MyBuildActivities.FileSystem;assembly=MyBuildActivities"

Invoked just at the end of the Compile and Test for Configuration sequence:

<Sequence DisplayName="Handle MSBuild Errors">
         <Sequence.Variables>
                 <Variable x:TypeArguments="scg:IEnumerable(x:String)" Name="logFiles" />                                                                                                                 
                 <Variable x:TypeArguments="x:String" Name="readStringFromFileResult" />
         </Sequence.Variables>
         <mtbwa:FindMatchingFiles DisplayName="Find Log Files" MatchPattern="[String.Format(&quot;{0}\**\*.log&quot;, logFileDropLocation)]" Result="[logFiles]" mtbwt:BuildTrackingParticipant.Importance="Low" />
         <cfs:ReadStringFromFile Files="[logFiles]" SearchString="MSB3245" Result="[readStringFromFileResult]" />
         <mtbwa:WriteBuildMessage DisplayName="Write Result" Message="[readStringFromFileResult]" Importance="[Microsoft.TeamFoundation.Build.Client.BuildMessageImportance.High]" />
         <If Condition="[readStringFromFileResult.Count > 0]" DisplayName="If SearchString Was Found" mtbwt:BuildTrackingParticipant.Importance="Low">
                 <If.Then>
                          <Throw DisplayName="Throw Exception" Exception="[New Exception(readStringFromFileResult)]" mtbwt:BuildTrackingParticipant.Importance="Low" />
                 </If.Then>
         </If>                                                                                                              
</Sequence>

I've tested this on TFS 2012 though it should work for TFS 2010 as well.

这篇关于将所有警告视为错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆