如何将MSB3245(无法解析参考)警告视为错误? [英] How can I treat MSB3245 (could not resolve reference) warning as an error?

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

问题描述

我的问题基于

My question is based on another one, but I want to do the opposite: tell msbuild to treat the warning as an error instead of suppressing a specific msbuild warning.

但是,到目前为止,我所看到的只是说/p:WarningsAsErrors仅包含csc警告和错误.我试着只是放下MSB,然后搜索可能要输入的数字,但是没有运气.

However, all I see so far says /p:WarningsAsErrors only takes in csc warnings and errors. I tried just dropping the MSB and googling for what number might go in there to work, but no luck.

是否可以将msbuild(命令行)中的未找到程序集引用"警告视为错误?

Is there any way to treat an "assembly reference not found" warning from msbuild (command line) as an error?

推荐答案

最近,我需要类似的东西(对某些日志事件起作用),但是我找不到干净的解决方案,主要是因为我还没有弄清楚如何以编程方式访问msbuild过程中的记录器.不过,我确实提出了这个建议,以适应您的问题,原理是:

Recently I needed something similar (acting on certain log events) but I couldn't find a clean solution mainly because I haven't figured out how to programmatically access the loggers in the msbuild process. I did come up with this though, adapted to your problem the principle is:

  • 安装自定义记录器以扫描警告
  • 构建
  • 如果发生警告,请设置静态标志
  • 具有自定义任务,检查该标志并在该标志处于打开状态时引发错误

听起来可能很难,但是代码很简单:

Might sound hard, but the code is simple enough:

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;

namespace Foo
{
  public static class Common
  {
    public static bool errorsOccurred = false;
  }

  public class ScanLogger : Logger
  {
    public override void Initialize( IEventSource eventSource )
    {
      eventSource.MessageRaised += ( s, e ) =>
        Common.errorsOccurred |= e.Message.Contains( "MSB3245" );
    }
  }

  public class CheckErrors : Task
  {
    public override bool Execute()
    {
      if( Common.errorsOccurred == false )
        return true;
      Log.LogError( "errorsOccurred = true" );
      return false;
    }
  }
}

以下是使用它的示例msbuild脚本:

Here's a sample msbuild script using it:

<UsingTask TaskName="Foo.CheckErrors" AssemblyFile="Foo.dll"/>

<Target Name="MyBuild">
  <Message Text="MSB3245"/> <!-- simulate the build warning -->
  <Foo.CheckErrors /> <!-- this will trigger an error -->
</Target>

您可以这样调用它:

msbuild /logger:Foo.dll my.proj

编辑我只是再次需要此文件,但再也找不到原始的dll或项目文件等-我认为仅将代码和最简单的构建指令存储在git中,并在需要时进行动态构建它可能更干净.因此,基本上将上面的代码存储在customlogger.cs文件中,然后在构建过程中的某个位置存储,然后再通过

edit I just needed this again but couldn't find the original dll nor project file etc anymore - I figured storing just the code and simplest build instructions in git and building it on the fly when needing it is probably cleaner. So basically store the code above in a file customlogger.cs and then somewhere in your build process, before effectively invoking msbuild with the custom logger, build it using

<Target Name="BuildCustomLoggerDll">
  <Csc Sources="$(MSBuildThisFileDirectory)customlogger.cs"
       References="System.dll;mscorlib.dll;Microsoft.Build.Framework.dll;Microsoft.Build.Utilities.v4.0.dll"
       TargetType="Library" OutputAssembly="$(MSBuildThisFileDirectory)CustomLogger.dll"/>
</Target>

更新:针对评论:今天再试一次,我不确定原始代码是否曾经真正起作用(嗯,它对显示的示例消息有效,但对于实际的警告MSB3245无效),因为它仅挂接消息事件,而ResolveAssemblyReference发出实际的警告事件,并且消息中通常不包含警告号.但这可以解决问题:

update In response to comments: trying this again today I'm not sure the original code actually ever worked (well, it does for the sample message shown but not for actual warning MSB3245), since it hooks message events only whereas the ResolveAssemblyReference emits an actual warning event and moreover the warning number isn't typically contained contained in the message. This does the trick though:

public class ScanLogger : Logger
{
  public override void Initialize( IEventSource eventSource )
  {
    eventSource.WarningRaised += ( s, e ) => Common.errorsOccurred |= e.Code == "MSB3245";
  }
}

这篇关于如何将MSB3245(无法解析参考)警告视为错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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