Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SourceRepository.GetResource throws if type is not an exact match #12455

Closed
Sainan opened this issue Feb 28, 2023 · 8 comments · Fixed by NuGet/NuGet.Client#5107
Closed

SourceRepository.GetResource throws if type is not an exact match #12455

Sainan opened this issue Feb 28, 2023 · 8 comments · Fixed by NuGet/NuGet.Client#5107
Labels
Area:Protocol Client/Server protocol /code around it Functionality:SDK The NuGet client packages published to nuget.org help wanted Considered good issues for community contributions. Priority:3 Issues under consideration. With enough upvotes, will be reconsidered to be added to the backlog. Type:Bug
Milestone

Comments

@Sainan
Copy link

Sainan commented Feb 28, 2023

NuGet Product Used

Other/NA

Product Version

Chocolatey v2.0.0-alpha-20230124

Worked before?

No response

Impact

It bothers me. A fix would be nice

Repro Steps & Context

As per the NuGet V3 API specification, RegistrationsBaseUrl, RegistrationsBaseUrl/3.0.0-beta and RegistrationsBaseUrl/3.0.0-rc are all aliases for the same @type. However, if the server provides only one of them (e.g., RegistrationsBaseUrl), the client throws an ArgumentNullException.

Verbose Logs

System.ArgumentNullException: Value cannot be null.
Parameter name: baseUrl
   at NuGet.Protocol.RegistrationResourceV3..ctor(IHttpSource client, Uri baseUrl)
   at NuGet.Protocol.RegistrationResourceV3Provider.<TryCreate>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at NuGet.Protocol.Core.Types.SourceRepository.<GetResourceAsync>d__16`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NuGet.Protocol.Core.Types.SourceRepository.<GetResourceAsync>d__15`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at NuGet.Protocol.PackageMetadataResourceV3Provider.<TryCreate>d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at NuGet.Protocol.Core.Types.SourceRepository.<GetResourceAsync>d__16`1.MoveNext()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at NuGet.Protocol.Core.Types.SourceRepository.GetResource[T](CancellationToken token)
   at NuGet.Protocol.Core.Types.SourceRepository.GetResource[T]()
   at chocolatey.infrastructure.app.nuget.NugetCommon.<GetRepositoryResource>d__7`1.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at chocolatey.infrastructure.app.services.NugetService.install_run(ChocolateyConfiguration config, Action`2 continueAction)
   at chocolatey.infrastructure.app.services.ChocolateyPackageService.perform_source_runner_function[T](ChocolateyConfiguration config, Func`2 function)
   at chocolatey.infrastructure.app.services.ChocolateyPackageService.install_run(ChocolateyConfiguration config)
   at chocolatey.infrastructure.app.runners.GenericRunner.run(ChocolateyConfiguration config, Container container, Boolean isConsole, Action`1 parseArgs)
   at chocolatey.infrastructure.app.runners.ConsoleApplication.run(String[] args, ChocolateyConfiguration config, Container container)
   at chocolatey.console.Program.Main(String[] args)
@kartheekp-ms kartheekp-ms added Functionality:SDK The NuGet client packages published to nuget.org Area:Protocol Client/Server protocol /code around it and removed Triage:Untriaged labels Mar 5, 2023
@kartheekp-ms
Copy link
Contributor

I tried to reproduce this issue following below steps.

  • create a new class library project
  • dotnet nuget locals all -c
  • dotnet add package "newtonsoft.json" (uses nuget.org as package source by default)
  • Navigated to %localappdata%\NuGet\v3-cache\670c1461c29885f9aa22c281d8b7da90845b38e4$ps_api.nuget.org_v3_index.json folder
  • Removed entries for RegistrationsBaseUrl/3.0.0-beta and RegistrationsBaseUrl/3.0.0-rc resources from service_index.dat
  • dotnet add package "system.text.json" - This command failed couple of times with the following error. However the same command successfully executed upon trying again.
info : An error was encountered when fetching 'GET https://api.nuget.org/v3/registration5-gz-semver2/system.text.json/index.json'. The request will now be retried.
info : No such host is known. (api.nuget.org:443)
info :   No such host is known.

@Sainan
Copy link
Author

Sainan commented Mar 5, 2023

Hmm, I did use Chocolatey as the client, but I do believe NuGet is the offender here. Can you try adding https://calamity.gg/nuget3-repro/index.json as a source? This repository hosts only the "plaindummy" package, which is as empty as a nupkg can get: https://github.com/calamity-inc/plain-nuget-server/tree/senpai/dummy

@kartheekp-ms
Copy link
Contributor

I am able to reproduce the same error following below steps.

  1. Create a new class library application. (net7.0 tfm)
  2. Added https://calamity.gg/nuget3-repro/index.json as package source
  3. Executed dotnet add .\sample.csproj package "plaindummy" which failed with error: Value cannot be null. (Parameter 'baseUrl')
PS C:\Users\{USERNAME}\source\repos\sample> dotnet nuget list source
Registered Sources:
  1.  Package source 1 [Enabled]
      https://calamity.gg/nuget3-repro/index.json

PS C:\Users\{USERNAME}\source\repos\sample> dotnet add .\sample.csproj package "plaindummy"
  Determining projects to restore...
  Writing C:\Users\{USERNAME}\AppData\Local\Temp\tmp3833.tmp
info : X.509 certificate chain validation will use the default trust store selected by .NET.
info : X.509 certificate chain validation will use the default trust store selected by .NET.
info : Adding PackageReference for package 'plaindummy' into project '.\sample.csproj'.
error: Value cannot be null. (Parameter 'baseUrl')

When I added <PackageReference Include="plaindummy" Version="*" /> to the csproj file manually and then executed dotnet restore which succeeded.

PS C:\Users\{USERNAME}\source\repos\sample> dotnet restore .\sample.csproj -v n
Build started 3/8/2023 9:19:51 AM.
     1>Project "C:\Users\{USERNAME}\source\repos\sample\sample.csproj" on node 1 (Restore target(s)).
     1>_GetAllRestoreProjectPathItems:
         Determining projects to restore...
       Restore:
         X.509 certificate chain validation will use the default trust store selected by .NET.
         X.509 certificate chain validation will use the default trust store selected by .NET.
         Restoring packages for C:\Users\{USERNAME}\source\repos\sample\sample.csproj...
           GET https://calamity.gg/nuget3/PackageBaseAddress/plaindummy/index.json
           OK https://calamity.gg/nuget3/PackageBaseAddress/plaindummy/index.json 22ms
           GET https://calamity.gg/nuget3/PackageBaseAddress/plaindummy/1.0.1/plaindummy.1.0.1.nupkg
           OK https://calamity.gg/nuget3/PackageBaseAddress/plaindummy/1.0.1/plaindummy.1.0.1.nupkg 21ms
         Installed plaindummy 1.0.1 from https://calamity.gg/nuget3-repro/index.json with content hash Xt5eQVCDcewnjU7CZyCDwnPVpvn44qLLJZR/YcbmIwFx6bFp61kVSimrIwOPR8MT3t/kMRFrywvafe1DwIGn5Q==.
         Assets file has not changed. Skipping assets file writing. Path: C:\Users\{USERNAME}\source\repos\sample\obj\project.assets.json
         Restored C:\Users\{USERNAME}\source\repos\sample\sample.csproj (in 372 ms).

         NuGet Config files used:
             C:\Users\{USERNAME}\source\repos\sample\NuGet.Config
             C:\Users\{USERNAME}\AppData\Roaming\NuGet\NuGet.Config
             C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.FallbackLocation.config
             C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.Offline.config

         Feeds used:
             https://calamity.gg/nuget3-repro/index.json

         Installed:
             1 package(s) to C:\Users\{USERNAME}\source\repos\sample\sample.csproj
     1>Done Building Project "C:\Users\{USERNAME}\source\repos\sample\sample.csproj" (Restore target(s)).

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.98

I think dotnet add package command searches for the latest version of the package in the feed which is failing.

@jeffkl jeffkl added Priority:3 Issues under consideration. With enough upvotes, will be reconsidered to be added to the backlog. Pipeline:Icebox labels Mar 9, 2023
@jeffkl
Copy link
Contributor

jeffkl commented Mar 9, 2023

Team Triage: @Sainan would you be willing to contribute a fix for this?

@jeffkl jeffkl added the help wanted Considered good issues for community contributions. label Mar 9, 2023
@Sainan
Copy link
Author

Sainan commented Mar 10, 2023

I don't think I would be able to.

@atamagaii
Copy link

@jeffkl I was able to reproduce this issue and I'm interested in contributing so I can get some practice for working in large code bases. Is this a good candidate for a community bug fix?

@jeffkl
Copy link
Contributor

jeffkl commented Mar 22, 2023

@atamagaii yes absolutely! Let us know if you need help.

@nkolev92
Copy link
Member

Fixed in NuGet/NuGet.Client#5107.

The Fixes statement wasn't on the same line as the issue so it didn't auto close it.

@nkolev92 nkolev92 added this to the 6.7 milestone Jun 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area:Protocol Client/Server protocol /code around it Functionality:SDK The NuGet client packages published to nuget.org help wanted Considered good issues for community contributions. Priority:3 Issues under consideration. With enough upvotes, will be reconsidered to be added to the backlog. Type:Bug
Projects
None yet
5 participants