Installing NuGet package with dependencies on multiple sources
Asked Answered
U

1

16

My job has a private NuGet repo. I'm able to install packages from it and from nuget.org. I'm running into problems when there's a package stored on the private repo that has a dependency on a package hosted on nuget.org.

For instance, my private repo hosts a package, P1. P1 has a dependency on P2 which is hosted on nuget.org. If do an "install-package P1" with my private repo set as the source i'll get an error saying it couldn't find the dependency P2. This makes sense since it's looking for P2 in the private repo but it's hosted on nuget.org. So far the workaround has been installing P2 from nuget.org then installing P1 from the private repo. While this technically works it's tedious and going to make selling NuGet to the rest of the team difficult.

Is there anyway I can run install-package with multiple sources? I've tried passing a list into the -Source parameter but so far have gotten

The NuGet.config is being managed by visual studio so any changes I make to it are being wiped out every time a run a nuget command in Visual Studio. I tried adding an additional nuget.config file at the solution level but as far as I can tell it was being ignored. I've tried several visitations of the install=package command but they generally look something like this:

Install-Package P1 -Source https://api.nuget.org/v3/index.json,http://privatefeed.com

For reference here is the NuGet.config file but changing it seems futile.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageRestore>
    <add key="enabled" value="True" />
    <add key="automatic" value="True" />
  </packageRestore>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="Private Nuget" value="http://privatefeed.com" />
  </packageSources>
  <disabledPackageSources>
    <add key="Microsoft and .NET" value="true" />
  </disabledPackageSources>
  <activePackageSource>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
  </activePackageSource>
</configuration>
Udela answered 3/3, 2016 at 17:40 Comment(6)
Thanks for the additions. I've just tested this on my machine and it works fine there. I can grab a package from my local feed (on my machine) and have dependencies come from nuget.org. So the next question would be what happens if you run nuget.exe (the command line version). e.g. do nuget.exe install p1 and see if it grabs all the packages. Also what versions of nuget and VS do you have?Chibcha
I've found that running nuget.exe from the command line works as expected. However, running NuGet from VS is extremely unreliable. A basic command such as install-package nlog -Source api.nuget.org/v3/index.json is returning a few different errors such as "Install-Package : A task was canceled.At line:1 char:1" or "Unable to connect to the remote serverAt line:1 char:1". Sometimes closing VS and reopening helps, other times it doesn't. I'm running VS 2015 v14 and NuGet v3.3Udela
I'm not sure if this matters but the private NuGet requires Nt authentication so I get promoted for a username/password whenever I install a package form itUdela
I did some additional experimenting and added the package to local directory then added that package as a source and removed the private repo. I was able to pull in the private package and other packages hosted on nuget.org. It seems that just having the private repo listed as a source is causing issues pulling it packages from any repo. I'm not sure why this is the case especially since I am able to use the private repo from nuget.exe without issue.Udela
If it works from the command line but not from VS would updating the nuget in VS help? It sounds like there is a discrepancy between VS and command line.Chibcha
Multiple package sources will cause non-deterministic behavior - that is, flakiness. When using a private repository, set the client to use only that repository and ensure that that repository points has upstream sources configured.Gyral
A
10

Using NuGet.exe, you can repeat the -Source option to specify multiple package sources.

Example:

nuget install P1 -Source https://api.nuget.org/v3/index.json -Source http://privatefeed.com

It appears that it's impossible to specify multiple sources using the Package Manage Console (PowerShell). However, if no -Source is specified then a NuGet.Config file is used. The config file can have multiple package sources and the file itself can be shared with a team.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="Private Nuget" value="http://privatefeed.com" />
  </packageSources>
</configuration>

Save as NuGet.Config in the same directory as your solution and add it to version control.

Note that you might have to reload visual studio for the config changes to take effect.

Now you can install packages without configuring -Source.

Example:

Install-Package P1
Accordion answered 7/4, 2017 at 8:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.