The problem occurs when a dll is referenced as a nuget package and as a project reference.
If there are 3 projects, Source
, Intermediate
and DoublyReferencedTarget
Then the problem will occur when Source
has:
Intermediate
DoublyReferencedTarget
And 'Intermediate' has
DoublyReferencedTarget
. Thus project DoublyReferencedTarget is referenced twice in two different ways, which creates this error.
I realise that this is a genuine error, but a better error message would be very useful. Some of our solutions contains 100's of projects (a bad idea I know), and it's just taken me a week to track down this problem.
I have created a repo to demonstrate the problem at https://github.com/ceddlyburge/nuget-error.
@ceddlyburge
I am unable to reproduce this with the latest 15.7 preview.
Which version of VS are you using?
I am able to reproduce it with the demo repo. VS 2017 Ent 15.6.6
And we also have this issue, and it is a quite big problem here, I've spent days to solve it, but we were unable to. So any help (hotfix, workaround, etc...) would be appreciated.
Hi there, I am on VS 2017 Professional 15.6.4
I also get the problem on the command line using nuget 4.3.0.4406, which in turn uses msbuild 15.6.82.30579
Running into this as well...
Ran into this as well with VS 15.9. Anyone found a solution?
NuGet 4.9.2.5706 (nuget restore), msbuild version '15.9.21.664', VS2017 15.9.6 still failing. When this will be solved?
Nuget 4.9.3.5777, VS2017 15.9.11. This still is an issue.
vs19 same issue
VS 2019 same error
This is insane to see such a problem exist for such a long time without any response or workaround provided by the Nuget team. As I still could confirm these issues exist (vs 2019, .net core 3.0 preview) the simple workaround is to change the version of a package you do reference in sources. With the new version, it should start working fine.
I was able to reproduce the error. The exception is thrown here: https://github.com/NuGet/NuGet.Client/blob/3113c5ddf420ae0c9c34959108e127136c8f141e/src/NuGet.Core/NuGet.Commands/RestoreCommand/LockFileBuilder.cs#L152
The issue appears to be that the same name+version pair is both a package and a project reference. If they had different versions, it would work, or if it was consistently a package or a project reference both times, I expect it would work.
My initial reaction is that the assets file might need to change to support this scenario, rather than being a simple bug to fix.
From the sample, if we remove the project reference to intermediate
from source
, source
can reference both the package and project and it works fine, NuGet selects the project. This "prefer project over package" logic doesn't appear to be happening for transitive dependencies, which I think is the bug.
I got this message, which I was able to fix by converting the project to the new project format. Have no idea what the real problem was.
Hey so I got this issue when doing some coding and adding a reference to a project that was already in the project as a nuget package. I removed the nuget package - added a project reference but the dotnet restore command failed. A work-around was to change the version number of the referenced project.
Thanks for this commit! Will this fix the issue in Visual Studio itself, or in the DevOps pipelines? Or neither?
I had the same issue using a TeamCity CI Pipeline. But I cloud slove the problem by switching to dotnet cli to restore packages.
The difference is that the _NuGet.exe restore_ command targets the whole .sln file whereas the _dotnet.exe restore_ command can be configured to target only a certain .csproj file.
All my projects have the new csproj format and use PackageReference instead of packages.config file.
It would be nice that the error message mentions the duplicate package that NuGet found.
In big solutions it's hard to find what that duplicate package is.
I have also encountered this problem, which can be resolved by removing the package that is referenced directly through the project and replacing it with a Nuget reference
Still failing in Microsoft Visual Studio Enterprise 2019 Version 16.5.4:
But not failing when "nuget restore sln" in NuGet Version: 5.4.0.6315
Hey so I got this issue when doing some coding and adding a reference to a project that was already in the project as a nuget package. I removed the nuget package - added a project reference but the dotnet restore command failed. A work-around was to change the version number of the referenced project.
I had the same exact issue. I removed the nuget package, and added the code as a project instead, and I couldn't get rid of the error, even after deleting all .vs folders, packages, and %temp%
Is there any chance we might see a bug fix for this more than 2 year old bug? It's kind of sad for such a widely used project to have a bug like this completely ignored.
It is still an issue today
I was able to reproduce the error. The exception is thrown here: https://github.com/NuGet/NuGet.Client/blob/3113c5ddf420ae0c9c34959108e127136c8f141e/src/NuGet.Core/NuGet.Commands/RestoreCommand/LockFileBuilder.cs#L152
The issue appears to be that the same name+version pair is both a package and a project reference. If they had different versions, it would work, or if it was consistently a package or a project reference both times, I expect it would work.
My initial reaction is that the assets file might need to change to support this scenario, rather than being a simple bug to fix.
It looks any change to lock file might break existing build and restore since we can't support both project reference and packagereference for same Id&version. Even we support which one take more priority is tricky question. So I believe best course of action is display "Explicit error" for what is wrong. I already talked with @nkolev92 on this.
From the sample, if we remove the project reference to
intermediate
fromsource
,source
can reference both the package and project and it works fine, NuGet selects the project. This "prefer project over package" logic doesn't appear to be happening for transitive dependencies, which I think is the bug.
@zivkan
Do we have logic written somewhere in documents or code for "prefer project over package"?
From a quick search, I couldn't find it. @nkolev92 do you know?
https://github.com/NuGet/NuGet.Client/blob/3ef6cd3c0145e59314a495569c2d53296794fbf3/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/IncludeFlagUtils.cs#L200-L221 which is called by https://github.com/NuGet/NuGet.Client/blob/3ef6cd3c0145e59314a495569c2d53296794fbf3/src/NuGet.Core/NuGet.Commands/RestoreCommand/Utility/IncludeFlagUtils.cs#L67-L165, which in turn is called when the dependencies are flattened, ie when the assets file is created: https://github.com/NuGet/NuGet.Client/blob/3ef6cd3c0145e59314a495569c2d53296794fbf3/src/NuGet.Core/NuGet.Commands/RestoreCommand/LockFileBuilder.cs#L171
Most helpful comment
It would be nice that the error message mentions the duplicate package that NuGet found.
In big solutions it's hard to find what that duplicate package is.