From 34600c410008dcc021a4495c8ed401ed63d2f560 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Thu, 9 Nov 2017 17:28:39 -0700 Subject: [PATCH] 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). --- build.psm1 | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/build.psm1 b/build.psm1 index ac182a9c2..473d3e08d 100644 --- a/build.psm1 +++ b/build.psm1 @@ -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 }