Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nuget doesn't recognize installed packages

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

like image 341
git_gud Avatar asked Jul 14 '17 13:07

git_gud


People also ask

How do I know if a NuGet package is installed?

go to the Project or Solution in question. right click, Manage NuGet Packages... on the left, you will see 'Installed Packages' click on this and you will see the list.

What happens when a NuGet package is installed?

NuGet creates a subfolder for each package identifier, then creates subfolders for each installed version of the package. NuGet installs package dependencies as required. This process might update package versions in the process, as described in Dependency Resolution.

How do I force a NuGet package to Install?

Switch to the Browse tab, search for the package name, select it, then select Install). For all packages, delete the package folder, then run nuget install . For a single package, delete the package folder and use nuget install <id> to reinstall the same one.

Does NuGet automatically Install dependencies?

Install NuGet packages without dependenciesBy default, when installing NuGet packages, corresponding package dependencies will also be installed in your project. To avoid the installation of dependent packages, choose the Ignore Dependencies option in the Dependency behavior drop-down in NuGet Package Manager.


2 Answers

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.

like image 197
Katana Avatar answered Sep 26 '22 15:09

Katana


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://docs.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://docs.microsoft.com/en-us/nuget/consume-packages/package-restore - on the multiple ways of restoring nuget packages

like image 35
rjso Avatar answered Sep 23 '22 15:09

rjso