Skip to content

Commit cdc5cf3

Browse files
JamesWTruherbergmeister
authored andcommitted
Harden dotnet --version handling (#1367)
dotnet --version can now emit a list of installed sdks as output *and* an error if the global.json file points to a version of the sdk which is *not* installed. However, the format of the new list with --version has a couple of spaces at the beginning of the line, so we need to be resilient against that.
1 parent 468212a commit cdc5cf3

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

build.psm1

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,6 @@ function Get-InstalledCLIVersion {
537537
# earlier versions of dotnet do not support --list-sdks, so we'll check the output
538538
# and use dotnet --version as a fallback
539539
$sdkList = & $script:DotnetExe --list-sdks 2>&1
540-
$sdkList = "Unknown option"
541540
if ( $sdkList -match "Unknown option" ) {
542541
$installedVersions = & $script:DotnetExe --version 2>$null
543542
}
@@ -621,9 +620,15 @@ function Get-DotnetExe
621620
# it's possible that there are multiples. Take the highest version we find
622621
# the problem is that invoking dotnet on a version which is lower than the specified
623622
# version in global.json will produce an error, so we can only take the dotnet which executes
623+
#
624+
# dotnet --version has changed its output, so we have to work much harder to determine what's installed.
625+
# dotnet --version can now emit a list of installed sdks as output *and* an error if the global.json
626+
# file points to a version of the sdk which is *not* installed. However, the format of the new list
627+
# with --version has a couple of spaces at the beginning of the line, so we need to be resilient
628+
# against that.
624629
$latestDotnet = $discoveredDotNet |
625630
Where-Object { try { & $_ --version 2>$null } catch { } } |
626-
Sort-Object { $pv = ConvertTo-PortableVersion (& $_ --version 2>$null); "$pv" } |
631+
Sort-Object { $pv = ConvertTo-PortableVersion (& $_ --version 2>$null| %{$_.Trim().Split()[0]}); "$pv" } |
627632
Select-Object -Last 1
628633
if ( $latestDotnet ) {
629634
$script:DotnetExe = $latestDotnet

0 commit comments

Comments
 (0)