Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions build.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ function Start-PSBuild {
}

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

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 = Get-DotNetVersionInPath
}

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

function Get-LatestInstalledSDK {
function Get-DotNetVersionInPath {
Start-NativeExecution -sb {
dotnet --list-sdks | Select-String -Pattern '\d*.\d*.\d*(-\w*\.\d*.\d*.\d*)?' | ForEach-Object { [System.Management.Automation.SemanticVersion]::new($_.matches.value) } | Sort-Object -Descending | Select-Object -First 1
dotnet --version

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this will work. When running within the repo folder, global.json will be honored, so dotnet --version will returns messages like the following when the dotnet version doesn't match:

PS:15> dotnet --version
Could not execute because the application was not found or a compatible .NET SDK is not installed.
Possible reasons for this include:
  * You intended to execute a .NET program:
      The application '--version' does not exist.
  * You intended to execute a .NET SDK command:
      A compatible installed .NET SDK for global.json version [7.0.100-preview.2.22153.17] from [C:\arena\source\PowerShell\global.json] was not found.
      Install the [7.0.100-preview.2.22153.17] .NET SDK or update [C:\arena\source\PowerShell\global.json] with an installed .NET SDK:
        5.0.402 [C:\Program Files\dotnet\sdk]
        6.0.202 [C:\Program Files\dotnet\sdk]

@daxian-dbw Dongbo Wang (daxian-dbw) Apr 29, 2022 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid we still need dotnet --list-sdks, but instead of returning the highest version, we need to check if the returned versions contain the required version. So, I think part of the changes in #17198 that fixed the parsing is still needed.

} -IgnoreExitcode 2> $null
}

Expand Down Expand Up @@ -2349,20 +2349,29 @@ 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 = Get-DotNetVersionInPath
Pop-Location

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

if ($dotnetCLIInstalledVersion -ne $chosenDotNetVersion) {
Write-Warning "The 'dotnet' in the current path can't find SDK version ${dotnetCLIRequiredVersion}, prepending $dotnetPath to PATH."
# Globally installed dotnet doesn't have the required SDK version, prepend the user local dotnet location
$env:PATH = $dotnetPath + [IO.Path]::PathSeparator + $env:PATH
if (Test-Path $dotnetPath) {
$dotnetPathEntry = $dotnetPath + [IO.Path]::PathSeparator
if ($env:PATH -notmatch "^$([Regex]::Escape("$dotnetPathEntry"))") {
# Remove possible entries further down the list.
$env:PATH = $env:PATH -replace [Regex]::Escape("$dotnetPathEntry"),''
Write-Warning "The 'dotnet' in the current path can't find SDK version ${dotnetCLIRequiredVersion}, prepending $dotnetPath to PATH."
# Globally installed dotnet doesn't have the required SDK version, prepend the user local dotnet location
$env:PATH = $dotnetPathEntry + $env:PATH
}
}
}
}
else {
Write-Warning "Could not find 'dotnet', appending $dotnetPath to PATH."
$env:PATH += [IO.Path]::PathSeparator + $dotnetPath
if (Test-Path $dotnetPath) {
Write-Warning "Could not find 'dotnet', appending $dotnetPath to PATH."
$env:PATH += [IO.Path]::PathSeparator + $dotnetPath
}
}

if (-not (precheck 'dotnet' "Still could not find 'dotnet', restoring PATH.")) {
Expand Down