NuGet Restore-如何使其将警告视为错误?

时间:2019-06-19 21:47:06

标签: nuget warnings tfsbuild nuget-package-restore warning-level

由于缺少.NET Core SDK,程序包还原仅部分成功,并且省略了SDK项目。

  

警告:读取msbuild项目信息时出错,请确保您的输入解决方案或项目文件有效。 NETCore和UAP项目将被跳过,仅packages.config文件将被还原。

这只是一个警告,并且nuget.exe以状态代码0退出,因此生成管道继续进行并在以后失败,从而更加难以找到原因。

因为这是我们环境中的常见问题,所以我希望将此警告视为错误。如果将其他警告也视为错误,那是完全可以的(甚至是理想的)。

我知道NuGet reads MSBuild properties TreatWarningsAsErrors and WarningsAsErrors,但是我还没有从命令行使用它们。我尝试将NUGET_RESTORE_MSBUILD_ARGS环境变量设置为/p:TreatWarningsAsErrors=true,但是即使该选项已传递给内部MSBuild调用,它也不会影响nuget.exe打印的警告。我没有找到其他合适的CLI optionsenvironment variables

上下文

在本地Azure Pipelines构建中,我运行NuGet任务来还原解决方案的程序包。该解决方案既包含针对.NET Framework的项目和使用packages.config的项目,也包含针对针对.NET Core的使用PackageReference的项目。

构建代理不受我的控制。有些安装了正确的SDK版本,有些则没有。我使用.NET Core SDK安装程序任务来安装适当的SDK。

由于解决方案中的构建定义更改或SDK更新,构建可能会中断。在那种情况下,错误消息是晦涩的,因为它是来自VS Build而不是来自软件包还原构建步骤,该步骤的根本原因在哪里,但发出绿色光芒。

在内部,NuGet任务(版本2. *)执行nuget.exe(当前为5.0.2版),该程序又执行MSBuild以执行与PackageReference相关的部分工作。 MSBuild失败,导致抛出NuGet.CommandLine.ExitCodeException,但将其捕获,记录并丢弃。在上面提到的Warning_ReadingProjectsFailed警告上方,将显示堆栈跟踪:

NuGet.CommandLine.ExitCodeException: Exception of type 'NuGet.CommandLine.ExitCodeException' was thrown.
    at NuGet.CommandLine.MsBuildUtility.<GetProjectReferencesAsync>d__6.MoveNext()
 --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at NuGet.CommandLine.RestoreCommand.<GetDependencyGraphSpecAsync>d__52.MoveNext()
 --- End of stack trace from previous location where exception was thrown ---
    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
    at NuGet.CommandLine.RestoreCommand.<DetermineInputsFromMSBuildAsync>d__47.MoveNext()

catch子句顶部的评论说:

                // At this point reading the project has failed, to keep backwards
                // compatibility this should warn instead of error if
                // packages.config files exist, but no project.json files.
                // This will skip NETCore projects which is a problem, but there is
                // not a good way to know if they exist, or if this is an old type of
                // project that the targets file cannot handle.

1 个答案:

答案 0 :(得分:1)

  

NuGet还原-如何使其将警告视为错误?

恐怕我们暂时无法将警告视为错误,因为这种行为是设计的

我已经向NuGet小组报告了a similar issue,并且收到了以下答复:

  

此消息是正常现象,但它不会阻止您还原

     

将来,一旦msbuild提供了一种跳过项目的方法,   缺少目标,此消息将消失:microsoft/msbuild#2471

此外,为项目级别的一个/所有NuGet警告设置了属性TreatWarningsAsErrors,但是当MSBuild / VS开始读取项目文件.csproj时,将抛出以上警告。这就是属性TreatWarningsAsErrors即使设置了也不起作用的原因。

由于构建代理不受您的控制,因此我们无法在代理上直接安装所需的.NET Core SDK版本,不能向代理添加相应的功能,也不能向构建管道添加对功能的需求。似乎我们必须在microsoft/msbuild#2471下添加评论,以询问是否可以用扳手设置信息是错误还是警告。