Skip to content

Commit 403d017

Browse files
mauroaGitHub Actions Autoformatter
andauthored
[dotnet][xma] Ensure we don't use DOTNET_ROOT and DOTNET_HOST_PATH in… (#18567)
… the Build Agent and remote tasks DOTNET_ROOT and DOTNET_HOST_PATH are being deprecated as a mechanism to store the location of dotnet. PATH will be used instead, so we should ensure that the existing code that makes usage of these variables is adapted to the new guidelines. More information: dotnet/roslyn@f454d69 dotnet/runtime#88754 (comment) Additionally, to avoid confusion, we are using a dedicate DOTNET_CUSTOM_PATH variable with the path of the dotnet used by the XMA Build Agent, so it can be used internally by the tasks without mixing it with the existing dotnet variables --------- Co-authored-by: GitHub Actions Autoformatter <github-actions-autoformatter@xamarin.com>
1 parent d1d3dcd commit 403d017

File tree

5 files changed

+94
-41
lines changed

5 files changed

+94
-41
lines changed

msbuild/Messaging/Xamarin.Messaging.Build/TaskRunner.cs

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.IO;
45
using System.Linq;
56
using System.Reflection;
@@ -10,24 +11,15 @@
1011

1112
namespace Xamarin.Messaging.Build {
1213
internal class TaskRunner : ITaskRunner {
13-
ITaskSerializer serializer;
14-
List<Type> tasks = new List<Type> ();
14+
static readonly ITracer tracer = Tracer.Get<TaskRunner> ();
15+
16+
readonly ITaskSerializer serializer;
17+
readonly List<Type> tasks = new List<Type> ();
1518

1619
internal TaskRunner (ITaskSerializer serializer)
1720
{
1821
this.serializer = serializer;
19-
20-
var sdkRootPath = Path.Combine (MessagingContext.GetXmaPath (), "SDKs");
21-
var dotnetPath = Path.Combine (sdkRootPath, "dotnet", "dotnet");
22-
23-
if (File.Exists (dotnetPath)) {
24-
Environment.SetEnvironmentVariable ("DOTNET_CUSTOM_HOME", Path.Combine (sdkRootPath, ".home"));
25-
} else {
26-
//In case the XMA dotnet has not been installed yet
27-
dotnetPath = "/usr/local/share/dotnet/dotnet";
28-
}
29-
30-
Environment.SetEnvironmentVariable ("DOTNET_HOST_PATH", dotnetPath);
22+
SetDotNetVariables ();
3123
}
3224

3325
internal IEnumerable<Type> Tasks => tasks.AsReadOnly ();
@@ -57,5 +49,68 @@ public ExecuteTaskResult Execute (string taskName, string inputs)
5749

5850
return result;
5951
}
52+
53+
void SetDotNetVariables ()
54+
{
55+
var xmaSdkRootPath = Path.Combine (MessagingContext.GetXmaPath (), "SDKs");
56+
var xmaDotNetRootPath = Path.Combine (xmaSdkRootPath, "dotnet");
57+
var xmaDotNetPath = default (string);
58+
59+
if (IsValidDotNetInstallation (xmaDotNetRootPath)) {
60+
//If the XMA dotnet is already installed, we use it and also declare a custom home for it (for NuGet restore and caches)
61+
Environment.SetEnvironmentVariable ("DOTNET_CUSTOM_HOME", Path.Combine (xmaSdkRootPath, ".home"));
62+
xmaDotNetPath = GetDotNetPath (xmaDotNetRootPath);
63+
} else {
64+
//In case the XMA dotnet has not been installed yet, we use the default dotnet installation
65+
xmaDotNetPath = GetDefaultDotNetPath ();
66+
xmaDotNetRootPath = Path.GetDirectoryName (xmaDotNetPath);
67+
}
68+
69+
var pathContent = GetPathContent ();
70+
//We add the XMA dotnet path first so it has priority over the default dotnet installation
71+
var newPathContent = $"{xmaDotNetRootPath}:{pathContent}";
72+
73+
//Override the PATH with the XMA dotnet in it, just in case it's used internally by dotnet
74+
Environment.SetEnvironmentVariable ("PATH", newPathContent);
75+
//Deprecated dotnet environment variable. We still preserve ir for backwards compatibility with other components that haven't deprecated it yet (like dotnet ILLink)
76+
Environment.SetEnvironmentVariable ("DOTNET_HOST_PATH", xmaDotNetPath);
77+
//Custom environment variable for internal iOS SDK usage
78+
Environment.SetEnvironmentVariable ("DOTNET_CUSTOM_PATH", xmaDotNetPath);
79+
80+
tracer.Info ($"Using dotnet: {xmaDotNetPath}");
81+
tracer.Info ($"Current PATH: {newPathContent}");
82+
}
83+
84+
string GetDefaultDotNetPath ()
85+
{
86+
var dotnetRootPath = "/usr/local/share/dotnet";
87+
88+
if (IsValidDotNetInstallation (dotnetRootPath)) {
89+
return GetDotNetPath (dotnetRootPath);
90+
}
91+
92+
var dotnetPath = "dotnet";
93+
var pathContent = GetPathContent ();
94+
var pathElements = pathContent.Split (new string [] { ":" }, StringSplitOptions.RemoveEmptyEntries);
95+
96+
foreach (var pathElement in pathElements) {
97+
try {
98+
if (IsValidDotNetInstallation (pathElement)) {
99+
dotnetPath = GetDotNetPath (pathElement);
100+
break;
101+
}
102+
} catch {
103+
//If we can't read a directory for any reason just skip it
104+
}
105+
}
106+
107+
return dotnetPath;
108+
}
109+
110+
string GetPathContent () => Environment.GetEnvironmentVariable ("PATH") ?? "";
111+
112+
bool IsValidDotNetInstallation (string rootPath) => File.Exists (GetDotNetPath (rootPath));
113+
114+
string GetDotNetPath (string rootPath) => Path.Combine (rootPath, "dotnet");
60115
}
61116
}

