Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restoring all Nuget Packages in a Visual Studio 2015 Solution

I have read many of the answers on SO and NuGet (and the Internet in general, really), but I can't seem to overcome the problem I am having with NuGet package restore in Visual Studio 2015. I have the following scenarios.

Solution A Structure

--Project A

If I open and build Solution A I see the dialog box that shows the nuget package restore progress and the solution builds successfully.

Solution B Structure

--Project A

--Project B

However, assuming that I have never built Solution A (i.e. fresh pull from TFS), if I open and build Solution B I see the dialog box that shows the nuget package restore progress, but the build fails because Project A fails to build.

What appears to be happening is that that NuGet is restoring the packages for Project B, but not for Project A thus the build failure. To the point, if I look at the references for Project B all of the NuGet references have resolved, but the references for Project A are still broken.

A few points:

  • I have disabled source control integration for NuGet so I am not checking in the Packages folder.
  • Each project has its own packages.config file
  • The build order in Solution B is Project A then Project B

Thoughts would be greatly appreciated.

like image 374
dparsons Avatar asked Mar 11 '23 20:03

dparsons


1 Answers

By default, NuGet creates the solution's packages folder in the solution root, and each project references its package DLLs to that "local" packages folder. In your example, if you open the .csproj file for Project A, you'll probably see that the reference path is something like ..\packages\[package name]\[etc].

So when you do a fresh pull from TFS and build solution B, Project A can't find its DLLs because c:\workspace\Solution A\packages doesn't exist yet (or whatever the absolute path is on your machine).

To correct this, use a shared package folder, created at c:\workspace\packages. To do this, you have to add an additional node to the NuGet.config in each solution (see https://docs.nuget.org/consume/nuget-config-file for details; I am also assuming you have a NuGet folder at c:\workspace\Solution A\.nuget):

<config>
  <add key="repositorypath" value="..\..\packages" />
</config>

I used a relative path here, but you can use an absolute path as well, and the documentation says you can use %HOME% as well.

Do this, then restart Visual Studio. The next time you open the package manager, it should ask you if you want to restore missing packages, and assuming you click yes, it will put them in the new location. The last step is to edit the .csproj file and change all instances of ..\packages to ..\..\packages (or you can uninstall and reinstall the package, but I find editing the .csproj a lot faster).

like image 60
howcheng Avatar answered Mar 14 '23 10:03

howcheng