Matteo Mosca
Matteo Mosca

Reputation: 7448

NuGet and Git Submodules

I've already extensively searched for a solution, tried different approaches but none worked, thus I'm asking here.

My current need is to have separate Git repositories each containing a .Net solution file with related projects. One or more of those repositories will contain only class libraries and I need to use those libraries inside other repositories solutions.

My first though was to use Git Submodules to avoid ugly copy-paste and still have the source code of the class libraries available when developing the applications.

So my attempt was to include the submodule, then in my current Solution do "add existing project" and load the desired class library(ies).

The huge problem I've been unable to overcome is Nuget package restore. The imported projects reference libraries in "..\Packages*" but when imported as submodule they are located in an additional subfolder so they can't find the required dependencies:

- MySolution.sln
- Packages folder
- MyAppProject folder
- Submodule folder
-- EXPECTED packages folder for MyLibraryProject that I cannot generate
-- MyLibraryProject folder

One solution I found and tried was this but it has two major problems:

Now I'd really like to know if there's a clean way to solve this problem, or if the only thing I can do is to give up having libraries source code available in my application solutions and publish my libraries on a private NuGet feed and reference them from there.

Thanks.

Upvotes: 28

Views: 4517

Answers (2)

Immi
Immi

Reputation: 187

You can resolve this issue by changing ..\packages everywhere in all *.csproj files to $(SolutionDir)\packages. Works pretty well. This script covers it:

find -name "*.csproj" | xargs sed -i 's/\.\.\\packages/$(SolutionDir)\\packages/g'

Upvotes: 6

mshish
mshish

Reputation: 346

It sounds like your issue is that the projects from the submodule are in a different directory level than their repo's solution. This is a common Nuget issue because it restores at the solution level. You can use the fix hint path script here to replace your references with $(SolutionDir) and likely solve the issue. https://github.com/owen2/AutomaticPackageRestoreMigrationScript/blob/master/README.md#fixhintpathsps1

Upvotes: 8

Related Questions