4
votes

When I try to add a NuGet package via the dotnet cli I get an error that it can't access one of my custom NuGet sources. Is there a way to say "I don't care, restore from where you can"?

McMaster.Extensions.CommandLineUtils clearly it exists in NuGet.org and it finds it but then stops b/c it can't access a custom source ????‍♂️.

PS c:\Temp\blah-project> dotnet add package McMaster.Extensions.CommandLineUtils
info : Adding PackageReference for package 'McMaster.Extensions.CommandLineUtils' into project 'c:\Temp\blah-project\blah-project.csproj'.
info : Restoring packages for c:\Temp\blah-project\blah-project.csproj
info : GET https://api.nuget.org/v3-flatcontainer/mcmaster.extensions.commandlineutils/index.json
info : OK https://api.nuget.org/v3-flatcontainer/mcmaster.extensions.commandlineutils/index.json 147ms
error: Unable to load the service index for source https://myinstace.pkgs.visualstudio.com/_packaging/Blah/nuget/v3/index.json.
error: Response status code does not indicate success: 401 (Unauthorized).

2

2 Answers

1
votes

The dotnet command has the option to specify --source. This allows you to only restore packages from a specific location, in this case you'd want to use

dotnet restore --source https://api.nuget.org/v3/index.json`

This should pull the packages down to your local NuGet store and solve the problem on your machine.

In order to add the package to your project you may need to manually add a <PackageReference> to your project like

<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.6.0" />

Then running the restore command above will get the package for you.

As a more-permanent fix, you should put a nuget.config file in the root of the project/repository where you specify the package sources for that specific project. There are settings that you can set to override your system's global nuget configuration/sources. See more information on nuget.config. You can create a starting nuget.config in your repo with dotnet new nugetconfig

1
votes

while restoring packages, dotnet make a call to all the package sources to get the package and uses the one that responds first.

In your case, because of some authentication issue it is not able to access that source. That source can be disabled using the below command and then try to pull the package.

dotnet nuget disable source <NAME> [--configfile <FILE>]