title: Azure devops Cant connect Nuget Restore and VSBuild for Nuget Packages
body: <p>I have a very basic build pipeline so far</p>
  
  <pre><code># 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)'
  </code></pre>
  
  <p>I am trying to the Nuget Restore task to get packages restored to <code>..\new_packages</code> directory and subsequently hoping that the VSBuild Task will get them from there and build the solution.</p>
  
  <p>The Nuget Restore executes the following command (all in one line)</p>
  
  <pre><code>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"
  </code></pre>
  
  <p>Then VSBuild Task runs msbuild (all in one line)</p>
  
  <pre><code>##[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"
  </code></pre>
  
  <p>The solution looks at the appropriate directory as specified in the <code>/p:ReferencePath="d:\a\1\new_packages</code>. </p>
  
  <p>But it still can't find any dlls for the Nuget packages and ultimately the build fails.</p>
  
  <pre><code>    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.
  </code></pre>
  
  <p>Technically, I know we're not supposed to hardcode a <code>/p:ReferencePath=..</code> do this but I'm just trying to get it to work.  </p>
  
  <p>Without that VSBuild Task seems to have no idea where to even look. </p>
  
  <p>It looks at </p>
  
  <pre><code> 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.
  </code></pre>
  
  <p>There is a similar question using the old way of using the pipeline </p>
  
  <p><a href="https://stackoverflow.com/questions/49287852/msbuild-does-not-find-restored-nuget-packages-on-visual-studio-online">MsBuild does not find restored NuGet-Packages on Visual Studio Online</a></p>
  
  <p>Their solution (though hacky) doesn't work for me either.  </p>
  
  <p>If I remove the EPPlus package, the next package <code>Entity Framework</code> will produce the same errors.<br>
  If I remove <code>Entity Framework</code> the next package will produce the same errors.  </p>
  
  <p>I'm just trying to find a real solution for this which doesn't involve these hardcoding hacks (..and works, obviously)</p>
  
  <p><strong>EDIT:</strong> </p>
  
  <p>Working, with quirks thanks to <code>Leo Liu-MSFT</code>.<br>
  My answer is below that one.</p>