Winget Client Powershell - Winget.Client.psm1 Import fails on WinGet preview versions
Brief description of your issue
The WinGet.Client Powershell Module can not be imported if the installed winget version is a preview version.
Steps to reproduce
- Install a WinGet preview version
winget --version
v1.2.3411-preview
- clone winget repository
- try to import WinGet.Client Powershell module
Import-Module C:\Programming\wingetTest\Microsoft.WinGet.Client.psm1
Expected behavior
successful module import
Actual behavior
Import-Module : Requires Windows Package Manager 1.1.12653 or later to be installed.
In Zeile:1 Zeichen:1
+ Import-Module C:\Programming\wingetTest\Microsoft.WinGet.Client.psm1
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [Import-Module], WinGetVersionMismatch
+ FullyQualifiedErrorId : Requires Windows Package Manager 1.1.12653 or later to be installed.,Microsoft.PowerShell.Commands.ImportModuleCommand
Environment
Windows Package Manager (Preview) v1.2.3411-preview
Windows: Windows.Desktop v10.0.19043.1415
Package: Microsoft.DesktopAppInstaller v1.17.3411.0
Looks like I found the issue:
The import fails, because an error is thrown in Get-Version.ps1 in the line
$Result = [version](& "winget" $WinGetArgs).trimstart("v") .
The cast of winget --version into an object of type System.Version fails in case of a preview version (e.g. "1.2.3411-preview") as the constructor of System.Version does not accept a string of this format.
A string containing the major, minor, build, and revision numbers, where each number is delimited with a period character ('.'). MS Docs
As a consequence version "0.0.0.0" gets returned by the catch block and WinGet.Client reports a version mismatch.
Good catch! We've recently re-introduced "-preview" at the end of the versioning for preview versions.
This issue has been resolved with the latest version of the WinGet Client and the Microsoft.WinGet.Client module.