How to configure package source mapping

Multiple feed sources

Most sitecore projects usually have more than one feed source for packages and during restore nuget will do a request to each of them to find a package. That means many unnecessary network requests.

The nuget.config file might look like that:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="Nuget" value="https://api.nuget.org/v3/index.json" />
    <add key="Sitecore" value="https://nuget.sitecore.com/resources/v3/index.json" />
  </packageSources>
  <activePackageSource>
    <add key="All" value="(Aggregate source)" />
  </activePackageSource>
</configuration>

With that config, for each package that has to be restored, nuget will do two requests (because we have two sources) to find a package. Our example is very simple, but if you have more sources in the config (maybe multiple private ones), the number of unnecessary requests might be huge.

Adding package source mapping

Fortunately nuget allows us to configure package source mapping. We can use patterns, here is an example:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <clear />
    <add key="Nuget" value="https://api.nuget.org/v3/index.json" />
    <add key="Sitecore" value="https://nuget.sitecore.com/resources/v3/index.json" />
  </packageSources>
  <packageSourceMapping>
    <packageSource key="Nuget">
      <package pattern="*" />
    </packageSource>
    <packageSource key="Sitecore">
      <package pattern="Sitecore.*" />
    </packageSource>
  </packageSourceMapping>
  <activePackageSource>
    <add key="All" value="(Aggregate source)" />
  </activePackageSource>
</configuration>

With that setup requests for packages with a name that starts with Sitecore. will be done to Sitecore feed source and all other packages will be requested only from the Nuget feed. Additionally in the logs, when doing restore, you will see lines like that:

Package source mapping matches found for package ID ‘Microsoft.CodeAnalysis.NetAnalyzers’ are: ‘Nuget’.
Package source mapping matches found for package ID ‘Sitecore.ContentSearch.SolrProvider’ are: ‘Sitecore’.