Tuesday 7 April 2020

NuGet Restore failing in Azure with Error parsing solution file

I recently came across a problem where builds were failing in Azure DevOps when performing a NuGet restore for the solution.

The error details were:

2020-04-07T08:05:03.8535680Z [command]C:\hostedtoolcache\windows\NuGet\4.1.0\x64\nuget.exe restore d:\a\1\s\MyProject\MyProject.sln -Verbosity Detailed -NonInteractive -ConfigFile d:\a\1\Nuget\tempNuGet_41515.config
2020-04-07T08:05:05.3883943Z NuGet Version:
2020-04-07T08:05:05.3886378Z MSBuild auto-detection: using msbuild version '' 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.
2020-04-07T08:05:05.4539665Z System.AggregateException: One or more errors occurred. ---> NuGet.CommandLine.CommandLineException: Error parsing solution file at d:\a\1\s\MyProject\MyProject.sln: Exception has been thrown by the target of an invocation.
2020-04-07T08:05:05.4540531Z at NuGet.CommandLine.MsBuildUtility.GetAllProjectFileNamesWithMsBuild(String solutionFile, String msbuildPath)
2020-04-07T08:05:05.4541882Z at NuGet.CommandLine.RestoreCommand.ProcessSolutionFile(String solutionFileFullPath, PackageRestoreInputs restoreInputs)
2020-04-07T08:05:05.4542419Z at NuGet.CommandLine.RestoreCommand.d__37.MoveNext()
2020-04-07T08:05:05.4542827Z --- End of stack trace from previous location where exception was thrown ---
2020-04-07T08:05:05.4543213Z at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
2020-04-07T08:05:05.4543673Z at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
2020-04-07T08:05:05.4544134Z at NuGet.CommandLine.RestoreCommand.d__30.MoveNext()
2020-04-07T08:05:05.4544520Z --- End of inner exception stack trace ---
2020-04-07T08:05:05.4545738Z at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
2020-04-07T08:05:05.4546231Z at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
2020-04-07T08:05:05.4546606Z at NuGet.CommandLine.Command.Execute()
2020-04-07T08:05:05.4546965Z at NuGet.CommandLine.Program.MainCore(String workingDirectory, String[] args)

I then ran a build that had run successfully before (I ran it against the same commit) and that had the same error, pointing me in the direction of the Azure hosted agent being the issue.
I then was able to confirm that the Azure agent had been updated to version 20200331.1 (this can be found in the Initialize Job step of the build).
After checking the GitHub repo for the build agent it confirmed that Visual Studio 2019 had been updated on that version of the agent.

After some research I realised that the version of NuGet.exe it was using was quite old and that NuGet should ideally match the version of Visual Studio (and more importantly MSBuild) you are using:
  • 4.1 of NuGet.exe matches Visual Studio 2017 Update 1 (15.1)
  • 4.7 of NuGet.exe matches Visual Studio 2017 Update 7 (15.7)
  • 5.0 of NuGet.exe matches Visual Studio 2019 (16.0)
  • 5.4 of NuGet.exe matches Visual Studio 2019 (16.4)
So in my case running NuGet.Exe version 4.1 to restore a Visual Studio 2019 project isn't a good idea.

To resolve this issue add a new task to your Build pipeline (NuGet Tool Installer) and set it restore a newer version of NuGet: For a YAML pipeline add:
- task: NuGetToolInstaller@1
    versionSpec: '5.x'

Or for the GUI type:

This will then insure that you are using the correct version of NuGet which should stop that error at least!

Hope that helps!