13

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>
Opossum Posse
  • 171
  • 1
  • 6
  • Can you show us the command line you have been using? And what is in your nuget.config – Petrik Mar 03 '16 at 19:11
  • Sure thing, see the edits above. – Opossum Posse Mar 03 '16 at 20:27
  • 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? – Petrik Mar 03 '16 at 23:04
  • 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 https://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.3 – Opossum Posse Mar 04 '16 at 15:32
  • 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 it – Opossum Posse Mar 04 '16 at 15:39
  • 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. – Opossum Posse Mar 04 '16 at 16:11
  • 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. – Petrik Mar 06 '16 at 18:57
  • 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. – GaTechThomas Mar 23 '23 at 17:30

1 Answers1

9

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
Steven Liekens
  • 13,266
  • 8
  • 59
  • 85