简体   繁体   English

Azure 管道 Nuget 恢复失败 MSB4226

[英]Azure Pipelines Nuget Restore Failing MSB4226

I am trying to set up an Azure pipeline for a project but I am getting a failure on the nuget restore command in my yml.我正在尝试为项目设置 Azure 管道,但在我的 yml 中的 nuget 恢复命令失败。 Below is the full error:以下是完整的错误:

[error]The nuget command failed with exit code(1) and error(D:\a\1\s\UDesign\Backup\UDesign\UDesign.csproj(190,11): error MSB4226: The imported project "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" was not found. Also, tried to find "Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" in the fallback search path(s) for $(MSBuildExtensionsPath32) - "C:\Program Files (x86)\MSBuild". These search paths are defined in "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\msbuild.exe.Config". Confirm that the path in the declaration is correct, and that the file exists on disk in one of the search paths.) ##[error]Packages failed to restore [错误]nuget 命令失败,退出代码 (1) 和错误(D:\a\1\s\UDesign\Backup\UDesign\UDesign.csproj(190,11): 错误 MSB4226: 导入的项目“C:\未找到 Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets。另外,试图找到“Microsoft\VisualStudio\v10.0\ WebApplications\Microsoft.WebApplication.targets”在 $(MSBuildExtensionsPath32) 的后备搜索路径中 - “C:\Program Files (x86)\MSBuild”。这些搜索路径在“C:\Program Files (x86)”中定义\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\msbuild.exe.Config”。确认声明中的路径是正确的,并且该文件存在于磁盘上的任一搜索路径中。)##[错误]包无法恢复

Pretty much every post on this error is on the Build command in the YML but this is failing on the Nuget Restore:几乎所有关于此错误的帖子都在 YML 中的 Build 命令上,但这在 Nuget 恢复中失败:

Pipelines failure image管道故障图像

YML below: YML 下面:

# .NET Desktop
# Build and run tests for .NET Desktop or Windows classic desktop solutions.
# Add steps that publish symbols, save build artifacts, and more:
# https://docs.microsoft.com/azure/devops/pipelines/apps/windows/dot-net

trigger:
- master

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    command: 'restore'
    restoreSolution: '$(solution)'
    feedsToUse: 'select'
    vstsFeed: 'MyFeed'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:OutputPath="$(Build.BinariesDirectory)\$(Build.BuildID)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'
    restoreNugetPackages: true
    msbuildArchitecture: 'x64'

- task: ArchiveFiles@2
  inputs:
    rootFolderOrFile: '$(Build.BinariesDirectory)\$(Build.BuildId)'
    includeRootFolder: false
    archiveType: 'zip'
    archiveFile: '$(Build.ArtifactStagingDirectory)/$(Build.BuildId).zip'
    replaceExistingArchive: true

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

I am not sure if the issue is with the config somehow or my NuGet packages, I can't find much on this issue.我不确定问题出在配置上还是我的 NuGet 包上,我在这个问题上找不到太多信息。

Could you please try this你能试试这个吗

Note: You need to replace the source and target paths according to your project.注意:您需要根据您的项目替换源路径和目标路径。

variables:
- name: BuildParameters.RestoreBuildProjects
  value: '**/*.csproj'
- name: BuildParameters.TestProjects
  value: '**/*[Tt]ests/*.csproj'
trigger:
  branches:
    include:
    - refs/heads/master
name: $(date:yyyyMMdd)$(rev:.r)
jobs:
- job: Job_1
  displayName: Agent job 1
  pool:
    vmImage: windows-2019
  steps:
  - checkout: self
  - task: DotNetCoreCLI@2
    displayName: Restore
    inputs:
      command: restore
      projects: $(BuildParameters.RestoreBuildProjects)
  - task: DotNetCoreCLI@2
    displayName: Build
    inputs:
      projects: $(BuildParameters.RestoreBuildProjects)
      arguments: --configuration $(BuildConfiguration)
  - task: DotNetCoreCLI@2
    displayName: Test
    enabled: False
    inputs:
      command: test
      projects: $(BuildParameters.TestProjects)
      arguments: --configuration $(BuildConfiguration)
  - task: DotNetCoreCLI@2
    displayName: Publish
    inputs:
      command: publish
      publishWebProjects: True
      projects: $(BuildParameters.RestoreBuildProjects)
      arguments: --configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory)
      zipAfterPublish: True
  - task: PublishBuildArtifacts@1
    displayName: Publish Artifact
    condition: succeededOrFailed()
    inputs:
      PathtoPublish: $(build.artifactstagingdirectory)
      TargetPath: '\\my\share\$(Build.DefinitionName)\$(Build.BuildNumber)'
...

Actual issue实际问题

The problem is actually unrelated to NuGet, it's just that NuGet is the first thing in your pipeline that tries to evaluate MSBuild files.该问题实际上与 NuGet 无关,只是 NuGet 是管道中尝试评估 MSBuild 文件的第一件事。 If you look carefully at the error:如果您仔细查看错误:

error MSB4226: The imported project "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" was not found.错误 MSB4226:找不到导入的项目“C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets”。

