Skip to content

Commit 514d6f6

Browse files
authored
Fix build.psm1 to find the required .NET SDK version when a higher version is installed (PowerShell#17299)
1 parent 44098e7 commit 514d6f6

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

build.psm1

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ function Start-PSBuild {
393393
}
394394

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

398398
If ($dotnetCLIInstalledVersion -ne $dotnetCLIRequiredVersion) {
399399
Write-Warning @"
@@ -2152,7 +2152,7 @@ function Start-PSBootstrap {
21522152
$dotNetExists = precheck 'dotnet' $null
21532153
$dotNetVersion = [string]::Empty
21542154
if($dotNetExists) {
2155-
$dotNetVersion = Get-LatestInstalledSDK
2155+
$dotNetVersion = Find-RequiredSDK $dotnetCLIRequiredVersion
21562156
}
21572157

21582158
if(!$dotNetExists -or $dotNetVersion -ne $dotnetCLIRequiredVersion -or $Force.IsPresent) {
@@ -2194,22 +2194,30 @@ function Start-PSBootstrap {
21942194
}
21952195
}
21962196

2197-
function Get-LatestInstalledSDK {
2197+
## If the required SDK version is found, return it.
2198+
## Otherwise, return the latest installed SDK version that can be found.
2199+
function Find-RequiredSDK {
2200+
param(
2201+
[Parameter(Mandatory, Position = 0)]
2202+
[string] $requiredSdkVersion
2203+
)
2204+
21982205
$output = Start-NativeExecution -sb { dotnet --list-sdks } -IgnoreExitcode 2> $null
21992206

2200-
$output | ForEach-Object {
2207+
$installedSdkVersions = $output | ForEach-Object {
22012208
# this splits strings like
22022209
# '6.0.202 [C:\Program Files\dotnet\sdk]'
22032210
# '7.0.100-preview.2.22153.17 [C:\Users\johndoe\AppData\Local\Microsoft\dotnet\sdk]'
22042211
# into version and path parts.
2205-
$version, $null = $_ -split '\s',2
2206-
try {
2207-
[System.Management.Automation.SemanticVersion]::new($version)
2208-
}
2209-
catch {
2210-
Write-Warning -Message "Unable to parse dotnet version semantically: $version"
2211-
}
2212-
} | Sort-Object -Descending | Select-Object -First 1
2212+
($_ -split '\s',2)[0]
2213+
}
2214+
2215+
if ($installedSdkVersions -contains $requiredSdkVersion) {
2216+
$requiredSdkVersion
2217+
}
2218+
else {
2219+
$installedSdkVersions | Sort-Object -Descending | Select-Object -First 1
2220+
}
22132221
}
22142222

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

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

0 commit comments

Comments
 (0)