1

When using the NuGet automatic package restore added in version 2.7, NuGet automatically downloads any missing packages to a packages\ folder located at the solution level. When a solution includes libraries (i.e. git subtree or git submodules) this causes problems as the library projects expect the packages to be downloaded to a packages folder located in their respective solution folder (which is typically nested as a subfolder inside the primary code's solution folder) and do not know to look for the packages in the primary code's solution folder. For example:

primary_code_folder\
->primary_code.sln
->packages\
    [various packages downloaded by NuGet]
->primary_code_project\
->library_solution_folder\
--->library_code.sln
--->packages\
      [where the library project expects packages to reside]
--->library_code_project\

Potential solutions to this:

  1. Manually open each library project .sln file and build the project to ensure the latest packages are restored within each library package folder. This is undesirable as it requires each developer to remember to do this every time someone updates/installs a new package.
  2. Somehow configure each library to also look in the packages folder in the primary solution's code. This seems like a highly undesirable fix because it modifies the library's code to be unique to a given project it is being used in.
  3. Configure the primary code's solution to run nuget.exe restore library_code.sln in a pre-build event command line command. However, nuget.exe isn't naturally in the solution and isn't (by default) located in a path variable. So it seems that copying nuget.exe into the project may cause later incompatibility as updates would not be pulled in. Perhaps there is a workaround for this?
  4. Somehow configure NuGet to store packages on a project level instead of a solution level?

Can anyone share how they have approached this problem? A similar question was asked here (NuGet Automatic Package Restore when using git submodules) for a different situation and the answer does not satisfy the needs I am describing in this post.

Community
  • 1
  • 1
elsevers
  • 532
  • 1
  • 4
  • 16
  • I had a similar problem a few months ago and ended up with a msbuild task similar to your solution #3 as I could not find anything better. The problem is that package restore is a Visual Studio feature, ie it is not executed if you try to build the stuff not from the original solution or using an msbuild command line. – Christoph Apr 01 '15 at 15:10
  • Thanks. I was wondering if maybe the package restore was a part of Visual Studio. One challenge with this workflow is that we have added the library project using git subtree and are actively editing it. So if we make a change to the library that breaks the build, the error would not show up properly in the IDE because the msbuild task would fail before Visual Studio attempts to compile the primary code solution. – elsevers Apr 02 '15 at 14:11
  • What I did not yet understand is what your current and your desired workflow would be: are you building the library and the primary code separately or do you want to have a one-step build of everything. If so, does this have to be performed in VS? And are you in control of all the projects, ie would it be acceptable to change the library solution? – Christoph Apr 02 '15 at 20:25
  • We want a one-step build of everything in VS. We are in control of everything, so we could modify the library solution. However, we are using this library in multiple VS solutions in multiple repos (always as a git subtree, so we actively push/pull the library repo from each primary code repo). – elsevers Apr 04 '15 at 03:43
  • One-step is basically what I am doing, too. Do you include the *project* (not the solution) of the library in the application solution? Because otherwise, you would need some separate step for building the library before the application anyway (where you could inject the package restore). – Christoph Apr 07 '15 at 08:17
  • Possible duplicate of [Nuget Package restore with git submodule](http://stackoverflow.com/questions/18609333/nuget-package-restore-with-git-submodule) – georgiosd Mar 30 '17 at 15:10

1 Answers1

1

This can be done by placing multiple nuget.config files, at each folder location, where a sln exists.

Within the nuget.config you need to add the following

<config>
 <add key="repositoryPath" value="C:\Temp" />
</config>

or

<config>
 <add key="repositoryPath" value="..\..\.." />
</config> 

have a look here for more details https://docs.nuget.org/consume/nuget-config-settings

tom male
  • 11
  • 2