question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PSResourceInfo object does not properly sort versions

See original GitHub issue

Prerequisites

  • Write a descriptive title.
  • Make sure you are able to repro it on the latest released version
  • Search the existing issues.

Steps to reproduce

I should be able to sort resources by their version but this does not work as expected. This does not work due to PowerShellGet 3.0.11-beta splitting up semantic versioning into two different properties Version [System.Version] and PrereleaseLabel [String] on the PSResourceInfo type.

PowerShellGet 3.0.11-beta already takes a dependency on Nuget.Versioning.dll which contains [NuGet.Versioning.NuGetVersion] type that can handle traditional [Version] as well as [SemanticVersion].

Related issue https://github.com/PowerShell/PowerShell/issues/2983, https://github.com/PowerShell/PowerShellGet/issues/83

cc: @jaykul

Expected behavior

Should be able to properly sort resources by version.

Actual behavior

Sorting does not take into account semantic versioning.

Error details

No response

Environment data

PS C:\> Get-Module -Name PowerShellGet

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Binary     3.0.11     beta       PowerShellGet                       {Find-PSResource, Get-InstalledPSResource, Get-PS…

PS C:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.2.0-preview.8
PSEdition                      Core
GitCommitId                    7.2.0-preview.8
OS                             Microsoft Windows 10.0.19043
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Jaykulcommented, Dec 16, 2021

tl;dr: These objects should have a SemanticVersion property so we can sort by that.

To make | Sort Version work the way it’s described, you would have to change the Version property.

But let’s try to think about this a different way. Imagine that you actually followed SemVer with this module…

Instead of this: image

You would have published:

Name          Version  Prerelease
----          -------  ----------
PowerShellGet 3.0.0.0  beta12    
PowerShellGet 3.0.0.0  beta11    
PowerShellGet 3.0.0.0  beta10    

So, if we had those three packages in a $Modules array and we wanted to invert the sort order so the newest is last, how would we do that? Currently, we can’t unless we create a new property to sort by:

Update-TypeData -MemberName SemanticVersion -MemberType ScriptProperty -Value {
  [System.Management.Automation.SemanticVersion]($this.Version, $this.PrereleaseLabel -join "-")
} -TypeName Microsoft.PowerShell.PowerShellGet.UtilClasses.PSResourceInfo 

So that we can do this:

$Modules | Sort SemanticVersion
1reaction
StevenBucher98commented, Aug 12, 2021

Thanks @ThomasNieto! We will make this change soon, we will try to get this in for the next release as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PSResourceInfo object does not properly sort versions #459
I should be able to sort resources by their version but this does not work as expected. This does not work due to...
Read more >
Complete the SemanticVersion work · Issue #2983
Can we improve the semantic version implementation for myget? #2354 ... PSResourceInfo object does not properly sort versions ...
Read more >
Sort a list that contains version numbers properly
Sort -Object then sorts the filtered tags as version numbers, which yields ... Version] instances; simply remove it first (not from the input ......
Read more >
Version field sort order not working properly
I have a document library with versioning, and I am tweaking the view through the UI. It appears that, right ootb, when we...
Read more >
PowerShell Training: Sorting Version Numbers
Sorting version numbers as strings can fail to order them correctly, but the [Version]. type accelerator can be used to sort them correctly....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found