You can see that the error is that MSBuild can't find Microsoft.WebApplication.targets .可以看到报错是 MSBuild can't find Microsoft.WebApplication.targets What does this mean?这是什么意思? I'm not 100% confident, but I do have high confidence that this means you're using a "self hosted" CI agent, not a Microsoft hosted agent , and whoever set up the machine installed Visual Studio 2019 without installing the ASP.NET workload.我不是 100% 有信心,但我非常有信心,这意味着您使用的是“自托管”CI 代理,而不是Microsoft 托管代理,并且任何设置机器的人都安装了 Visual Studio 2019 而没有安装 ASP.NET 工作负载. Therefore, whoever sets up your CI agents should install the "ASP.NET and web development" workload.因此,任何设置 CI 代理的人都应该安装“ASP.NET 和 web 开发”工作负载。

Suggestion 1: CI agent should use BuildTools SKU建议 1:CI 代理应使用 BuildTools SKU

If you look at VS's release and build history , you see for every version, under "Links to Installer", there's a "BuildTools" link.如果您查看VS 的发布和构建历史,您会看到每个版本的“安装程序链接”下都有一个“构建工具”链接。 This is a subset of Visual Studio without all the GUI stuff, meant for CI agents specifically.这是 Visual Studio 的一个子集,没有所有的 GUI 东西,专门用于 CI 代理。 I don't think the BuildTools SKU has workload options, it just installs all the build tools necessary for all workloads.我认为 BuildTools SKU 没有工作负载选项,它只是安装所有工作负载所需的所有构建工具。

Only in rare circumstances is the "full" Visual Studio required on a CI agent, so unless you know you're in that circumstance, I suggest installing the BuildTools on your CI agent instead of Enterprise or Professional.只有在极少数情况下,CI 代理才需要“完整”的 Visual Studio,所以除非您知道自己处于这种情况,否则我建议在您的 CI 代理上安装 BuildTools,而不是在 Enterprise 或 Professional 上安装。

Suggestion 2: Don't use NuGetCommand建议 2:不要使用NuGetCommand

There's two reasons.有两个原因。 Firstly, NuGetCommand is a task that uses NuGet.exe.首先, NuGetCommand是一个使用 NuGet.exe 的任务。 The problem with NuGet.exe is that when you're using PackageReference , rather than packages.config , is that there are times when NuGet and the build tools (msbuild's props/targets) change file format for the temporary files. NuGet.exe 的问题在于,当您使用PackageReference而不是packages.config时,有时 NuGet 和构建工具(msbuild 的道具/目标)会更改临时文件的文件格式。 If you don't keep the version of NuGet.exe aligned with the version of MSBuild/Visual Studio, you might get build errors.如果您不将 NuGet.exe 的版本与 MSBuild/Visual Studio 的版本保持一致,则可能会出现构建错误。 So, it's much better to use dotnet restore , or if you can't use the dotnet cli, then use msbuild -t:restore .因此,最好使用dotnet restore ,或者如果您不能使用 dotnet cli,则使用msbuild -t:restore This way you're always using the exact version of NuGet that is designed for the version of dotnet/msbuild installed, and you'll never have to think about NuGet-MSBuild version problems again.这样,您始终使用专为安装的 dotnet/msbuild 版本设计的 NuGet 的确切版本,并且您永远不必再考虑 NuGet-MSBuild 版本问题。

Secondly, someone from the Azure DevOps team told my team that they want to deprecate these "heavy" tasks, and suggest customers call commands directly in a script task .其次, Azure DevOps 团队的某个人告诉我的团队,他们想弃用这些“繁重”的任务,并建议客户直接在脚本任务中调用命令 In particular, the way they implemented these features will break NuGet's new Package Source Mapping feature.特别是,他们实现这些功能的方式将破坏 NuGet 的新 Package 源映射功能。

If your solution contains only SDK style projects, then I suggest you use:如果您的解决方案仅包含 SDK 风格的项目,那么我建议您使用:

- task: NuGetAuthenticate
  displayName: Set up NuGet authentication
- script: dotnet restore $(solution)
  displayName: Restore

If your solution contains any "legacy" (non-SDK style) projects, then instead of using the dotnet CLI, use - script: msbuild -t:restore $(solution) .如果您的解决方案包含任何“旧版”(非 SDK 样式)项目,则不要使用 dotnet CLI,而是使用- script: msbuild -t:restore $(solution)

Suggestion 3: Don't use VSBuild建议 3:不要使用 VSBuild

You're using the task VSBuild to run your build.您正在使用任务VSBuild来运行您的构建。 This is a task that uses devenv.com to build the project.这是使用devenv.com构建项目的任务。 While devenv does have command line parameters to build from the command line, msbuild.exe is the "official" tool to do command line builds.虽然 devenv 确实具有从命令行构建的命令行参数, msbuild.exe是进行命令行构建的“官方”工具。 Again, unless you know you're in a special circumstance that really needs to be built via devenv /build , I suggest you use the dotnet CLI or MSBuild instead.同样,除非您知道自己处于确实需要通过devenv /build的特殊情况,否则我建议您改用dotnet CLI 或MSBuild As previously mentioned, the Azure DevOps team apparently want to deprecate the "heavy" tasks, so I suggest you use a script and call dotnet or msbuild directly.如前所述,Azure DevOps 团队显然希望弃用“繁重”任务,因此我建议您使用脚本并直接调用dotnetmsbuild

- script: msbuild -t:build $(solution)
  displayName: Build solution

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM