Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS2017 nuget keeps searching for packages in wrong location

So I migrated from nuget packages.config to PackageReference and found out there were some compatibility issues. I reverted the project to its working state (before the PackageReference) and now my project is not compiling.

I get the following error:

Severity Code Description Project File Line Suppression State Error The package EntityFramework with version 6.2.0 could not be found in C:\Users\user.nuget\packages. Run a NuGet package restore to download the package. DbManager

This happened to multiple packages. It seems that Nuget is searching for packages in the user.net\package directory for some reason. Originally, there was a folder within the project that contained all the packages.

I forced the global path to be at the folder within the project by editing the NuGet.Config file.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <config>
    <add key="repositoryPath" value="C:\Projects\App\App Source\packages\" />
    <add key="globalPackagesFolder" value="C:\Projects\App\App Source\packages\" />
  </config>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
  </packageSources>
  <packageRestore>
    <add key="enabled" value="True" />
    <add key="automatic" value="True" />
  </packageRestore>
  <bindingRedirects>
    <add key="skip" value="False" />
  </bindingRedirects>
</configuration>

I don't know why Nuget keeps looking for packages at that location. It should be looking at the packages folder within the project.

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="6.2.0" targetFramework="net46" />
  <package id="EntityFramework6.Npgsql" version="3.1.1" targetFramework="net46" />
  <package id="Npgsql" version="3.2.7" targetFramework="net46" />
  <package id="System.Threading.Tasks.Extensions" version="4.3.0" targetFramework="net46" />
  <package id="Z.EntityFramework.Plus.EF6" version="1.7.17" targetFramework="net46" />
</packages>

All of these packages in packages.config are not being found. This problem started happening when I tried PackageReference.

Is there any way to reset Nuget's settings? I would appreciate any guidance in solving this problem.

like image 816
ChipNugget Avatar asked Nov 15 '18 00:11

ChipNugget


Video Answer


1 Answers

TLDR; This is caused by latent copies of the new project.assets.json file being left in your /obj/ folders. These can be safely deleted.

You can run this Powershell (at your own risk) in your root solution folder as a quick way to purge these files:

ls project.assets.json -Recurse | foreach {rm $_}

project.assets.json is generated for projects using PackageReference to cache the Nuget dependency graph for your project. It seems to confuse Visual Studio/Nuget if it's left there even if your project is using (or reverted back to using) packages.config

This can happen in Visual Studio 2019 as well, if you try PackageReference and then revert back to packages.config (or even if you switch between Git branches with one Nuget restore method versus the other).

Further Info
More info on project.assets.json here:
https://learn.microsoft.com/en-us/dotnet/core/tools/dotnet-build?tabs=netcore2x

like image 96
basecode Avatar answered Oct 08 '22 17:10

basecode