PowerShell / PowerShellGetv2

PowerShellGet is the Package Manager for PowerShell
https://www.PowerShellGallery.com
MIT License
431 stars 138 forks source link

Find-Module options -AllowPrerelease and -AllVersions do not return proper results #491

Closed RepeatDaily closed 5 years ago

RepeatDaily commented 5 years ago

I have configured a share-based repository, and am using the prerelease property in the manifest. Modules are published to the repository using Publish-Module.

PrivateData = @{
  PSData = @{
    ...
    Prerelease = 'something interesting'
  }
}

a. When I search for a module I get the latest version, which is good.

PS>Find-Module My-SuperModule -Repository SuperDuper | ...

Name               Version PreRelase    Latest
----               ------- ---------    -------
My-SuperModule 1.0.1       False        True

b. When I use the -AllVersions switch it only shows me prerelease versions, which is not good.

PS>Find-Module My-SuperModule -Repository SuperDuper -AllVersions | ...

Name               Version                      PreRelase   Latest
----               -------                      ---------   -------
My-SuperModule 1.0.1-uXX012345d20190607b009      True       True
My-SuperModule 1.0.1-uXX012345d20190607b008      True       True
My-SuperModule 1.0.0-uXX012345d20190607b007      True       True
My-SuperModule 1.0.0-uXX012345d20190607b006      True       True

c. When I add the -AllowPrerelease switch to that command I get everything, including the release versions.

PS>Find-Module My-SuperModule -Repository SuperDuper -AllVersions -AllowPrerelease | ...

Name               Version                      PreRelase   Latest
----               -------                      ---------   -------
My-SuperModule 1.0.1                            False       True
My-SuperModule 1.0.1-uXX012345d20190607b009      True       True
My-SuperModule 1.0.1-uXX012345d20190607b008      True       True
My-SuperModule 1.0.0                            False       True
My-SuperModule 1.0.0-uXX012345d20190607b007      True       True
My-SuperModule 1.0.0-uXX012345d20190607b006      True       True

I would expect that PowerShellGet would only return 'release' modules unless the -AllowPrerelease switch passed, but it appears to be working exactly the opposite.

Additional Environment Data:

Name                           Value
----                           -----
PSVersion                      5.1.16299.1146
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.16299.1146
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

PowerShellGet version:

ModuleType Version    Name
---------- -------    ----
Script     2.1.4      PowerShellGet

I tested this with PowerShellGet v 2.1.2 and 2.1.4 with the same results.

SydneyhSmith commented 5 years ago

Thanks for reporting this issue @RepeatDaily I am having some difficulty reproducing, do you mind sharing the results of Get-InstalledModule PackageManagement and Get-PackageProvider

RepeatDaily commented 5 years ago

Hello @SydneyhSmith, here are the results.

PS> Get-PackageProvider

Name                     Version
----                     -------
msi                      3.0.0.0
msu                      3.0.0.0
NuGet                    3.0.0.1
PowerShellGet            2.1.4.0

PS> Get-InstalledModule PackageManagement

Version              Name
-------              ----
1.4.1                PackageManagement
petrsnd commented 5 years ago

@SydneyhSmith

I have this same issue. I was trying to use Find-Module against the feed for a module I maintain.

https://www.powershellgallery.com/packages/safeguard-ps

You can reproduce the error I am seeing using this command:

PS> Find-Module safeguard-ps -AllVersions

You will see many errors such as:

Cannot convert value "2.2.128-pre" to type "System.Version". Error: "Input string was not in a correct format."
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:7156 char:9
+         $PSGetItemInfo = Microsoft.PowerShell.Utility\New-Object PSCu ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastParseTargetInvocation

You cannot call a method on a null-valued expression.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:7204 char:9
+         $PSGetItemInfo.PSTypeNames.Insert(0, "Microsoft.PowerShell.Co ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Cannot convert value "2.2.126-pre" to type "System.Version". Error: "Input string was not in a correct format."
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:7156 char:9
+         $PSGetItemInfo = Microsoft.PowerShell.Utility\New-Object PSCu ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvalidCastParseTargetInvocation

You cannot call a method on a null-valued expression.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:7204 char:9
+         $PSGetItemInfo.PSTypeNames.Insert(0, "Microsoft.PowerShell.Co ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
SydneyhSmith commented 5 years ago

@petrsnd thanks for posting about this issue, and for the detailed repro-case. I am having some trouble reproducing this with Modules published to the PowerShell Gallery (rather than private repos)--would you be able to provide the results of the command Get-InstalledModule PowerShellGet, PackageManagement based on the error messages its possible that you may need to update to a newer version of PowerShellGet which is able to handle pre-release versioning

petrsnd commented 5 years ago

I get errors with Get-InstalledModule.

PS> Get-InstalledModule PowerShellGet
PackageManagement\Get-Package : No match was found for the specified search criteria and module names 'PowerShellGet'.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:2253 char:9
+         PackageManagement\Get-Package @PSBoundParameters | Microsoft. ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Microsoft.Power...lets.GetPackage:GetPackage) [Get-Package], Exception
    + FullyQualifiedErrorId : NoMatchFound,Microsoft.PowerShell.PackageManagement.Cmdlets.GetPackage
PS> Get-InstalledModule PackageManagement
PackageManagement\Get-Package : No match was found for the specified search criteria and module names 'PackageManagement'.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:2253 char:9
+         PackageManagement\Get-Package @PSBoundParameters | Microsoft. ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Microsoft.Power...lets.GetPackage:GetPackage) [Get-Package], Exception
    + FullyQualifiedErrorId : NoMatchFound,Microsoft.PowerShell.PackageManagement.Cmdlets.GetPackage

But, Get-Module works fine:

PS> Get-Module PowerShellGet

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     1.0.0.1    PowerShellGet                       {Find-Command, Find-DscResource, Find-Module, Find-RoleCapability...}
PS> Get-Module PackageManagement

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Binary     1.0.0.1    PackageManagement                   {Find-Package, Find-PackageProvider, Get-Package, Get-PackageProvider...}
petrsnd commented 5 years ago

@SydneyhSmith

I think I have the latest PowerShellGet.

PS> Install-Module PowerShellGet

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by
running the Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): y
WARNING: Version '1.0.0.1' of module 'PowerShellGet' is already installed at 'C:\Program
Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1'. To install version '2.2', run Install-Module and add the -Force parameter,
this command will install version '2.2' in side-by-side with version '1.0.0.1'.
petrsnd commented 5 years ago

Should I be using 2.2?

SydneyhSmith commented 5 years ago

@petrsnd yep, 2.2 is the latest version of PowerShellGet and should resolve your Find-Module issue. i.e. Install-Module PowerShellGet -Force -AllowClobber should do the trick

In regards to the behavior of Get-InstalledModule PowerShellGet, this is likely because Get-InstalledModule only returns modules installed using PowerShellGet while Get-Module searches for all modules...since the version of you had PowerShellGet is 1.0.0.1 this is likely the version that shipped "inbox" with PowerShell and was therefore never installed....A bit more confusing than it should be but hopefully that clarifies a bit

SydneyhSmith commented 5 years ago

Closing this issue as resolved by the latest version of PowerShellGet, if you are still hitting this issue let us know and we will re-open.

LaurentDardenne commented 2 years ago

I have the same problem with a local repository (FileSystem) :

$RepositoryPath = Join-Path $env:Temp -ChildPath 'PSLocalRepository'
$DevRepositoryPath = Join-Path $env:Temp -ChildPath 'PSDevLocalRepository'

Foreach( $path in $RepositoryPath,$DevRepositoryPath )
{ New-Item -Path $Path -ItemType 'Directory' -Force >$Null }

