25

When I run nuget restore from the command line, I get

Error parsing solution file at MyProject.sln: Exception has been thrown by the target of an invocation.

but restoring nuget packages from Visual Studio runs without errors. Any workarounds?

sashoalm
  • 75,001
  • 122
  • 434
  • 781

7 Answers7

40

This error is particularly frustrating on large solutions with many projects, as there is no hint from NuGet at what point in the file parsing failed.

To analyze the problem, try msbuild MyProject.sln; the parser of msbuild is slightly more verbose. At least it will give you a line number, so you will know where to look for the error. Open MyProject.sln in a text editor to inspect that line. In my case, it was simply a blank line that accidentally got introduced while manually resolving a TFS merge conflict.

(It may seem quite obvious to call msbuild, but in our case, that call was part of a larger build script where nuget restore would come first, aborting the build process before msbuild was reached.)

A future release of NuGet should return a more detailed error message; see issue #1150.

Ruud Helderman
  • 10,563
  • 1
  • 26
  • 45
14

I found the solution after examining our source control. There was an incorrect merge (in git) which caused our solution to have 2 nested projects

Project(...) = ...
Project(...) = ...
EndProject
Global
.......

and the last EndProject was missing. What's interesting is that Visual Studio didn't fail even though our solution file was effectively corrupt.

Adding an EndProject between the 2 Projects fixed the error.

sashoalm
  • 75,001
  • 122
  • 434
  • 781
11

I Had same issue. The problem was the sln file has same blank lines. I removed the lines. It resolved

Ex Problem

//Blank Line
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2010
MinimumVisualStudioVersion = 10.0.40219.1

    GlobalSection(ProjectConfigurationPlatforms) = postSolution
        {658E5ED2-A01E-41DD-A952-F5EDE9E4AEE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
        {658E5ED2-A01E-41DD-A952-F5EDE9E4AEE0}.Debug|Any CPU.Build.0 = Debug|Any CPU

        {658E5ED2-A01E-41DD-A952-F5EDE9E4AEE0}.Prod|Any CPU.ActiveCfg = Prod|Any CPU
        {658E5ED2-A01E-41DD-A952-F5EDE9E4AEE0}.Prod|Any CPU.Build.0 = Prod|Any CPU
    EndGlobalSection

Fixed Version

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2010
MinimumVisualStudioVersion = 10.0.40219.1

    GlobalSection(ProjectConfigurationPlatforms) = postSolution
        {658E5ED2-A01E-41DD-A952-F5EDE9E4AEE0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
        {658E5ED2-A01E-41DD-A952-F5EDE9E4AEE0}.Debug|Any CPU.Build.0 = Debug|Any 
        {658E5ED2-A01E-41DD-A952-F5EDE9E4AEE0}.Prod|Any CPU.ActiveCfg = Prod|Any CPU
        {658E5ED2-A01E-41DD-A952-F5EDE9E4AEE0}.Prod|Any CPU.Build.0 = Prod|Any CPU
    EndGlobalSection
Sercan Timoçin
  • 658
  • 5
  • 11
6

My solution was to update nuget.exe to the latest version.

Piper
  • 851
  • 11
  • 14
3

I had the same problem. This happened on our CI-server (TFS 2018) when using the "Nuget restore" task. Appearantly the build agent was using an old version of nuget (4.1) and it had trouble reading the Visual Studio 2019 solution file (sln and/or csproj) resulting in the error indicated by @sashoalm.

The answer from Microsoft was to use the task "Nuget Tool Installer" but this was not working behind the enterprise proxy wall that I am behind. From what I have read people have a hard time with this. According the Microsoft they have resolved this but only in Azure Devops Server 2019 in an update to the "Nuget tool installer" task.

What is the resolution is to manually download the nuget.exe of your choice and copy it to the build agent. Then, replace your "Nuget" task with a "Powershell" task and do

Write-Host "Restoring packages"
Write-Host "Using nuget.exe at" $(nugetexepath)
$(nugetexepath) restore [PATH_TO_SLN_FILE] -Verbosity Detailed -NonInteractive

I added a variable

nugetexepath

as well in the build configuration.

Perhaps this gets resolved in a future update from Microsoft to help all that are still on TFS 2018 and not on Azure Devops Server 2019.

2

This can also happen after installing VS 2017 15.7 but without having .Net 4.7.2 installed.

See here for more details: https://github.com/NuGet/Home/issues/6918

Mark
  • 149
  • 11
1

I got this error from my azure pipeline. I went onto the build server & ran msbuild mysolution.sln. That revealed a different error:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Build.Framework, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified. at Microsoft.Build.CommandLine.MSBuildApp.Execute(String commandLine)
at Microsoft.Build.CommandLine.MSBuildApp.Main()

That's when I found that I found this answer Could not load file or assembly 'Microsoft.Build.Framework'(VS 2017) & thought I needed to update nuget, so I added a step in my pipeline for the NuGet Installer task & set it to use a newer version.

https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/tool/nuget?view=azure-devops

No more errors.

James Burnett
  • 151
  • 1
  • 5