PowerShell / PSResourceGet

PSResourceGet is the package manager for PowerShell
https://www.powershellgallery.com/packages/Microsoft.PowerShell.PSResourceGet
MIT License
485 stars 92 forks source link

Find-PSResource throw System.FormatException with a cloudsmith web server (nuget) #1364

Closed LaurentDardenne closed 1 year ago

LaurentDardenne commented 1 year ago

Prerequisites

Steps to reproduce

$arrayOfHashtables = @{Name = 'RepoTest'; Uri = 'https://nuget.cloudsmith.io/repopsresourceget/test/v2'; Trusted = $true; Priority= 50; ApiVersion='V2'}
Register-PSResourceRepository -Repository $arrayOfHashtables
get-PSResourceRepository RepoTest|select *

Set-PSResourceRepository repoTest -ApiVersion 'v2' -Trusted
get-PSResourceRepository RepoTest|select *

$r=invoke-RestMethod "https://nuget.cloudsmith.io/repopsresourceget/test/v2/FindPackagesById()?id='Depends'"
$r.properties

Find-PSResource 'Depends' -Repository RepoTest 
$Error[0]|select *

Note :

Expected behavior

Find-PSResource return the requested package.

Actual behavior

C:\temp> Find-PSResource 'Tools' -Repository RepoTest
Find-PSResource : Le format de la chaîne d'entrée est incorrect.
Au caractère Ligne:1 : 1
+ Find-PSResource 'Tools' -Repository RepoTest
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Find-PSResource], FormatException
    + FullyQualifiedErrorId : System.FormatException,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource

Error details

C:\temp> $Error[0]|select *

PSMessageDetails      :
Exception             : System.FormatException: Le format de la chaîne d'entrée est incorrect.
                           à System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number,
                        NumberFormatInfo info, Boolean parseDecimal)
                           à System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
                           à Microsoft.PowerShell.PSResourceGet.Cmdlets.V2ServerAPICalls.GetCountFromResponse(String
                        httpResponse, ErrorRecord& errRecord)
                           à Microsoft.PowerShell.PSResourceGet.Cmdlets.V2ServerAPICalls.FindName(String packageName,
                        Boolean includePrerelease, ResourceType type, ErrorRecord& errRecord)
                           à Microsoft.PowerShell.PSResourceGet.Cmdlets.FindHelper.<SearchByNames>d__19.MoveNext()
                           à Microsoft.PowerShell.PSResourceGet.Cmdlets.FindHelper.<FindByResourceName>d__16.MoveNext()
                           à
                        Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource.ProcessResourceNameParameterSet()
                           à System.Management.Automation.CommandProcessor.ProcessRecord()
TargetObject          :
CategoryInfo          : NotSpecified: (:) [Find-PSResource], FormatException
FullyQualifiedErrorId : System.FormatException,Microsoft.PowerShell.PSResourceGet.Cmdlets.FindPSResource
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : à <ScriptBlock>, <Aucun fichier> : ligne 1
PipelineIterationInfo : {}

Environment data

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Binary     0.5.24     Microsoft.PowerShell.PSResourceGet  {Find-PSResource, Get-InstalledPSResource, Get-PSResourceR...

Key   : PSVersion
Value : 5.1.19041.3031
Name  : PSVersion

Key   : PSEdition
Value : Desktop
Name  : PSEdition

Key   : PSCompatibleVersions
Value : {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.19041.3031}
Name  : PSCompatibleVersions

Key   : BuildVersion
Value : 10.0.19041.3031
Name  : BuildVersion

Key   : CLRVersion
Value : 4.0.30319.42000
Name  : CLRVersion

Key   : WSManStackVersion
Value : 3.0
Name  : WSManStackVersion

Key   : PSRemotingProtocolVersion
Value : 2.3
Name  : PSRemotingProtocolVersion

Key   : SerializationVersion
Value : 1.1.0.1
Name  : SerializationVersion

Visuals

Register image

Call api REST image

Call Find-PsResource image

SydneyhSmith commented 1 year ago

@LaurentDardenne was this working with any previous versions of the module? Also we were looking at the docs for your repo and it looks like v3 endpoints are supported, this may work better as its possible our v2 queries are different on the PS Gallery...thanks for testing this out and reporting such a detailed issue 😄

LaurentDardenne commented 1 year ago

was this working with any previous versions of the module?

This works with the version 0.5.23 : image

Also we were looking at the docs for your repo and it looks like v3 endpoints are supported, this may work better as its possible our v2 queries are different on the PS Gallery

Is it now better to configure repositories with apiVersion 'v3' (if possible)?

anamnavi commented 1 year ago

@LaurentDardenne thanks for reporting this! This previously worked with 0.5.23-beta23 because we were error handling for V2 server protocol repositories a bit differently...now we check for the <count> property in the response (and if the count is 0 we write a "Package not found" error), however in the response we get from CloudSmith server it says this property is not yet implemented for them. I will fix this to at least write a better error.

We noticed in their docs that they support V3 NuGet server. So you could register your repository with that server protocol version (v3) support. Register-PSResourceRepository -Name "CloudSmithv3" -Uri "https://nuget.cloudsmith.io/repopsresourceget/test/v3/index.json" Find-PSResource -Name "Depends" -Repository CloudSmithv3 This worked for me.

Is it now better to configure repositories with apiVersion 'v3' (if possible)?

I think it's a good idea to see which server protocol your repository can support (by looking at the docs) and try with PSResourceGet. In this case, CloudSmith supports both V2 server and V3 server protocol. Unfortunately with them not having implemented the <count> property V2 server support won't work with PSResourceGet. So I tried V3 server support and it worked with PSResourceGet. I will make a note of this in our docs for repository support.

LaurentDardenne commented 1 year ago

So I tried V3 server support and it worked with PSResourceGet.

Effectively, but I have another problem:

C:\temp> $m=Find-PSResource -Name "Depends" -Repository RepoTestV3 -IncludeDependencies
WARNING: Installing dependencies is not currently supported for V3 server protocol repositories. The package
will be installed without installing dependencies.

C:\temp> $m.Dependencies.count
0

For a migration from 'PowerShellGet' to 'PSResourceGet' my first idea is to reuse the existing one (nuget v2), I thought the new version of 'PSResourceGet' was backward compatible.