Skip to content

Commit ffe499c

Browse files
committed
Harden dotnet --version handling
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 ca1b7c0 commit ffe499c

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

Diff for: build.psm1

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

0 commit comments

Comments
 (0)