nuget doesn't recognize installed packages
Asked Answered
O

7

27

I have a C# project on Git that uses libraries from NuGet. Everything works fine, but when I pull on a fresh new machine and open the solution in Visual Studio it won't compile because of broken references. If I click on the references under the project I can see the classic warning sign with the yellow exclamation mark.

Nuget restore won't do anything (and I still haven't found any use of this feature...), files repositories.config are fine. If I right click on solution and then 'Manage NuGet packages for solution' no installed package is shown.

To this day I solved it this way: run

Install-Package package_name

it says:

'package_name' already installed.
My_project already has a reference to 'package_name'.

...and after that it shows the packages on the Manager, already assigned to the correct project.


NOTHING HAS BEEN CHANGED in the code ANYWHERE, I can see that because there are no differences on Git.

I have to do it only once on new machines, but it's really annoying. Any idea?


NuGet version: 2.8.60318.667


UPDATE 27/07

I tried the procedure from scratch on another PC, with windows 10, and everything works... same version of Visual Studio, NuGet, etc... I'm baffled

Ocular answered 14/7, 2017 at 13:46 Comment(10)
are you selecting the right package source?Carboxylase
Also, make sure you try cleaning up the package cache. I have had this happen to me before.Barbbarba
Yeah if you go into source control explorer and see that the packages folder is added - remove it and re-pull and it should rebuild and resote the packages just fine - you can also use a .tfignore or a .gitignore file to force the packages folder to not be included in commitsSnapback
I have seen this behavior before when the package install is corrupted, the solution in this case is to uninstall then install again, unfortunately there is no repair optionPyromancy
@Carboxylase I don't really get what you're askingOcular
@Juan I did it but it solved nothingOcular
@Snapback the packages are already correctly skipped by a .gitignore as you suggestOcular
Did you have .nuget folder in you solution? If yes, please delete it and enable NuGet Package Restore learn.microsoft.com/en-us/nuget/consume-packages/…. Make sure you can restore the packages successfully, then use the command "Update-Package -reinstall" in the Package Manager Console to force reinstall the package references into project. #42776376Fugazy
@Leo-MSFT No I don't have that folder, since I'm not using nuger restore. I'm not sure I want to update package, since I'm not using latest versions of the libraries... and again, nuget restore hasn't ever helped me...Ocular
@SOROMEED, yes , I know the restore will not resolve the reference error, NuGet restore only restore the packages in to the Packages folder. Then you should use the command "Update-Package -reinstall" to re-install all references to resolve you issue. If this still not help, please try to delete the package info in the packages.config and delete it from project file(**.csproj), then re-install it.Fugazy
M
11

This is probably because of the incorrect path of the .dll in your .csproj. The package restore downloads the packages to the local directory. It doesn't change the reference path of the assembly in the .csproj, meaning that the project will still try to locate dlls on the local directory. The yellow mark means the project is unable to locate the assembly.

Unload the project, right click on project and select "Edit .csproj", and verify the path of missing dlls. For example - If you have NUnit,

<Reference Include="nunit.framework">
      <HintPath>..\packages\NUnit.3.6.1\lib\net45\nunit.framework.dll</HintPath>
</Reference>

verify if the dll is present inside "\packages\NUnit.3.6.1\lib\net45" directory.

Microfarad answered 14/7, 2017 at 19:42 Comment(1)
This helped me figure out my issue. I couldn't reinstall the nuget package because it said it was already installed, so I removed the package from packages.config, then Nuget let me resinstall it. Worked after that.Sparrowgrass
O
9

From the top of my head I can think of a few reasons the packages are not being downloaded, ideally you would have to share a few more details.

First the install-package command won't work. Your packages are already installed VS is just unable to download them, so it makes sense that you are getting that error.

  • First and foremost is this a public package hosted in nuget.org like System.MVC.Web? Because if this is in a new machine, using a private nuget server, you need to configure that source in Tools > Options > Nuget Package Manager > Package Sources. (See https://learn.microsoft.com/en-us/nuget/tools/package-manager-ui for more details)
  • Check if you have added the folders to your git repo but at the same time set the exclusion for its contents. To check that when you do a clean checkout see if the folders exist but are empty. If thats the case just remove the folders, the git ignore should do its job from now on, and everyone new clone will do the proper check.
  • If the two above which are the most likely ones to be the reason do not work. Try and restore the packages from the Package Manager Console and update your post with the details.

You can open the Package Manager Console and type:

Update-Package -reinstall

or

Update-Package -reinstall -Project YourProjectName

FYI there's comprehensive document from Microsoft - https://learn.microsoft.com/en-us/nuget/consume-packages/package-restore - on the multiple ways of restoring nuget packages

Odell answered 14/7, 2017 at 14:36 Comment(3)
1. Yes, just to give an example: 'Newtonsoft.Json'. The sources are already configured by default at www.nuget.org/api/v2/ 2. The package folder is empty with only the repositories.config file referring to other repositories.config files specific to every single project includede in solution. I have a gitignore for ignoring folders and keeping only the .config file. This part seems to be working correctly.Ocular
@SOROMEED I think that could potentially be the issue. If you enabled the package restore in the solution you don't need that repositories.config. In an ideal world your packages file should not contain anything inside, the restore will crawl all projects and restore the packages it needs. So just try and delete that folder. FYI #11208135Odell
interesting, I don't quite get why this approach wouldn't work, but I'll try your way, even though I can't stand nuget restoreOcular
P
2

I have solved this problem. Follow this steps

  1. In Visual Studio, click Tools > Extension and Updates.
  2. Navigate to Online, search for "NuGet Package Manager for Visual Studio" and click Update.
  3. (If there is no button Update, navigate to Updates > Visual Studio Gallery, find the "NuGet Package Manager for Visual Studio" and click Update.)
  4. Then restart Visual Studio.
Primaveria answered 14/7, 2017 at 13:52 Comment(4)
I already did it and it didn't fix it. As you can see my nuget version is already updated to the lastest available today.Ocular
"As you can see my nuget version is already updated to the lastest available today" - not really; you've shown 2.8.*; that's really old. What version of Visual Studio are you running?Feld
Ah, VS 2012, I see the tag now. Are you able to update to a more recent version of Visual Studio? It's possible that some tools etc are relying on a more recent version of NuGet, which probably in turn means a more recent version of VS.Feld
No, I can't upgrade my Visual Studio versionOcular
G
2

try removing your package from below nuget cache folder so that NUGET is forced to download from source

C:\Users\<< your user name >>\.nuget\packages

Gunilla answered 21/1, 2019 at 8:40 Comment(0)
G
2

I experienced this issue today, and upgrading to the latest version of VS 2017 (15.8.7) didn't help at all.

Check your packages.config file(s) to see if your packages tag looks like this:

<packages xmlns="urn:packages">

If it does, remove the xmlns attribute so it's just:

<packages>

That fixed it for me!

Gainly answered 4/12, 2020 at 10:15 Comment(0)
N
0

Even after you've installed it at the Solution level, depending on your default you may have to pick which projects you want it to be available in. That was my problem.

enter image description here

Nichy answered 2/12, 2022 at 18:15 Comment(0)
S
0

I just downgraded the package version and then re-install the version that is supposed to show and it fixed the problem. Hope this helps

Skiplane answered 8/3, 2024 at 18:59 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.