Skip to content

Commit 1f44dac

Browse files
committed
[xabuild] Probe DOTNET_ROOT for .NET sdks
Context: #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 dotnet/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`.
1 parent 6e769fa commit 1f44dac

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

tools/xabuild/XABuildPaths.cs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,17 @@ public XABuildPaths ()
142142
string programFiles = Environment.GetFolderPath (Environment.SpecialFolder.ProgramFilesX86);
143143
string prefix = Path.Combine (XamarinAndroidBuildOutput, "lib", "xamarin.android");
144144

145+
string DOTNET_ROOT = Environment.GetEnvironmentVariable ("DOTNET_ROOT");
146+
string dotnetRootSdkDir = DOTNET_ROOT == null ? null : Path.Combine (DOTNET_ROOT, "sdk");
147+
145148
if (IsWindows) {
146149
var instance = MSBuildLocator.QueryLatest ();
147150
VsInstallRoot = instance.VisualStudioRootPath;
148151

149152
MSBuildPath = Path.Combine (VsInstallRoot, "MSBuild");
150153
MSBuildBin = Path.GetDirectoryName (instance.MSBuildPath);
151154
MSBuildConfig = Path.Combine (MSBuildBin, "MSBuild.exe.config");
152-
DotNetSdkPath = FindLatestDotNetSdk (Path.Combine (Environment.GetEnvironmentVariable ("ProgramW6432"), "dotnet", "sdk"));
155+
DotNetSdkPath = FindLatestDotNetSdk (Path.Combine (Environment.GetEnvironmentVariable ("ProgramW6432"), "dotnet", "sdk"), dotnetRootSdkDir);
153156
MSBuildSdksPath = DotNetSdkPath ?? Path.Combine (MSBuildPath, "Sdks");
154157
SystemFrameworks = Path.Combine (programFiles, "Reference Assemblies", "Microsoft", "Framework");
155158
string msbuildDir = Path.GetDirectoryName (MSBuildBin);
@@ -184,7 +187,7 @@ public XABuildPaths ()
184187
throw new InvalidOperationException ("Unable to locate MSBuild binaries directory");
185188

186189
MSBuildConfig = Path.Combine (MSBuildBin, "MSBuild.dll.config");
187-
DotNetSdkPath = FindLatestDotNetSdk ("/usr/local/share/dotnet/sdk");
190+
DotNetSdkPath = FindLatestDotNetSdk ("/usr/local/share/dotnet/sdk", dotnetRootSdkDir);
188191
MSBuildSdksPath = DotNetSdkPath ?? Path.Combine (MSBuildBin, "Sdks");
189192
SystemFrameworks = Path.Combine (mono, "xbuild-frameworks");
190193
MonoSystemFrameworkRoot = mono;
@@ -291,11 +294,14 @@ string RunPathsTargets (string pathsTargets, string target)
291294
}
292295
}
293296

294-
string FindLatestDotNetSdk (string dotNetPath)
297+
string FindLatestDotNetSdk (params string[] dotNetPaths)
295298
{
296-
if (Directory.Exists (dotNetPath)) {
297-
Version latest = new Version (0,0);
298-
string Sdk = null;
299+
Version latest = new Version (0,0);
300+
string Sdk = null;
301+
302+
foreach (var dotNetPath in dotNetPaths) {
303+
if (!Directory.Exists (dotNetPath))
304+
continue;
299305
foreach (var dir in Directory.EnumerateDirectories (dotNetPath)) {
300306
var version = GetVersionFromDirectory (dir);
301307
var sdksDir = Path.Combine (dir, "Sdks");
@@ -308,9 +314,8 @@ string FindLatestDotNetSdk (string dotNetPath)
308314
}
309315
}
310316
}
311-
return Sdk;
312317
}
313-
return null;
318+
return Sdk;
314319
}
315320

316321
static Version GetVersionFromDirectory(string dir)

0 commit comments

Comments
 (0)