elsevers
elsevers

Reputation: 540

NuGet automatic package restore poses problems for libraries

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.

Upvotes: 1

Views: 320

Answers (1)

tom male
tom male

Reputation: 11

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

Upvotes: 1

Related Questions