Update Find-Dotnet to find dotnet with compat SDK (#5341)

Fix #5260 - with this fix folks shouldn't hit the version mismatch check.

The approach is to see if the dotnet in the current PATH has a compatible SDK.  Folks will have a globall installed dotnet if they've installed VS, VSCode C# ext or have  installed the .NET Core SDK.  This verion may not have the SDK required by PSCore.  And the global cannot see user local dotnet SDK installs.  So if the global dotnet doesn't have the right SDK, we prepend the path to the user's local dotnet dir.

Also, updateed $dotnetCLIRequiredVersion to read its value from global.json so there is one less source of the truth (for the SDK version).
This commit is contained in:
Keith Hill 2017-11-09 17:28:39 -07:00 committed by Aditya Patwardhan
parent 73188ce27a
commit 34600c4100

View file

@ -3,7 +3,7 @@
$script:TestModulePathSeparator = [System.IO.Path]::PathSeparator
$dotnetCLIChannel = "release"
$dotnetCLIRequiredVersion = "2.0.2"
$dotnetCLIRequiredVersion = $(Get-Content $PSScriptRoot/global.json | ConvertFrom-Json).Sdk.Version
# Track if tags have been sync'ed
$tagsUpToDate = $false
@ -1703,13 +1703,23 @@ function Start-ResGen
function Find-Dotnet() {
$originalPath = $env:PATH
$dotnetPath = if ($Environment.IsWindows) {
"$env:LocalAppData\Microsoft\dotnet"
} else {
"$env:HOME/.dotnet"
}
$dotnetPath = if ($Environment.IsWindows) { "$env:LocalAppData\Microsoft\dotnet" } else { "$env:HOME/.dotnet" }
if (-not (precheck 'dotnet' "Could not find 'dotnet', appending $dotnetPath to PATH.")) {
# If there dotnet is already in the PATH, check to see if that version of dotnet can find the required SDK
# This is "typically" the globally installed dotnet
if (precheck dotnet) {
# Must run from within repo to ensure global.json can specify the required SDK version
Push-Location $PSScriptRoot
$dotnetCLIInstalledVersion = (dotnet --version)
Pop-Location
if ($dotnetCLIInstalledVersion -ne $dotnetCLIRequiredVersion) {
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
}
}
else {
Write-Warning "Could not find 'dotnet', appending $dotnetPath to PATH."
$env:PATH += [IO.Path]::PathSeparator + $dotnetPath
}