Home: PackageSpec.Name and RestoreMetadata.ProjectName do not match resulting in "Error occurred while restoring NuGet packages: The given key was not present in the dictionary. "

Created on 12 Oct 2017  路  26Comments  路  Source: NuGet/Home

Details about Problem

NuGet product used (NuGet.exe | VS UI | Package Manager Console | dotnet.exe): VS

NuGet version (x.x.x.xxx): 4.4.0.4475

VS version (if appropriate): 15.4.0

Worked before? If so, with which NuGet version:

Detailed repro steps so we can see the same problem

  1. Create a new netcore console app - ParentApp.csproj

  2. Edit ParentApp.csproj and add <AssemblyName>TestAssemblyName</AssemblyName>

  3. Add a new netcore console app - ChildApp.csproj to the solution.

  4. Edit ChildApp.csproj to add a package reference that generates a warning -

  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="10.0.0" />
  </ItemGroup>
  1. Add a project reference from ParentApp.csproj to ChildApp.csproj.

  2. Restore solution.

Expected

Restore Succeeds

Actual

Restore fails for ParentApp with error -

Error occurred while restoring NuGet packages: The given key was not present in the dictionary

Investigation Notes

The root cause here is that because the parent project has overridden the AssemblyName msbuild property, the PackageSpec.Name is assigned the AssemblyName value. But the PackageSpec.RestoreMetadata.ProjectName is still the project file name. This results in a key look up exception in TransitiveNoWarnUtils here.

Workaround

Workaround is to have the AssemblyName and PackageId properties same as the project file name.

Sample Project

NoWarnParent.zip

Restore 1 Bug

Most helpful comment

Folks, this has been fixed in 15.4.4 which released today. You can read more about it - https://www.visualstudio.com/en-us/news/releasenotes/vs2017-relnotes

All 26 comments

cc @alpaix @emgarten @rrelyea

@mishra14 Thanks for looking into this! Just wanted to confirm the workaround actually helps to mitigate the problem. The error disappeared after I ensured every project is named after the assembly it produces.

This has quite the impact. Have to apply the workaround to close to 100 projects. Quite inconvenient.
Hope this is fixed very, very soon.

I also have close to 100 projects, and I really wouldn't like to change all the assembly names to match the project names. What I ended up doing was installing the NuGet command line tool version 4.3. Then I could at least run nuget restore on the command line in my solution folder, and after that, I was able to successfully build the solution in Visual Studio 2017.

Unfortunately, I'm still unable to use the package manager to install or update packages in Visual Studio as it gives me this error, but now I can continue working at least and add packages by manually editing packages.config and project files until this is fixed.

@baltie Do you have packages.config projects? If so, then this bug should not affect you as this is a netcore/sdk/PackageReference issue.

@mishra14 We have .net standard projects, some netcore and we've converted to using PackageReference in the rest. So we're quite affected.

This issue has a major impact on us. Renaming project files is not a feasible workaround. Using command-line NuGet 4.3 as a workaround.

Keeping this open to track insertion into VS.

We are facing the same issue. It will be great if the fix is available as soon as possible.

Is there any there any other workaround that does not require to change the nuget package?

Thanks!

I'm not sure whether it's the same case or a different problem but I'm having KeyNotFoundException: The given key was not present in the dictionary exception all the time in VS 2017.4. It's just terrible - I can't install or uninstall any package in some projects (not all).
Here's how it looks:

