简体   繁体   中英

Azure devops Cant connect Nuget Restore and VSBuild for Nuget Packages

I have a very basic build pipeline so far

# 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\1\new_packages"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

I am trying to the Nuget Restore task to get packages restored to ..\\new_packages directory and subsequently hoping that the VSBuild Task will get them from there and build the solution.

The Nuget Restore executes the following command (all in one line)

C:\Program Files (x86)\Microsoft Visual Studio\2019\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\5.4.0\x64\nuget.exe" 
  /p:RestoreSolutionDirectory="D:\a\1\s\\" 
  /p:RestoreConfigFile="D:\a\1\Nuget\tempNuGet_105.config" 
  /p:RestorePackagesPath="..\new_packages" 
  /p:SolutionDir="D:\a\1\s\\" 
  /p:SolutionName="XYZ"

Then VSBuild Task runs msbuild (all in one line)

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

The solution looks at the appropriate directory as specified in the /p:ReferencePath="d:\\a\\1\\new_packages .

But it still can't find any dlls for the Nuget packages and ultimately the build fails.

    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\1\s\XYZ.sln" on node 1 (default targets).
ValidateSolutionConfiguration:
  Building solution configuration "Release|Any CPU".
Project "D:\a\1\s\XYZ.sln" (1) is building "D:\a\1\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 Studio\2019\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 Studio\2019\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\1\s\ServiceLayer\InputOutputServiceLayer.csproj]
          For SearchPath "d:\a\1\new_packages".
          Considered "d:\a\1\new_packages\EPPlus.winmd", but it didn't exist.
          Considered "d:\a\1\new_packages\EPPlus.dll", but it didn't exist.
          Considered "d:\a\1\new_packages\EPPlus.exe", but it didn't exist.
          For SearchPath "{HintPathFromItem}".
          Considered "D:\a\1\s\packages\EPPlus.4.5.3.2\lib\net40\EPPlus.dll", but it didn't exist.

Technically, I know we're not supposed to hardcode a /p:ReferencePath=.. do this but I'm just trying to get it to work.

Without that VSBuild Task seems to have no idea where to even look.

It looks at

 ResolveAssemblyReferences:
  Primary reference "EPPlus, Version=4.5.3.2, Culture=neutral, PublicKeyToken=ea159fdaa78159a1, processorArchitecture=MSIL".
##[warning]C:\Program Files (x86)\Microsoft Visual Studio\2019\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 Studio\2019\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\1\s\ServiceLayer\InputOutputServiceLayer.csproj]
          For SearchPath "..\d:\a\1\a".
          Considered "..\d:\a\1\a\EPPlus.winmd", but it didn't exist.
          Considered "..\d:\a\1\a\EPPlus.dll", but it didn't exist.
          Considered "..\d:\a\1\a\EPPlus.exe", but it didn't exist.
          For SearchPath "{HintPathFromItem}".
          Considered "d:\a\1\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.

There is a similar question using the old way of using the pipeline

MsBuild does not find restored NuGet-Packages on Visual Studio Online

Their solution (though hacky) doesn't work for me either.

If I remove the EPPlus package, the next package Entity Framework will produce the same errors.
If I remove Entity Framework the next package will produce the same errors.

I'm just trying to find a real solution for this which doesn't involve these hardcoding hacks (..and works, obviously)

EDIT:

Working, with quirks thanks to Leo Liu-MSFT .
My answer is below that one.

Azure devops Cant connect Nuget Restore and VSBuild for Nuget Packages

I am afraid you could not make it work unless you modify the HintPath in your project file.

When you change the nuget packages restored to ..\\new_packages directory, after restore, the package in the new directory should have following structural levels:

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

The dll file is stored in your new directory in the form of a nuget package, rather than directly in the new directory.

So, when you specify the /p:ReferencePath="d:\\a\\1\\new_packages" during building the project, VS will find the dll file directly from the ReferencePath . That the reason why you get the error message is:

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

Actually, the dll path should be d:\\a\\1\\new_packages\\EPPlus.4.5.3.2\\lib\\net40 . Since this path is related to both the name and version of the package, we could not use one parameter to specify the path of multiple dlls .

In this case, we have to modify the HintPath in the project file.

The correct process is that change the repositoryPath in the nuget.config file to change the nuget package directory on your local:

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

then use command line Update-Package -reinstall in the Package Manager Console to force reinstall the package references into project on your local , it will update the HintPath for all packages, then submit the changes to the source control. Then we could use the Nuget Restore task to get packages restored to ..\\new_packages directory

Hope this helps.

I got it to work but with a few quirks (and features?) and few pending questions.

Please see /users/7460777/leo-liu-msft above to get some context as to why some of these steps are taken.

As Leo mentioned this has to do with the HintPath not being updated properly in the projects that use packages.config

To avoid this issue altogether,
migrate as many projects as possible to the <PackageReference /> instead of using packages.config . To do this, right click on the References inside each project, there should be an option to do so.
Some projects (Eg -> in my case, my asp.net (.net Framework) MVC Web Client) can't be migrated.
(..I'll add more details i found on this later)
UPDATE:
( https://devblogs.microsoft.com/nuget/migrate-packages-config-to-package-reference/ )
( https://github.com/NuGet/Home/issues/5877 )


Next Steps:--

1) On the top level folder where I have the .sln file, I created the following nuget.config file.

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

Since my packages directory is here.

2) My updated build.yml is below.

# 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) The following step caused everything to work, but Im still unsure why exactly.
I deleted everything inside the packages folder and basically just pushed those deletions upstream. Nuget restore Task did a restore and the VSBuild task succeeded.


Things I'm still unsure of --

1) Why is a temporary Nuget.config file created even though I clearly said where it exists?

Saving NuGet.config to a temporary config file.
[command]C:\hostedtoolcache\windows\NuGet\5.4.0\x64\nuget.exe sources Add -NonInteractive -Name NuGetOrg -Source https://api.nuget.org/v3/index.json -ConfigFile d:\a\1\Nuget\tempNuGet_140.config
Package source with Name: NuGetOrg added successfully.
[command]C:\hostedtoolcache\windows\NuGet\5.4.0\x64\nuget.exe restore d:\a\1\s\XYZ.sln -PackagesDirectory .\packages -Verbosity Detailed -NonInteractive -ConfigFile d:\a\1\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 Studio\2019\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 Studio\2019\Enterprise\MSBuild\Current\bin\msbuild.exe 
"C:\Users\VssAdministrator\AppData\Local\Temp\NuGetScratch\0eie04ka.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\5.4.0\x64\nuget.exe" /p:RestoreSolutionDirectory="d:\a\1\s\\" /p:RestoreConfigFile="d:\a\1\Nuget\tempNuGet_140.config" /p:RestorePackagesPath=".\packages" /p:SolutionDir="d:\a\1\s\\" /p:SolutionName="XYZ"

2) Why does deleting packages make it work while committing the packages make it unable to find any dll files even though it looks at the correct places?

Primary reference "NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL".
##[warning]C:\Program Files (x86)\Microsoft Visual Studio\2019\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 Studio\2019\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\1\s\XYZ_WebClient\XYZ_WebClient.csproj]
          For SearchPath "{HintPathFromItem}".
          Considered "D:\a\1\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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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