Skip to content

Commit 5872e57

Browse files
committed
[dotnet][xma] Ensure we don't use DOTNET_ROOT and DOTNET_HOST_PATH in 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
1 parent b68efbe commit 5872e57

File tree

4 files changed

+75
-24
lines changed

4 files changed

+75
-24
lines changed

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

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,15 @@
1010

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

1618
internal TaskRunner (ITaskSerializer serializer)
1719
{
1820
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);
21+
SetDotNetVariables();
3122
}
3223

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

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

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,13 @@ public abstract class BTouchTaskBase : XamarinToolTask {
7878
string DotNetPath {
7979
get {
8080
// 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;
81+
var dotnetPath = Environment.GetEnvironmentVariable ("DOTNET_CUSTOM_PATH");
8482

85-
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
86-
// This might happen when building from inside VS (design-time builds, etc.)
87-
return "dotnet.exe";
83+
if (!string.IsNullOrEmpty (dotnetPath)) {
84+
return dotnetPath;
8885
}
89-
90-
throw new InvalidOperationException ($"DOTNET_HOST_PATH is not set");
86+
87+
return Environment.OSVersion.Platform == PlatformID.Win32NT ? "dotnet.exe" : "dotnet";
9188
}
9289
}
9390

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void ComputeProperties ()
9393
var environment = default (Dictionary<string, string?>);
9494

9595
if (IsDotNet) {
96-
executable = Environment.GetEnvironmentVariable ("DOTNET_HOST_PATH");
96+
executable = Environment.GetEnvironmentVariable ("DOTNET_CUSTOM_PATH");
9797
if (string.IsNullOrEmpty (executable))
9898
executable = "dotnet";
9999
arguments.Add ("build");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected string ComputeValueUsingTarget (string computeValueTarget, string targ
3535
";
3636
File.WriteAllText (projectPath, csproj);
3737

38-
var dotnetPath = Environment.GetEnvironmentVariable ("DOTNET_HOST_PATH");
38+
var dotnetPath = Environment.GetEnvironmentVariable ("DOTNET_CUSTOM_PATH");
3939

4040
if (string.IsNullOrEmpty (dotnetPath)) {
4141
dotnetPath = "dotnet";

0 commit comments

Comments
 (0)