Azure devops 无法为 Nuget 包连接 Nuget Restore 和 MSBuild

Azure devops Cant connect Nuget Restore and VSBuild for Nuget Packages

到目前为止我有一个非常基本的构建管道

# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4

trigger:
- testing-build-yml-1

pool:
  vmImage: 'windows-latest'

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

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'
    # packagesDirectory: '..\packages'
    restoreDirectory: '..\new_packages'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    #msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)" /p:ReferencePath="d:\a\new_packages"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

我正在尝试 Nuget Restore 任务将包恢复到 ..\new_packages 目录,随后希望 VSBuild 任务将从那里获取它们并构建解决方案。

Nuget Restore 执行以下命令(全部在一行中)

C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\MSBuild\Current\bin\msbuild.exe 
  "C:\Users\VssAdministrator\AppData\Local\Temp\NuGetScratch\jgtjjpe0.nac.nugetinputs.targets" 
  /t:GenerateRestoreGraphFile 
   /nologo 
   /nr:false 
   /v:q 
  /p:NuGetRestoreTargets="C:\Users\VssAdministrator\AppData\Local\Temp\NuGetScratch\whba0xm4.c0f.nugetrestore.targets" 
  /p:RestoreUseCustomAfterTargets="True" /p:RestoreTaskAssemblyFile="C:\hostedtoolcache\windows\NuGet.4.0\x64\nuget.exe" 
  /p:RestoreSolutionDirectory="D:\a\s\" 
  /p:RestoreConfigFile="D:\a\Nuget\tempNuGet_105.config" 
  /p:RestorePackagesPath="..\new_packages" 
  /p:SolutionDir="D:\a\s\" 
  /p:SolutionName="XYZ"

然后 VSBuild Task 运行 msbuild(全部在一行中)

##[command]"C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\MSBuild\Current\Bin\msbuild.exe" 
 "D:\a\s\XYZ.sln" 
   /nologo 
   /nr:false 
   /dl:CentralLogger,
 "D:\a\_tasks\VSBuild_71a9a2d3-a98a-4caa-96ab-affca411ecda.161.2\ps_modules\MSBuildHelpers\Microsoft.TeamFoundation.DistributedTask.MSBuild.Logger.dll";
 "RootDetailId=e5869970-8e7b-4a06-a8fd-f0662f639a6d|SolutionDir=D:\a\s"*ForwardingLogger,"D:\a\_tasks\VSBuild_71a9a2d3-a98a-4caa-96ab-affca411ecda.161.2\ps_modules\MSBuildHelpers\Microsoft.TeamFoundation.DistributedTask.MSBuild.Logger.dll"
 /p:DeployOnBuild=true 
 /p:WebPublishMethod=Package 
 /p:PackageAsSingleFile=true 
 /p:SkipInvalidConfigurations=true 
 /p:PackageLocation="D:\a\a" 
 /p:ReferencePath="d:\a\new_packages" 
 /p:platform="Any CPU" 
 /p:configuration="Release" 
 /p:VisualStudioVersion="16.0" 
 /p:_MSDeployUserAgent="VSTS_607486bc-2d64-463d-9a7f-0cf06fe82263_build_16_0"

解决方案会查看 /p:ReferencePath="d:\a\new_packages 中指定的相应目录。

但它仍然找不到 Nuget 包的任何 dll,最终构建失败。

    Building the projects in this solution one at a time. To enable parallel build, please add the "-m" switch.
Build started 1/9/2020 10:11:18 PM.
Project "D:\a\s\XYZ.sln" on node 1 (default targets).
ValidateSolutionConfiguration:
  Building solution configuration "Release|Any CPU".
Project "D:\a\s\XYZ.sln" (1) is building "D:\a\s\ServiceLayer\InputOutputServiceLayer.csproj" (2) on node 1 (default targets).
PrepareForBuild:
  Creating directory "bin\Release\".
  Creating directory "obj\Release\".
ResolveAssemblyReferences:
  Primary reference "EPPlus, Version=4.5.3.2, Culture=neutral, PublicKeyToken=ea159fdaa78159a1, processorArchitecture=MSIL".
