23

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

git_gud
  • 649
  • 1
  • 11
  • 27
  • are you selecting the right *package source*? – Rahul Jul 14 '17 at 13:47
  • Also, make sure you try cleaning up the package cache. I have had this happen to me before. – JuanR Jul 14 '17 at 13:49
  • 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 commits – confusedandamused Jul 14 '17 at 13:55
  • 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 option – MikeT Jul 14 '17 at 14:05
  • @Rahul I don't really get what you're asking – git_gud Jul 14 '17 at 14:10
  • @Juan I did it but it solved nothing – git_gud Jul 14 '17 at 14:10
  • @confusedandamused the packages are already correctly skipped by a .gitignore as you suggest – git_gud Jul 14 '17 at 14:11
  • Did you have .nuget folder in you solution? If yes, please delete it and enable NuGet Package Restore https://learn.microsoft.com/en-us/nuget/consume-packages/package-restore. 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. https://stackoverflow.com/questions/42775876/nuget-packages-are-there-but-missing-references/42778916#42778916 – Leo Liu Jul 17 '17 at 02:00
  • @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... – git_gud Jul 17 '17 at 09:31
  • @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. – Leo Liu Jul 19 '17 at 06:02

6 Answers6

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

rjso
  • 1,314
  • 10
  • 19
  • **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. – git_gud Jul 14 '17 at 14:45
  • @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 https://stackoverflow.com/questions/11207635/what-is-the-nuget-repositories-config-file-for – rjso Jul 14 '17 at 14:56
  • 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 restore – git_gud Jul 17 '17 at 09:32
9

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.

Katana
  • 401
  • 3
  • 22
  • 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. – Rich Nov 09 '17 at 14:33
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.
Damian Vogel
  • 1,050
  • 1
  • 13
  • 19
Kemal Güler
  • 608
  • 1
  • 6
  • 21
  • 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. – git_gud Jul 14 '17 at 14:11
  • "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? – Jon Skeet Jul 14 '17 at 14:40
  • 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. – Jon Skeet Jul 14 '17 at 14:40
  • No, I can't upgrade my Visual Studio version – git_gud Jul 17 '17 at 09:33
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

gargmanoj
  • 123
  • 9
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!

Piku Shrivastav
  • 119
  • 1
  • 1
  • 9
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

Austin B
  • 184
  • 1
  • 1
  • 8