From da7b44c55c76d9efa5f0c0ce6da47b767bbeb2f3 Mon Sep 17 00:00:00 2001 From: Jonathan Pryor Date: Fri, 27 Mar 2020 18:30:57 -0400 Subject: [PATCH] [xabuild] Probe DOTNET_ROOT for .NET sdks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Context: https://github.com/xamarin/xamarin-android/pull/4467 Context: https://devdiv.visualstudio.com/DevDiv/_build/results?buildId=3590265&view=logs&j=96fd57f5-f69e-53c7-3d47-f67e6cf9b93e&t=7b19ceb3-907a-5a0d-b8ed-87e3f7335a58 When trying to integration test xamarin/Java.Interop#614, the `make all-tests` job fails: /usr/local/share/dotnet/sdk/2.2.402/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.TargetFrameworkInference.targets(137,5): error NETSDK1045: The current .NET SDK does not support targeting .NET Core 3.1. Either target .NET Core 2.2 or lower, or use a version of the .NET SDK that supports .NET Core 3.1. [/Users/builder/azdo/_work/2/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop.csproj] Particularly odd about this is that we have an explicit job to install .NET Core 3.1.100, so why is .NET Core 2.2.402 being used at all?! Turns Outâ„¢ that while .NET Core 3.1.100 was installed, it was installed *into a different prefix*; from the `install .NET Core 3.1.100` job: ##[debug]set DOTNET_ROOT=/Users/builder/azdo/_work/_tool/dotnet and from the `make jenkins` job output: DOTNET_ROOT = /Users/builder/azdo/_work/_tool/dotnet Thus, the `install .NET Core 3.1.100` job is installing .NET Core into `$HOME/azdo/_work/_tool/dotnet`, while `make all-tests` is only looking for .NET Core SDKs within `/usr/local/share/dotnet`, which does *not* contain .NET Core 3.1 at all. Where does the `make all-tests` path come from? It comes from `tools/xabuild`, which sets the `MSBuildSDKsPath` environment variable, and `xabuild` did not check `$DOTNET_ROOT`. Update `xabuild` so that `$DOTNET_ROOT` is also probed for .NET Core installation directories, and use the *latest* .NET Core SDK path found between `$DOTNET_ROOT` and `/usr/local/share/dotnet`. --- tools/xabuild/XABuildPaths.cs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/tools/xabuild/XABuildPaths.cs b/tools/xabuild/XABuildPaths.cs index d9acb757faf..cfe3a9ac141 100644 --- a/tools/xabuild/XABuildPaths.cs +++ b/tools/xabuild/XABuildPaths.cs @@ -142,6 +142,9 @@ public XABuildPaths () string programFiles = Environment.GetFolderPath (Environment.SpecialFolder.ProgramFilesX86); string prefix = Path.Combine (XamarinAndroidBuildOutput, "lib", "xamarin.android"); + string DOTNET_ROOT = Environment.GetEnvironmentVariable ("DOTNET_ROOT"); + string dotnetRootSdkDir = DOTNET_ROOT == null ? null : Path.Combine (DOTNET_ROOT, "sdk"); + if (IsWindows) { var instance = MSBuildLocator.QueryLatest (); VsInstallRoot = instance.VisualStudioRootPath; @@ -149,7 +152,7 @@ public XABuildPaths () MSBuildPath = Path.Combine (VsInstallRoot, "MSBuild"); MSBuildBin = Path.GetDirectoryName (instance.MSBuildPath); MSBuildConfig = Path.Combine (MSBuildBin, "MSBuild.exe.config"); - DotNetSdkPath = FindLatestDotNetSdk (Path.Combine (Environment.GetEnvironmentVariable ("ProgramW6432"), "dotnet", "sdk")); + DotNetSdkPath = FindLatestDotNetSdk (Path.Combine (Environment.GetEnvironmentVariable ("ProgramW6432"), "dotnet", "sdk"), dotnetRootSdkDir); MSBuildSdksPath = DotNetSdkPath ?? Path.Combine (MSBuildPath, "Sdks"); SystemFrameworks = Path.Combine (programFiles, "Reference Assemblies", "Microsoft", "Framework"); string msbuildDir = Path.GetDirectoryName (MSBuildBin); @@ -184,7 +187,7 @@ public XABuildPaths () throw new InvalidOperationException ("Unable to locate MSBuild binaries directory"); MSBuildConfig = Path.Combine (MSBuildBin, "MSBuild.dll.config"); - DotNetSdkPath = FindLatestDotNetSdk ("/usr/local/share/dotnet/sdk"); + DotNetSdkPath = FindLatestDotNetSdk ("/usr/local/share/dotnet/sdk", dotnetRootSdkDir); MSBuildSdksPath = DotNetSdkPath ?? Path.Combine (MSBuildBin, "Sdks"); SystemFrameworks = Path.Combine (mono, "xbuild-frameworks"); MonoSystemFrameworkRoot = mono; @@ -291,11 +294,14 @@ string RunPathsTargets (string pathsTargets, string target) } } - string FindLatestDotNetSdk (string dotNetPath) + string FindLatestDotNetSdk (params string[] dotNetPaths) { - if (Directory.Exists (dotNetPath)) { - Version latest = new Version (0,0); - string Sdk = null; + Version latest = new Version (0,0); + string Sdk = null; + + foreach (var dotNetPath in dotNetPaths) { + if (!Directory.Exists (dotNetPath)) + continue; foreach (var dir in Directory.EnumerateDirectories (dotNetPath)) { var version = GetVersionFromDirectory (dir); var sdksDir = Path.Combine (dir, "Sdks"); @@ -308,9 +314,8 @@ string FindLatestDotNetSdk (string dotNetPath) } } } - return Sdk; } - return null; + return Sdk; } static Version GetVersionFromDirectory(string dir)