Skip to content

Commit c19ae0a

Browse files
committed
harden search for dotnet
Sometimes you can't even invoke dotnet if the version is too low
1 parent 8a7ea47 commit c19ae0a

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

build.psm1

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -502,13 +502,19 @@ function Get-DotnetExe
502502
$discoveredDotnet = Get-Command -CommandType Application dotnet -ErrorAction SilentlyContinu
503503
if ( $discoveredDotnet ) {
504504
# it's possible that there are multiples. Take the highest version we find
505-
$latestDotnet = $discoveredDotNet | Sort-object { [version](& $_ --version) } | Select-Object -Last 1
506-
Write-Verbose -Verbose "Found dotnet here: $latestDotnet"
507-
$script:DotnetExe = $latestDotnet
508-
return $latestDotnet
509-
}
510-
# it's not in the path, try harder to find it
511-
# check the usual places
505+
# the problem is that invoking dotnet on a version which is lower than the specified
506+
# version in global.json will produce an error, so we can only take the dotnet which executes
507+
$latestDotnet = $discoveredDotNet |
508+
Where-Object { try { & $_ --version } catch { } } |
509+
Sort-Object { [version](& $_ --version) } |
510+
Select-Object -Last 1
511+
if ( $latestDotnet ) {
512+
Write-Verbose -Verbose "Found dotnet here: $latestDotnet"
513+
$script:DotnetExe = $latestDotnet
514+
return $latestDotnet
515+
}
516+
}
517+
# it's not in the path, try harder to find it by checking some usual places
512518
if ( ! (test-path variable:IsWindows) -or $IsWindows ) {
513519
$dotnetHuntPath = "$HOME\AppData\Local\Microsoft\dotnet\dotnet.exe"
514520
Write-Verbose -Verbose "checking Windows $dotnetHuntPath"

0 commit comments

Comments
 (0)