Skip to content

Commit

Permalink
Fix build.psm1 to find the required .NET SDK version when a higher …
Browse files Browse the repository at this point in the history
…version is installed (#17299)
  • Loading branch information
daxian-dbw committed May 11, 2022
1 parent 44098e7 commit 514d6f6
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions build.psm1
Expand Up @@ -393,7 +393,7 @@ function Start-PSBuild {
}

# Verify if the dotnet in-use is the required version
$dotnetCLIInstalledVersion = Get-LatestInstalledSDK
$dotnetCLIInstalledVersion = Find-RequiredSDK $dotnetCLIRequiredVersion

If ($dotnetCLIInstalledVersion -ne $dotnetCLIRequiredVersion) {
Write-Warning @"
Expand Down Expand Up @@ -2152,7 +2152,7 @@ function Start-PSBootstrap {
$dotNetExists = precheck 'dotnet' $null
$dotNetVersion = [string]::Empty
if($dotNetExists) {
$dotNetVersion = Get-LatestInstalledSDK
$dotNetVersion = Find-RequiredSDK $dotnetCLIRequiredVersion
}

if(!$dotNetExists -or $dotNetVersion -ne $dotnetCLIRequiredVersion -or $Force.IsPresent) {
Expand Down Expand Up @@ -2194,22 +2194,30 @@ function Start-PSBootstrap {
}
}

function Get-LatestInstalledSDK {
## If the required SDK version is found, return it.
## Otherwise, return the latest installed SDK version that can be found.
function Find-RequiredSDK {
param(
[Parameter(Mandatory, Position = 0)]
[string] $requiredSdkVersion
)

$output = Start-NativeExecution -sb { dotnet --list-sdks } -IgnoreExitcode 2> $null

$output | ForEach-Object {
$installedSdkVersions = $output | ForEach-Object {
# this splits strings like
# '6.0.202 [C:\Program Files\dotnet\sdk]'
# '7.0.100-preview.2.22153.17 [C:\Users\johndoe\AppData\Local\Microsoft\dotnet\sdk]'
# into version and path parts.
$version, $null = $_ -split '\s',2
try {
[System.Management.Automation.SemanticVersion]::new($version)
}
catch {
Write-Warning -Message "Unable to parse dotnet version semantically: $version"
}
} | Sort-Object -Descending | Select-Object -First 1
($_ -split '\s',2)[0]
}

if ($installedSdkVersions -contains $requiredSdkVersion) {
$requiredSdkVersion
}
else {
$installedSdkVersions | Sort-Object -Descending | Select-Object -First 1
}
}

function Start-DevPowerShell {
Expand Down Expand Up @@ -2361,7 +2369,7 @@ function Find-Dotnet() {
if (precheck dotnet) {
# Must run from within repo to ensure global.json can specify the required SDK version
Push-Location $PSScriptRoot
$dotnetCLIInstalledVersion = Get-LatestInstalledSDK
$dotnetCLIInstalledVersion = Find-RequiredSDK $chosenDotNetVersion
Pop-Location

Write-Verbose -Message "Find-DotNet: dotnetCLIInstalledVersion = $dotnetCLIInstalledVersion; chosenDotNetVersion = $chosenDotNetVersion"
Expand Down

0 comments on commit 514d6f6

Please sign in to comment.