由于缺少 .NET Core SDK,包还原仅部分成功并省略了 SDK 项目。
警告:读取 msbuild 项目信息时出错,请确保您输入的解决方案或项目文件有效。NETCore 和 UAP 项目将被跳过,只会还原 packages.config 文件。
这只是一个警告,并且 nuget.exe 以状态码 0 退出,因此构建管道会继续并稍后失败,从而更难找到原因。
因为这是我们环境中的常见问题,所以我希望将此警告视为错误。如果其他警告也被视为错误,那是完全可以的(甚至是可取的)。
我知道NuGet 读取 MSBuild 属性TreatWarningsAsErrors
和WarningsAsErrors
,但我没有设法从命令行使用它们。我尝试将 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.