@@ -393,7 +393,7 @@ function Start-PSBuild {
393
393
}
394
394
395
395
# Verify if the dotnet in-use is the required version
396
- $dotnetCLIInstalledVersion = Get-LatestInstalledSDK
396
+ $dotnetCLIInstalledVersion = Find-RequiredSDK $dotnetCLIRequiredVersion
397
397
398
398
If ($dotnetCLIInstalledVersion -ne $dotnetCLIRequiredVersion ) {
399
399
Write-Warning @"
@@ -2152,7 +2152,7 @@ function Start-PSBootstrap {
2152
2152
$dotNetExists = precheck ' dotnet' $null
2153
2153
$dotNetVersion = [string ]::Empty
2154
2154
if ($dotNetExists ) {
2155
- $dotNetVersion = Get-LatestInstalledSDK
2155
+ $dotNetVersion = Find-RequiredSDK $dotnetCLIRequiredVersion
2156
2156
}
2157
2157
2158
2158
if (! $dotNetExists -or $dotNetVersion -ne $dotnetCLIRequiredVersion -or $Force.IsPresent ) {
@@ -2194,22 +2194,30 @@ function Start-PSBootstrap {
2194
2194
}
2195
2195
}
2196
2196
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
+
2198
2205
$output = Start-NativeExecution - sb { dotnet -- list- sdks } - IgnoreExitcode 2> $null
2199
2206
2200
- $output | ForEach-Object {
2207
+ $installedSdkVersions = $ output | ForEach-Object {
2201
2208
# this splits strings like
2202
2209
# '6.0.202 [C:\Program Files\dotnet\sdk]'
2203
2210
# '7.0.100-preview.2.22153.17 [C:\Users\johndoe\AppData\Local\Microsoft\dotnet\sdk]'
2204
2211
# 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
+ }
2213
2221
}
2214
2222
2215
2223
function Start-DevPowerShell {
@@ -2361,7 +2369,7 @@ function Find-Dotnet() {
2361
2369
if (precheck dotnet) {
2362
2370
# Must run from within repo to ensure global.json can specify the required SDK version
2363
2371
Push-Location $PSScriptRoot
2364
- $dotnetCLIInstalledVersion = Get-LatestInstalledSDK
2372
+ $dotnetCLIInstalledVersion = Find-RequiredSDK $chosenDotNetVersion
2365
2373
Pop-Location
2366
2374
2367
2375
Write-Verbose - Message " Find-DotNet: dotnetCLIInstalledVersion = $dotnetCLIInstalledVersion ; chosenDotNetVersion = $chosenDotNetVersion "
0 commit comments