0

I have been trying to figure out why automatic package restore in Visual Studio 2015 doesn't work. As I understand it there is nothing to do but check a couple of settings. When you build, it looks for missing packages and downloads them automatically. I have a solution that has 15 individual projects. The majority of them will not compile because of "missing packages".

I do not have any of the legacy NuGet (.nuget folder etc.) in any of these projects and I have the latest and greatest version of NuGet.

Visual Studio simply will not download the missing files. I deleted the solution package folder and it does re-create and download all of the packages when I build, but each individual project still shows missing references. If I go to the package manager console and issue a

Update-Package -reinstall

then the packages download and everything works. I'm just wonder why it doesn't do this automatically. It's supposed to right?

user2033791
  • 810
  • 12
  • 23
  • The next time it happens, look at where the "missing" reference is pointing to. Open the .csproj file and look at the . I have a feeling something isn't correct there. – Kiliman Dec 02 '16 at 23:58
  • I can only say it depends on a lot of factors. If once it was restoring for instance, and you closed VS, then it can break restore for ever. And easy workaround is to download NuGet.exe from Microsoft, and execute `nuget restore yoursolution.sln` to force a restore. – Lex Li Dec 03 '16 at 03:13
  • Did you check the restored package path and the referenced path to see if they are consistent? – Eddie Chen - MSFT Dec 08 '16 at 06:42

1 Answers1

0

NuGet Restore only restores files in the packages directory, but does not restore files inside your project or modify your project.

For example, if there has a package will add some reference dlls or add some content files into your project. After deleting these reference dlls and content files from your project, it will not be added when restoring this package. This will cause the your project could not find the missing pacakges when compile.

So we need use "Update-Package -reinstall" command to force reinstall the package references and content files into project.

Update the example for sharing projects in a team:

Following is my solution structure, the CommDLL project installed some NuGet packages and entire solution is managed by a source control.

enter image description here

I download this solution on another machine from source control use another user account and install another NuGet packages into the CommDLL project. Then use some content from the new installed package and build the project successful. Please make sure the package dlls has been added into your project and it has been set the correct HintPath in .csproj file.

<Reference Include="Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
  <HintPath>..\packages\Newtonsoft.Json.9.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
  <Private>True</Private>
</Reference>

Next, I check in this modified project into the source control. Please make sure you the .csproj file and packages.config file are checked in.

enter image description here

Now I get the latest version on another machine to get the modified content. After check out the latest version from source control, the package references are shown with a warning because the project is not compiled, which means the packages are not restored. Please rebuild this project, it will restore the packages for your project (make sure your Visual Studio 2015 has enable "Allow NuGet to download missing packages" and "Automatically check for missing packages during build in Visual Studio" before rebuild this project).

enter image description here

Weiwei
  • 3,674
  • 1
  • 10
  • 13
  • All I really know is that there seems to be conflicting advice on the topic. Everything from "With VS 2015 update 3 you don't have to do anything" to "You have to modify your solution and create Nuget.Config files". It seems like it works well until you have more than one person adding packages to the solution then everything falls apart. The way it's implemented in VS makes it seem like you can just use the NuGet package manager and everything will take care of itself, but that does not work. I would love to see an example implementation that includes a team and shared projects. – user2033791 Dec 06 '16 at 13:10
  • It seems like the packages directory should be treated more like the obj and bin directories. They don't get checked in to source control and they are just recreated for whomever loads the project. – user2033791 Dec 06 '16 at 13:10
  • Here's one opinion: http://stackoverflow.com/questions/18376313/setting-up-a-common-nuget-packages-folder-for-all-solutions-when-some-projects-a – user2033791 Dec 06 '16 at 13:10
  • Here's another: http://blog.davidebbo.com/2014/01/the-right-way-to-restore-nuget-packages.html – user2033791 Dec 06 '16 at 13:11
  • On the official nuget site it says: https://docs.nuget.org/ndocs/consume-packages/package-restore#automatic-restore-in-visual-studio "A .nuget folder is created in the solution containing a nuget.config file" but that does not seem to happen when I create a solution. – user2033791 Dec 06 '16 at 13:16
  • And the site also says "It requires importing a .targets file into all projects in the solution, which this can introduce issues when projects are shared among multiple solutions" and that may be our problem. So if you can't share projects among multiple solutions then I guess it really doesn't work. (At least not for us). – user2033791 Dec 06 '16 at 13:21
  • The latest nuget for VS2015 should be version 3.1.1. So the problem of "It requires importing a .targets file into all projects in the solution, which this can introduce issues when projects are shared among multiple solutions" should not occurs in your solution, because this issue is occurs for MSBuild-integrated restore approach, which uses nuget 2.6 and previous version. The example that implement sharing projects in a team, I will update it in my reply. – Weiwei Dec 07 '16 at 04:35