1

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

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

这只是一个警告,并且 nuget.exe 以状态码 0 退出,因此构建管道会继续并稍后失败,从而更难找到原因。

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

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

语境

在本地 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.
4

1 回答 1

1

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

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

我已经向 NuGet 团队报告了类似的问题,并收到了以下回复:

此消息是预期的,但它不应阻止您的 restore

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

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

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

于 2019-06-20T09:18:02.677 回答