19

i have a project where i include 2 submodules from git. Both projects have "nuget package restore" enabled, the parent project too. The package folder in the two included submodules is not checked in, does not exist in checked out projects. When building the parent project Nuget tries to restore the packages in the subfolders but into the wrong package folder!

"C:\Dev\git\oasisdb\odb_oasis_repository\ODB_OASIS_Repository\.nuget\NuGet.exe" install "C:\Dev\git\oasisdb\odb_oasis_repository\odb_oasis_rvm\ODB_OASIS_RVM_EF\ODB_OASIS_RVM_EF\packages.config" -source ""  -NonInteractive -RequireConsent -solutionDir "C:\Dev\git\oasisdb\odb_oasis_repository\ODB_OASIS_Repository\ "

Why does nuget not restore in the solution dir of the submodule?

Thanks

Sascha Herrmann
  • 532
  • 5
  • 14
  • Possible duplicate of [NuGet not getting missing packages](http://stackoverflow.com/questions/17797052/nuget-not-getting-missing-packages) – Anders Abel Nov 30 '15 at 13:18

4 Answers4

19

Nuget is restoring the package in the opened solution directory.

You can edit the .csproj of the submodule project and modify package dll references from :

   <ItemGroup>
    <Reference Include="Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
      <HintPath>..\packages\Microsoft.Rest.ClientRuntime.2.1.0\lib\net45\Microsoft.Rest.ClientRuntime.dll</HintPath>
      <Private>True</Private>
    </Reference>

to :

 <ItemGroup>
<Reference Include="Microsoft.Rest.ClientRuntime, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
  <HintPath>$(SolutionDir)\packages\Microsoft.Rest.ClientRuntime.2.1.0\lib\net45\Microsoft.Rest.ClientRuntime.dll</HintPath>
  <Private>True</Private>
</Reference>

Hope this help!

Srounsroun
  • 237
  • 2
  • 4
  • 2
    If you have to modify each project in the submodule you are effectively modifying the submodule, that is a big no. As @ Nuzzolilo says below, the library must not know anything about the projects that are using it. They should be agnostic. Sorry, but this is not a valid answer. – SuperJMN Dec 16 '16 at 09:21
  • yes of course this should be use only if you own the submodule and know what to do with this trick. Thanks for clarification – Srounsroun Feb 14 '17 at 15:29
  • 1
    I found that $(SolutionDir) includes the trailing backslash, so that can be removed. It seems that the tag can cope with double backslashes, but the tag can't and will cause build errors e.g. – Alan Hinton Jan 08 '20 at 11:48
9

Found the answers: For anyone interrested:

http://www.xavierdecoster.com/how-to-nuget-package-restore-when-sharing-projects-between-solutions

and

NuGet not getting missing packages

Community
  • 1
  • 1
Sascha Herrmann
  • 532
  • 5
  • 14
5

you can use symbolic link: After nuget downloads all packages to solution's packages directory, create symbolic link in submodule's root directory (names packages and link to the solution level packages directory). In short - in your startup project add Pre-Build event that creates symbolic link between your solution packages directory to all your submodules packages directory:

This is the batch:

SET sourceDir=$(SolutionDir)packages
SET destDir=$(SolutionDir)..\..\submodules\saturn72\src\packages

if not exist %sourceDir% mkdir %sourceDir%

if not exist %destDir% mklink /j %destDir% %sourceDir%

Full explanation and source code: SolutionWithGitSubmodulesAndNuget

Roi Shabtai
  • 2,981
  • 2
  • 31
  • 47
  • Had to use a NTFS junction on Windows because my submodule's .gitignore wasn't ignoring the symbolic link. – bgfvdu3w May 18 '18 at 21:49
1

If you're using VS2015 Update 1 or later, you can convert your project to use project.json to fix this.

In short:

  • Run Uninstall-Package <package name> -Force -RemoveDependencies for all your packages. You may wanna copy-paste your packages.config in notepad before you do this.
  • Delete packages.config from the project, save the project, unload
  • Edit the project file and remove:
    • Any referenced .props files at the top related to nuget
    • Any <Reference> elements that reference a package
    • The .targets files at the bottom that reference nuget - usually starts with: <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    • If your packages contain Roslyn analyzers, make sure to remove them too.
  • Save the file and relod the project

Add project.json with:

{
  "dependencies": {
  },
  "frameworks": {
    ".NETFramework,Version=v4.6.1": {}
  },
  "runtimes": {
    "win": {}
  }
}

Finally add your packages again, either by hand under dependencies or using Install-Package or with the nuget UI in VS.

I've also had to remove any Microsoft.Bcl.* packages from my projects because they explicitly look for a packages.config file.

EDIT: this (removing the Microsoft.Bcl.* packages will give you a compile-time error, even though the project will build fine, because the .targets file Microsoft.Bcl.Build adds will still look for packages.config.

To suppress this, edit your project file and add:

<SkipValidatePackageReferences>true</SkipValidatePackageReferences>

This needs to go to the first <PropertyGroup> that doesn't have a Condition attribute set. If there isn't one, just add another at the top, like:

<PropertyGroup>
    <SkipValidatePackageReferences>true</SkipValidatePackageReferences>
</PropertyGroup>
georgiosd
  • 3,038
  • 3
  • 39
  • 51