##[warning]C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): Warning MSB3245: Could not resolve this reference. Could not locate the assembly "EPPlus, Version=4.5.3.2, Culture=neutral, PublicKeyToken=ea159fdaa78159a1, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "EPPlus, Version=4.5.3.2, Culture=neutral, PublicKeyToken=ea159fdaa78159a1, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. [D:\a\s\ServiceLayer\InputOutputServiceLayer.csproj]
          For SearchPath "d:\a\new_packages".
          Considered "d:\a\new_packages\EPPlus.winmd", but it didn't exist.
          Considered "d:\a\new_packages\EPPlus.dll", but it didn't exist.
          Considered "d:\a\new_packages\EPPlus.exe", but it didn't exist.
          For SearchPath "{HintPathFromItem}".
          Considered "D:\a\s\packages\EPPlus.4.5.3.2\lib\net40\EPPlus.dll", but it didn't exist.

从技术上讲,我知道我们不应该对 /p:ReferencePath=.. 进行硬编码,但我只是想让它工作。

没有那个 VSBuild Task 似乎不知道去哪里找。

它看着

 ResolveAssemblyReferences:
  Primary reference "EPPlus, Version=4.5.3.2, Culture=neutral, PublicKeyToken=ea159fdaa78159a1, processorArchitecture=MSIL".
##[warning]C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): Warning MSB3245: Could not resolve this reference. Could not locate the assembly "EPPlus, Version=4.5.3.2, Culture=neutral, PublicKeyToken=ea159fdaa78159a1, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "EPPlus, Version=4.5.3.2, Culture=neutral, PublicKeyToken=ea159fdaa78159a1, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. [d:\a\s\ServiceLayer\InputOutputServiceLayer.csproj]
          For SearchPath "..\d:\a\a".
          Considered "..\d:\a\a\EPPlus.winmd", but it didn't exist.
          Considered "..\d:\a\a\EPPlus.dll", but it didn't exist.
          Considered "..\d:\a\a\EPPlus.exe", but it didn't exist.
          For SearchPath "{HintPathFromItem}".
          Considered "d:\a\s\packages\EPPlus.4.5.3.2\lib\net40\EPPlus.dll", but it didn't exist.
          For SearchPath "{TargetFrameworkDirectory}".
          Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\EPPlus.winmd", but it didn't exist.

使用管道的旧方法也有类似的问题

他们的解决方案(虽然很老套)也不适合我。

如果我删除 EPPlus 包,下一个包 Entity Framework 将产生相同的错误。
如果我删除 Entity Framework 下一个包将产生相同的错误。

我只是想为此找到一个真正的解决方案,它不涉及这些硬编码黑客(..显然有效)

编辑:

工作,由于 Leo Liu-MSFT 而有怪癖。
我的答案在那个下面。

Azure devops Cant connect Nuget Restore and VSBuild for Nuget Packages

除非您修改项目文件中的 HintPath,否则恐怕无法使它工作。

当您更改还原到..\new_packages目录的nuget包时,还原后,新目录中的包应具有以下结构级别:

d:\a\new_packages\EPPlus.4.5.3.2\lib\net40\EPPlus.dll

dll文件以nuget包的形式存放在你新建的目录下,而不是直接存放在新建目录下

因此,当您在构建项目时指定/p:ReferencePath="d:\a\new_packages",VS 将直接从ReferencePath 中找到dll 文件。您收到错误消息的原因是:

Considered "d:\a\new_packages\EPPlus.dll"

实际上,dll 路径应该是 d:\a\new_packages\EPPlus.4.5.3.2\lib\net40。由于这个路径与包名和版本都相关,所以我们不能用一个参数指定多个dlls.

的路径

这种情况下,我们要修改工程文件中的HintPath

正确的过程是改变nuget.config文件中的repositoryPath来改变你本地的nuget包目录:

Is it possible to change the location of packages for NuGet?

然后在包管理器控制台中使用命令行 Update-Package -reinstall 强制将包引用重新安装到本地 上的项目 中,它将更新 HintPath对于所有包,然后将更改提交到源代码管理。然后我们可以使用 Nuget Restore 任务将包恢复到 ..\new_packages 目录

希望这对您有所帮助。

我让它工作了,但有一些怪癖(和功能?)和一些悬而未决的问题。

请参阅上面的 /users/7460777/leo-liu-msft 以了解为什么要采取这些步骤。

正如 Leo 提到的,这与 HintPath 在使用 packages.config

的项目中未正确更新有关

