【问题标题】:NuGet restore - how to make it treat warnings as errors?NuGet 还原 - 如何使其将警告视为错误?
【发布时间】:2019-11-02 16:54:46
【问题描述】:

由于缺少 .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 的项目,也包含面向使用 PackageReference 的 .NET Core 的项目。

构建代理不在我的控制之下。有些安装了正确的 SDK 版本,有些没有。我使用 .NET Core SDK Installer 任务来安装相应的 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.

【问题讨论】:

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


    【解决方案1】:

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

    恐怕我们现在不能让它将警告视为错误,因为这种行为符合设计

    我已经向 NuGet 团队举报a similar issue,并收到如下回复:

    此消息是正常的,但它不应阻止您的恢复

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

    此外,TreatWarningsAsErrors 属性设置为项目范围级别的一个/所有 NuGet 警告,但当 MSBuild/VS 开始读取项目文件 .csproj 时将引发上述警告。这就是属性 TreatWarningsAsErrors 无法正常工作的原因,即使它已设置。

    由于构建代理不在你的控制之下,我们无法直接在代理上安装所需的.NET Core SDK版本,在代理中添加相应的能力,在构建管道中添加对该能力的需求。看来我们必须在microsoft/msbuild#2471 下添加评论,以询问我们是否可以使用扳手来设置信息是错误或警告。

    【讨论】:

    • 感谢您的回答!我没有 GitHub 帐户,我一直以读者的身份参与其中,但也许是时候改变这一点了。我不知道你最后一句中的“扳手”是什么意思,但我想我明白了整体的意思,一旦我发现它足够重要,值得付出努力,我就能推动它。很遗憾听到 NuGet 还不够成熟,无法提供这样的选项。
    猜你喜欢
    • 1970-01-01
    • 2011-03-15
    • 2011-01-05
    • 2010-11-05
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 2022-08-03
    • 1970-01-01
    相关资源
    最近更新 更多