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

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

问题描述

我的问题基于 另一个,但我想做相反的:告诉 msbuild 将警告视为错误,而不是抑制特定的 msbuild 警告.

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

edit 我只是再次需要它,但再也找不到原始 dll 或项目文件等 - 我想将代码和最简单的构建指令存储在 git 中,并在需要时即时构建它可能更清洁.因此,基本上将上面的代码存储在文件 customlogger.cs 中,然后在构建过程中的某个位置,在使用自定义记录器有效调用 msbuild 之前,使用

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天全站免登陆