为了完全避免这个问题,
将尽可能多的项目迁移到 <PackageReference /> 而不是使用 packages.config。为此,请右键单击每个项目中的 References,应该有一个选项可以这样做。
某些项目(例如 -> 在我的例子中,我的 asp.net (.net Framework) MVC Web Client)无法迁移。
(..我稍后会添加我发现的更多细节)
更新:
(https://devblogs.microsoft.com/nuget/migrate-packages-config-to-package-reference/)
(https://github.com/NuGet/Home/issues/5877)


后续步骤:--

1) 在包含 .sln 文件的顶级文件夹中,我创建了以下 nuget.config 文件。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <config>
    <add key="repositoryPath" value=".\packages" />
  </config>
</configuration>

因为我的 packages 目录在这里。

2) 下面是我更新的 build.yml

# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4

trigger:
- testing-build-yml

pool:
  vmImage: 'windows-latest'

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

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'
    packagesdirectory: '.\packages'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

3) 以下步骤使一切正常,但我仍然不确定具体原因。
我删除了 packages 文件夹中的所有内容,基本上只是将这些删除内容推到了上游。 Nuget 恢复任务进行了恢复,VSBuild 任务成功。


我仍然不确定的事情 --

1) 为什么会创建临时 Nuget.config 文件,即使我清楚地说明了它存在于何处?

Saving NuGet.config to a temporary config file.
[command]C:\hostedtoolcache\windows\NuGet.4.0\x64\nuget.exe sources Add -NonInteractive -Name NuGetOrg -Source https://api.nuget.org/v3/index.json -ConfigFile d:\a\Nuget\tempNuGet_140.config
Package source with Name: NuGetOrg added successfully.
[command]C:\hostedtoolcache\windows\NuGet.4.0\x64\nuget.exe restore d:\a\s\XYZ.sln -PackagesDirectory .\packages -Verbosity Detailed -NonInteractive -ConfigFile d:\a\Nuget\tempNuGet_140.config
NuGet Version: 5.4.0.6315
MSBuild auto-detection: using msbuild version '16.4.0.56107' from 'C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\MSBuild\Current\bin'. Use option -MSBuildVersion to force nuget to use a specific version of MSBuild.
MSBuild P2P timeout [ms]: 120000
C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\MSBuild\Current\bin\msbuild.exe 
"C:\Users\VssAdministrator\AppData\Local\Temp\NuGetScratch[=12=]eie04ka.4ch.nugetinputs.targets" /t:GenerateRestoreGraphFile /nologo /nr:false /v:q /p:NuGetRestoreTargets="C:\Users\VssAdministrator\AppData\Local\Temp\NuGetScratch\y3ui4hhz.gfr.nugetrestore.targets" /p:RestoreUseCustomAfterTargets="True" /p:RestoreTaskAssemblyFile="C:\hostedtoolcache\windows\NuGet.4.0\x64\nuget.exe" /p:RestoreSolutionDirectory="d:\a\s\" /p:RestoreConfigFile="d:\a\Nuget\tempNuGet_140.config" /p:RestorePackagesPath=".\packages" /p:SolutionDir="d:\a\s\" /p:SolutionName="XYZ"

2) 为什么删除包后它可以正常工作,而提交包后即使查找了正确的位置也无法找到任何 dll 文件?

Primary reference "NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL".
##[warning]C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): Warning MSB3245: Could not resolve this reference. Could not locate the assembly "NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors.
C:\Program Files (x86)\Microsoft Visual Studio19\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(2106,5): warning MSB3245: Could not resolve this reference. Could not locate the assembly "NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL". Check to make sure the assembly exists on disk. If this reference is required by your code, you may get compilation errors. [D:\a\s\XYZ_WebClient\XYZ_WebClient.csproj]
          For SearchPath "{HintPathFromItem}".
          Considered "D:\a\s\packages\NLog.4.6.7\lib\net45\NLog.dll", but it didn't exist.
          For SearchPath "{TargetFrameworkDirectory}".
          Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\NLog.winmd", but it didn't exist.
          Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\NLog.dll", but it didn't exist.
          Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\NLog.exe", but it didn't exist.
          Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\Facades\NLog.winmd", but it didn't exist.
          Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\Facades\NLog.dll", but it didn't exist.
          Considered "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\Facades\NLog.exe", but it didn't exist.