Home: Can't update or install package: An item with the same key has already been added

Created on 17 Jan 2016  Â·  19Comments  Â·  Source: NuGet/Home

See details and workaround in the StackOverflow post.

Bug

Most helpful comment

We should present an appropriate error message if there are duplicate entries in packages.config or project.json

On Jan 17, 2016, at 16:56, sampalmer [email protected] wrote:

See details and workaround in the StackOverflow post

—
Reply to this email directly or view it on GitHub.

All 19 comments

We should present an appropriate error message if there are duplicate entries in packages.config or project.json

On Jan 17, 2016, at 16:56, sampalmer [email protected] wrote:

See details and workaround in the StackOverflow post

—
Reply to this email directly or view it on GitHub.

I believe we fixed this in 3.4

This appears to be partially fixed in 3.4 There are now warning messages that there are duplicate entries, but the package manager console hangs when opening for the first time after this state is present:

duplicatepackages

Work-around: clean up the packages.config file by hand and restart Visual Studio

I can confirm the obscure error message is still there

Fortunately, I came across within minutes of googling and didn't waste much time.

A proper error message indicating what the actual problem is, would be much appreciated.

What version of nuget.exe are you using?

Sorry meant version of nuget in visual studio. Current version is 3.5 available on www.nuget.Org/downloads

image

I'm using the latest version as you can see and the message is still there

Thanks! Can you attach repro steps?

Repro steps?

package.config had two entries of the same package with different versions

restore fired as part of sln build in VS

And only from powershell? (Not next to a computer, so trying to collect as much info before getting to it)

May be will be helpfull, I also cannot pack package with such lines in nuspec files:

    <file src="Views\Web.config" target="Views" />
    <file src="Web.config" target="" />

I get the same error "An item with the same key has already been added".

sorry @yishaigalatzer totally missed your response way back when :(

not powershell no

restore happened when building the solution in VS like I said

Still encountering this issue using NuGet 4.1.0 in Visual Studio 2017 version 15.1, removing all the duplicate entire sin all package.config solution wide does not fix the problem, neither does deleting duplicate entries in the "packages" folder.

This was not an issue when using NuGet 3.5.0, but rather something that only happens in NuGet 4.1.0

EDIT:
Given a solution with more than one projects in it, where one or more of the projects have project reference to other projects in the solution, NuGet will break with the error above.

Removing the project references is a _TEMPORARY_ workarround to this issue, it appears as if there is no way for NuGet to handle projects that reference both NuGet packages and other projects at the same time.

@Zarickan Could you provide a simple repro solution we can look at?

I will try and reproduce, but I might know why it is doing this.

The project is a sepia of libraries, published as NuGet packages, lets call them Core, A, B, C, all in the same solution, also in the solution is a test project, Test. Test depends on all the libraries, Core, A, B, C as project references. The other projects, A, B, C depend on Core as a NuGet package.

I am also getting this problem in many places in our shared library projects. We are in process of migrating from VS 2015 (.Net Core preview tooling using project.json) to VS 2017 Update 2 with the new .csproj). We have a large number of .NET Core class libraries targeting net46 that generate NuGet packages which are shared across a number of other project types (Windows Service, Web App, Web Api's, and WebJobs). We never saw this issue with VS 2015 using project.json. It only started occurring with the version of NuGet that comes with VS 2017.

We have two solution files containing all of the class library projects shared between our apps. The first one is for projects that are shared across all of the applications we are building (We call this one the "CommonPackages" solution). The second solution has class library projects that are only shared across all of the server side applications (We call this one "CommonCloudPackages"). Some of the class libraries are included in other class libraries within the solutions. In these cases, we are using project references instead of packages references. However, in the case server side solution it includes package references to the packages from the "CommonPackage" solution and many of the projects also have project references two other projects within the same solution.

I've found in most cases to get around this error I've had to temporarily remove the project references in order to update or add new package references.

Below is the stack trace I see when this problem occurs. You really should consider adding some more detail as to why this problem is occurring and how to resolve it if possible.

System.ArgumentException: An item with the same key has already been added.
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary2.Insert(TKey key, TValue value, Boolean add) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 source, Func2 keySelector, Func2 elementSelector, IEqualityComparer1 comparer) at NuGet.Commands.LockFileBuilder.CreateLockFile(LockFile previousLockFile, PackageSpec project, IEnumerable1 targetGraphs, IReadOnlyList1 localRepositories, RemoteWalkContext context) at NuGet.Commands.RestoreCommand.BuildAssetsFile(LockFile existingLockFile, PackageSpec project, IEnumerable1 graphs, IReadOnlyList`1 localRepositories, RemoteWalkContext contextForProject)
at NuGet.Commands.RestoreCommand.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.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.d__10.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.d__3.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.d__4.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.d__74.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.d__58.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.<>c__DisplayClass56_1.<b__0>d.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.d__56.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.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 System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at NuGet.PackageManagement.UI.UIActionEngine.d__7.MoveNext()
Time Elapsed: 00:00:02.1024330
========== Finished ==========

Could this have anything to do with referenced project dependencies? I have a asp.net core web application targeting .net 4.6.2 that references a .netstandard 1.5 infrastructure project. Both projects rely on the same nuget package for handling database access. The web app needs it inorder to create instances to dependency inject into the infrastructure project to be used. In my web application project, I can drill down through the dependencies tree and see the infrastructure package and it's nuget dependencies.

Edit:
Nevermind, my issue was I had a base infrastructure project that was a nuget package, but also a project in my solution. The application services project referenced the nuget package, and my MsSql infrastructure project reference the solution project causing the naming conflict. It would be helpful if there were better error reporting for it though. I only found out the conflicting package by running "dotnet restore" on the web application project. Neither the package manager console nor the visual studio nuget package manager gave any details other than a stack trace.

Please update to the latest VS 2017 and NuGet 4.4.0.

Was this page helpful?
0 / 5 - 0 ratings