Restoring packages for D:\Work\R-n-D\XFW3\Src\Transfer\Transfer.csproj...
Committing restore...
Generating MSBuild file D:\Work\R-n-D\XFW3\Src\Transfer\obj\Transfer.csproj.nuget.g.props.
Writing lock file to disk. Path: D:\Work\R-n-D\XFW3\Src\Transfer\obj\project.assets.json
Restore completed in 116,13 ms for D:\Work\R-n-D\XFW3\Src\Transfer\Transfer.csproj.
Successfully uninstalled 'DotNetZip 1.10.1' from Transfer
Restoring packages for D:\Work\R-n-D\XFW3\Src\Transfer.Tests\Transfer.Tests.csproj...
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at NuGet.Commands.TransitiveNoWarnUtils.ExtractTransitiveNoWarnProperties(RestoreTargetGraph targetGraph, String parentProjectName, HashSet`1 parentProjectWideNoWarn, Dictionary`2 parentPackageSpecificNoWarn, Dictionary`2 warningPropertiesCache)
   at NuGet.Commands.TransitiveNoWarnUtils.CreateTransitiveWarningPropertiesCollection(IEnumerable`1 targetGraphs, PackageSpec parentProjectSpec)
   at NuGet.Commands.RestoreCollectorLogger.get_TransitiveWarningPropertiesCollection()
   at NuGet.Commands.RestoreCollectorLogger.IsWarningSuppressed(IRestoreLogMessage message)
   at NuGet.Commands.RestoreCollectorLogger.Log(IRestoreLogMessage message)
   at NuGet.Commands.RestoreCollectorLogger.Log(ILogMessage message)
   at NuGet.Commands.LockFileBuilder.CreateLockFile(LockFile previousLockFile, PackageSpec project, IEnumerable`1 targetGraphs, IReadOnlyList`1 localRepositories, RemoteWalkContext context)
   at NuGet.Commands.RestoreCommand.BuildAssetsFile(LockFile existingLockFile, PackageSpec project, IEnumerable`1 graphs, IReadOnlyList`1 localRepositories, RemoteWalkContext contextForProject)
   at NuGet.Commands.RestoreCommand.<ExecuteAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NuGet.Commands.RestoreRunner.<ExecuteAsync>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NuGet.Commands.RestoreRunner.<ExecuteAndCommitAsync>d__6.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NuGet.Commands.RestoreRunner.<CompleteTaskAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NuGet.Commands.RestoreRunner.<RunAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NuGet.Commands.RestoreRunner.<RunAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NuGet.PackageManagement.DependencyGraphRestoreUtility.<RestoreAsync>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at NuGet.PackageManagement.NuGetPackageManager.<ExecuteBuildIntegratedProjectActionsAsync>d__75.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at NuGet.PackageManagement.NuGetPackageManager.<ExecuteNuGetProjectActionsAsync>d__73.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NuGet.PackageManagement.NuGetPackageManager.<ExecuteNuGetProjectActionsAsync>d__72.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NuGet.PackageManagement.UI.UIActionEngine.<ExecuteActionsAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
   at NuGet.PackageManagement.UI.UIActionEngine.<>c__DisplayClass7_0.<<PerformActionImplAsync>b__0>d.MoveNext()
Time Elapsed: 00:00:02.3511240
========== Finished ==========

Restoring NuGet packages...
Time Elapsed: 00:00:01.7247814
========== Finished ==========

Error occurred while restoring NuGet packages: The given key was not present in the dictionary.

@mishra14 @rrelyea we verified this issue on latest 4.5.0rtm, this issue not repro.

@evil-shrike looking at the call stack, that seems to be the same issue.

This has been inserted into VS and will be available as Visual Studio 2017 15.5 Preview 3.

@mishra14 Can we have a servicing release on 15.4 for this issue?

It is being a royal pita and nuget command line only partially works around it for me.

Thanks.

What a PITA, the whole nuget process is a mess recently. Especially solutions that mix csproj of .NET Framework, .NET Standard and .NET Core. And now this..

I have a library targetting netstandard1.3 that builds fine (even after a full git clean -xdf). As soon as I change it to target netstandard2.0 (changing _nothing_ else), NuGet restore fails with this same problem:

Error occurred while restoring NuGet packages: The given key was not present in the dictionary.

Dear Microsoft,

Package management has been a solved problem for donkeys' years on "other platforms". Why do you find it so hard to maintain a very basic and unfeatured system (for example, no "provides"- semantics)? My main OS's have been running for years with Arch's "pacman" and Debian's "apt" without any of the problems that constantly hit me with NuGet.

Please, get it together.

Confirmed that the issue no longer occurs with Visual Studio 2017 15.5 Preview 3.

@kentcb can you please try 15.5 Preview 3 to see if you are still facing this problem?

@ksmithRenweb we are consider a servicing fix for this. I will update this thread once there is an update.

@mishra14 is it possible to release nuget-hotfixes in Visual Studio 2017 even though users don't want to install the preview version? Anyway in my Visual Studio 2017 Preview everything works like a charm.

a fix is highly desirable to have ASAP. Please don't suggest installing a preview of VS.
I'm still getting this error in 15.4.3rtm and it's a real pain.

Folks, we are trying to get this fix into 15.4.4. I will update once there is an update. Thanks!

Folks, this has been fixed in 15.4.4 which released today. You can read more about it - https://www.visualstudio.com/en-us/news/releasenotes/vs2017-relnotes

Was this page helpful?
0 / 5 - 0 ratings