$Repositories=@(
  [PsCustomObject]@{
      name='TestLocalModule'
      publishlocation=$RepositoryPath
      sourcelocation=$RepositoryPath
  },
  [PsCustomObject]@{
      name='DevTestLocalModule'
      publishlocation=$DevRepositoryPath
      sourcelocation=$DevRepositoryPath
  },
  [PsCustomObject]@{
    name='MygetOnError'
    publishlocation='https://www.myget.org/F/ottomatt/api/v2/package'
     #wrong uri syntax
    sourcelocation='https://www.myget.org/F/ottomatt/api'
  }
)   
Foreach ($Repository in $Repositories) 
{
  $Name=$Repository.Name
  try{
    Get-PSRepository $Name -EA Stop >$null   
  }catch {
    if ($_.CategoryInfo.Category -ne 'ObjectNotFound')
    { throw $_ }
    else
    { 
      $Parameters=@{
        Name=$Name
        SourceLocation=$Repository.SourceLocation 
        PublishLocation=$Repository.PublishLocation
        InstallationPolicy='Trusted'
    }

    if (Test-Path env:CI)
    { Write-Output "Register repository '$($Repository.Name)'"  }
    Register-PSRepository @Parameters
   }
  }
}

$modules=@(
  @{Name='Pester';Version='5.3.3';Path=$RepositoryPath}
  @{Name='Pester';Version='5.3.2';Path=$RepositoryPath}

  @{Name='PnP.PowerShell';Version='1.11.0';Path=$RepositoryPath}

  @{Name='PnP.PowerShell';Version='1.11.37-nightly';Path=$DevRepositoryPath}
)

$ProgressPreference = 'SilentlyContinue'
Foreach ($Module in $modules)
{
  $ModuleFolder='{0}/{1}' -F $Module.Name,$Module.Version
  $NupkgFileLocation="https://www.powershellgallery.com/api/v2/package/$ModuleFolder"

  $Directory = Join-Path $Module.Path -ChildPath $Module.Name
  $FileName = '{0}.{1}.nupkg' -F $Module.Name,$Module.Version
  New-Item -Path $Directory -ItemType 'Directory' -Force >$Null 
   Invoke-WebRequest -Uri $NupkgFileLocation -OutFile $(Join-Path $Directory -ChildPath $FileName)
}
$ProgressPreference = 'Continue'   

$Repositories=@(Get-PSRepository|Select-Object -ExpandProperty Name)
Find-Module -name 'pnp.powershell' -Repository $Repositories

# Version              Name                                Repository           Description
# -------              ----                                ----------           -----------
# 1.11.37-nightly      PnP.PowerShell                      DevTestLocalModule   Microsoft 365 Patterns and Practices P...
# 1.11.0               PnP.PowerShell                      PSGallery            Microsoft 365 Patterns and Practices P...
# 1.11.0               PnP.PowerShell                      TestLocalModule      Microsoft 365 Patterns and Practices P...

Find-Module -name 'pnp.powershell' -Repository $Respositories -AllowPrerelease

# Version              Name                                Repository           Description
# -------              ----                                ----------           -----------
# 1.11.37-nightly      PnP.PowerShell                      DevTestLocalModule   Microsoft 365 Patterns and Practices P...
# 1.11.38-nightly      PnP.PowerShell                      PSGallery            Microsoft 365 Patterns and Practices P...
# 1.11.0               PnP.PowerShell                      TestLocalModule      Microsoft 365 Patterns and Practices P...

Whether or not -Allowprerelease is specified, all modules from local repositories are present in the result.

Version :

$psversiontable

Name                           Value
----                           -----
PSVersion                      5.1.19041.1682
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.1682
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

get-module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Con...
Manifest   3.0.0.0    Microsoft.PowerShell.Security       {ConvertFrom-SecureString, ConvertTo-SecureString, Get-Acl...
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}
Manifest   3.0.0.0    Microsoft.WSMan.Management          {Connect-WSMan, Disable-WSManCredSSP, Disconnect-WSMan, En...
Script     1.4.8.1    PackageManagement                   {Find-Package, Find-PackageProvider, Get-Package, Get-Pack...
Script     2.2.5      PowerShellGet                       {Find-Command, Find-DscResource, Find-Module, Find-RoleCap...
Script     2.0.0      PSReadline                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, Remove-PS...

Path of modules :

get-module powershellget -ListAvailable

    Répertoire : C:\Users\AccountName\Documents\WindowsPowerShell\Modules

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     2.2.5      PowerShellGet                       {Find-Command, Find-DSCResource, Find-Module, Find-RoleCap...

    Répertoire : C:\Program Files\WindowsPowerShell\Modules

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     1.0.0.1    PowerShellGet                       {Install-Module, Find-Module, Save-Module, Update-Module...}