msbuild/Xamarin.MacDev.Tasks/Extensions/TaskExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,22 @@ namespace Microsoft.Build.Tasks {
55
public static class TaskExtensions {
66
public static bool ShouldExecuteRemotely (this Task task, string sessionId)
77
=> Environment.OSVersion.Platform == PlatformID.Win32NT && !string.IsNullOrEmpty (sessionId);
8+
9+
public static string GetDotNetPath (this Task task)
10+
{
11+
//Custom environment variable set by the XMA Build Agent
12+
var dotnetPath = Environment.GetEnvironmentVariable ("DOTNET_CUSTOM_PATH");
13+
14+
if (string.IsNullOrEmpty (dotnetPath)) {
15+
//Deprecated dotnet environment variable used for backwards compatibility
16+
dotnetPath = Environment.GetEnvironmentVariable ("DOTNET_HOST_PATH");
17+
}
18+
19+
if (string.IsNullOrEmpty (dotnetPath)) {
20+
dotnetPath = Environment.OSVersion.Platform == PlatformID.Win32NT ? "dotnet.exe" : "dotnet";
21+
}
22+
23+
return dotnetPath;
24+
}
825
}
926
}

msbuild/Xamarin.MacDev.Tasks/Tasks/BTouchTaskBase.cs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
using Microsoft.Build.Framework;
1010
using Microsoft.Build.Utilities;
11+
using Microsoft.Build.Tasks;
1112

1213
using Xamarin.Utils;
1314
using Xamarin.Localization.MSBuild;
@@ -75,26 +76,10 @@ public abstract class BTouchTaskBase : XamarinToolTask {
7576
[Required]
7677
public string ResponseFilePath { get; set; }
7778

78-
string DotNetPath {
79-
get {
80-
// Return the dotnet executable we're executing with.
81-
var dotnet_path = Environment.GetEnvironmentVariable ("DOTNET_HOST_PATH");
82-
if (!string.IsNullOrEmpty (dotnet_path))
83-
return dotnet_path;
84-
85-
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
86-
// This might happen when building from inside VS (design-time builds, etc.)
87-
return "dotnet.exe";
88-
}
89-
90-
throw new InvalidOperationException ($"DOTNET_HOST_PATH is not set");
91-
}
92-
}
93-
9479
protected override string ToolName {
9580
get {
9681
if (IsDotNet)
97-
return Path.GetFileName (DotNetPath);
82+
return Path.GetFileName (this.GetDotNetPath ());
9883

9984
return Path.GetFileNameWithoutExtension (ToolExe);
10085
}
@@ -108,7 +93,7 @@ protected override string GenerateFullPathToTool ()
10893
// system dotnet, which might not exist or not have the version we
10994
// need.
11095
if (IsDotNet)
111-
return DotNetPath;
96+
return this.GetDotNetPath ();
11297

11398
return Path.Combine (ToolPath, ToolExe);
11499
}

msbuild/Xamarin.MacDev.Tasks/Tasks/ComputeRemoteGeneratorPropertiesTaskBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55

66
using Microsoft.Build.Framework;
7+
using Microsoft.Build.Tasks;
78

89
using Xamarin.Localization.MSBuild;
910
using Xamarin.Utils;
@@ -93,9 +94,8 @@ void ComputeProperties ()
9394
var environment = default (Dictionary<string, string?>);
9495

9596
if (IsDotNet) {
96-
executable = Environment.GetEnvironmentVariable ("DOTNET_HOST_PATH");
97-
if (string.IsNullOrEmpty (executable))
98-
executable = "dotnet";
97+
executable = this.GetDotNetPath ();
98+
9999
arguments.Add ("build");
100100

101101
var customHome = Environment.GetEnvironmentVariable ("DOTNET_CUSTOM_HOME");

msbuild/Xamarin.MacDev.Tasks/Tasks/XamarinBuildTask.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Linq;
55
using Microsoft.Build.Framework;
6+
using Microsoft.Build.Tasks;
67
using Xamarin.Localization.MSBuild;
78
using Threading = System.Threading.Tasks;
89

@@ -35,12 +36,7 @@ protected string ComputeValueUsingTarget (string computeValueTarget, string targ
3536
";
3637
File.WriteAllText (projectPath, csproj);
3738

38-
var dotnetPath = Environment.GetEnvironmentVariable ("DOTNET_HOST_PATH");
39-
40-
if (string.IsNullOrEmpty (dotnetPath)) {
41-
dotnetPath = "dotnet";
42-
}
43-
39+
var dotnetPath = this.GetDotNetPath ();
4440
var environment = default (Dictionary<string, string>);
4541
var customHome = Environment.GetEnvironmentVariable ("DOTNET_CUSTOM_HOME");
4642

0 commit comments

Comments
 (0)