Skip to content

Commit

Permalink
[release/8.0.1xx-preview7] [dotnet][xma] Ensure we don't use DOTNET_R…
Browse files Browse the repository at this point in the history
…OOT and DOTNET_HOST_PATH in. (#18591)

. 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


Backport of #18567

---------

Co-authored-by: Mauro Agnoletti <mauro.agnoletti@gmail.com>
Co-authored-by: GitHub Actions Autoformatter <github-actions-autoformatter@xamarin.com>
  • Loading branch information
3 people authored Jul 27, 2023
1 parent 473b886 commit 67b5bdc
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 41 deletions.
83 changes: 69 additions & 14 deletions msbuild/Messaging/Xamarin.Messaging.Build/TaskRunner.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
Expand All @@ -10,24 +11,15 @@

namespace Xamarin.Messaging.Build {
internal class TaskRunner : ITaskRunner {
ITaskSerializer serializer;
List<Type> tasks = new List<Type> ();
static readonly ITracer tracer = Tracer.Get<TaskRunner> ();

readonly ITaskSerializer serializer;
readonly List<Type> tasks = new List<Type> ();

internal TaskRunner (ITaskSerializer serializer)
{
this.serializer = serializer;

var sdkRootPath = Path.Combine (MessagingContext.GetXmaPath (), "SDKs");
var dotnetPath = Path.Combine (sdkRootPath, "dotnet", "dotnet");

if (File.Exists (dotnetPath)) {
Environment.SetEnvironmentVariable ("DOTNET_CUSTOM_HOME", Path.Combine (sdkRootPath, ".home"));
} else {
//In case the XMA dotnet has not been installed yet
dotnetPath = "/usr/local/share/dotnet/dotnet";
}

Environment.SetEnvironmentVariable ("DOTNET_HOST_PATH", dotnetPath);
SetDotNetVariables ();
}

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

return result;
}

void SetDotNetVariables ()
{
var xmaSdkRootPath = Path.Combine (MessagingContext.GetXmaPath (), "SDKs");
var xmaDotNetRootPath = Path.Combine (xmaSdkRootPath, "dotnet");
var xmaDotNetPath = default (string);

if (IsValidDotNetInstallation (xmaDotNetRootPath)) {
//If the XMA dotnet is already installed, we use it and also declare a custom home for it (for NuGet restore and caches)
Environment.SetEnvironmentVariable ("DOTNET_CUSTOM_HOME", Path.Combine (xmaSdkRootPath, ".home"));
xmaDotNetPath = GetDotNetPath (xmaDotNetRootPath);
} else {
//In case the XMA dotnet has not been installed yet, we use the default dotnet installation
xmaDotNetPath = GetDefaultDotNetPath ();
xmaDotNetRootPath = Path.GetDirectoryName (xmaDotNetPath);
}

var pathContent = GetPathContent ();
//We add the XMA dotnet path first so it has priority over the default dotnet installation
var newPathContent = $"{xmaDotNetRootPath}:{pathContent}";

//Override the PATH with the XMA dotnet in it, just in case it's used internally by dotnet
Environment.SetEnvironmentVariable ("PATH", newPathContent);
//Deprecated dotnet environment variable. We still preserve ir for backwards compatibility with other components that haven't deprecated it yet (like dotnet ILLink)
Environment.SetEnvironmentVariable ("DOTNET_HOST_PATH", xmaDotNetPath);
//Custom environment variable for internal iOS SDK usage
Environment.SetEnvironmentVariable ("DOTNET_CUSTOM_PATH", xmaDotNetPath);

tracer.Info ($"Using dotnet: {xmaDotNetPath}");
tracer.Info ($"Current PATH: {newPathContent}");
}

string GetDefaultDotNetPath ()
{
var dotnetRootPath = "/usr/local/share/dotnet";

if (IsValidDotNetInstallation (dotnetRootPath)) {
return GetDotNetPath (dotnetRootPath);
}

var dotnetPath = "dotnet";
var pathContent = GetPathContent ();
var pathElements = pathContent.Split (new string [] { ":" }, StringSplitOptions.RemoveEmptyEntries);

foreach (var pathElement in pathElements) {
try {
if (IsValidDotNetInstallation (pathElement)) {
dotnetPath = GetDotNetPath (pathElement);
break;
}
} catch {
//If we can't read a directory for any reason just skip it
}
}

return dotnetPath;
}

string GetPathContent () => Environment.GetEnvironmentVariable ("PATH") ?? "";

bool IsValidDotNetInstallation (string rootPath) => File.Exists (GetDotNetPath (rootPath));

string GetDotNetPath (string rootPath) => Path.Combine (rootPath, "dotnet");
}
}
17 changes: 17 additions & 0 deletions msbuild/Xamarin.MacDev.Tasks/Extensions/TaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,22 @@ namespace Microsoft.Build.Tasks {
public static class TaskExtensions {
public static bool ShouldExecuteRemotely (this Task task, string sessionId)
=> Environment.OSVersion.Platform == PlatformID.Win32NT && !string.IsNullOrEmpty (sessionId);

public static string GetDotNetPath (this Task task)
{
//Custom environment variable set by the XMA Build Agent
var dotnetPath = Environment.GetEnvironmentVariable ("DOTNET_CUSTOM_PATH");

if (string.IsNullOrEmpty (dotnetPath)) {
//Deprecated dotnet environment variable used for backwards compatibility
dotnetPath = Environment.GetEnvironmentVariable ("DOTNET_HOST_PATH");
}

if (string.IsNullOrEmpty (dotnetPath)) {
dotnetPath = Environment.OSVersion.Platform == PlatformID.Win32NT ? "dotnet.exe" : "dotnet";
}

return dotnetPath;
}
}
}
21 changes: 3 additions & 18 deletions msbuild/Xamarin.MacDev.Tasks/Tasks/BTouchTaskBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using Microsoft.Build.Tasks;

using Xamarin.Utils;
using Xamarin.Localization.MSBuild;
Expand Down Expand Up @@ -75,26 +76,10 @@ public abstract class BTouchTaskBase : XamarinToolTask {
[Required]
public string ResponseFilePath { get; set; }

string DotNetPath {
get {
// Return the dotnet executable we're executing with.
var dotnet_path = Environment.GetEnvironmentVariable ("DOTNET_HOST_PATH");
if (!string.IsNullOrEmpty (dotnet_path))
return dotnet_path;

if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
// This might happen when building from inside VS (design-time builds, etc.)
return "dotnet.exe";
}

throw new InvalidOperationException ($"DOTNET_HOST_PATH is not set");
}
}

protected override string ToolName {
get {
if (IsDotNet)
return Path.GetFileName (DotNetPath);
return Path.GetFileName (this.GetDotNetPath ());

return Path.GetFileNameWithoutExtension (ToolExe);
}
Expand All @@ -108,7 +93,7 @@ protected override string GenerateFullPathToTool ()
// system dotnet, which might not exist or not have the version we
// need.
if (IsDotNet)
return DotNetPath;
return this.GetDotNetPath ();

return Path.Combine (ToolPath, ToolExe);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.IO;

using Microsoft.Build.Framework;
using Microsoft.Build.Tasks;

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

if (IsDotNet) {
executable = Environment.GetEnvironmentVariable ("DOTNET_HOST_PATH");
if (string.IsNullOrEmpty (executable))
executable = "dotnet";
executable = this.GetDotNetPath ();

arguments.Add ("build");

var customHome = Environment.GetEnvironmentVariable ("DOTNET_CUSTOM_HOME");
Expand Down
8 changes: 2 additions & 6 deletions msbuild/Xamarin.MacDev.Tasks/Tasks/XamarinBuildTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Tasks;
using Xamarin.Localization.MSBuild;
using Threading = System.Threading.Tasks;

Expand Down Expand Up @@ -35,12 +36,7 @@ protected string ComputeValueUsingTarget (string computeValueTarget, string targ
";
File.WriteAllText (projectPath, csproj);

var dotnetPath = Environment.GetEnvironmentVariable ("DOTNET_HOST_PATH");

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

var dotnetPath = this.GetDotNetPath ();
var environment = default (Dictionary<string, string>);
var customHome = Environment.GetEnvironmentVariable ("DOTNET_CUSTOM_HOME");

Expand Down

6 comments on commit 67b5bdc

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💻 [CI Build] Windows Integration Tests passed 💻

All Windows Integration Tests passed.

Pipeline on Agent
Hash: 67b5bdcb59fc9bb580dcf34ea59fd0c2478cfc4a [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💻 [CI Build] Tests on macOS M1 - Mac Ventura (13.0) passed 💻

All tests on macOS M1 - Mac Ventura (13.0) passed.

Pipeline on Agent
Hash: [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💻 [CI Build] Tests on macOS M1 - Mac Big Sur (11.5) passed 💻

All tests on macOS M1 - Mac Big Sur (11.5) passed.

Pipeline on Agent
Hash: [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ API diff for current PR / commit

NET (empty diffs)
  • iOS: (empty diff detected)
  • tvOS: (empty diff detected)
  • MacCatalyst: (empty diff detected)
  • macOS: (empty diff detected)

❗ API diff vs stable (Breaking changes)

.NET (:heavy_exclamation_mark: Breaking changes :heavy_exclamation_mark:)
  • iOS: vsdrops gist (:heavy_exclamation_mark: Breaking changes :heavy_exclamation_mark:)
  • tvOS: vsdrops gist (:heavy_exclamation_mark: Breaking changes :heavy_exclamation_mark:)
  • MacCatalyst: vsdrops gist (:heavy_exclamation_mark: Breaking changes :heavy_exclamation_mark:)
  • macOS: vsdrops gist (:heavy_exclamation_mark: Breaking changes :heavy_exclamation_mark:)
  • Microsoft.iOS vs Microsoft.MacCatalyst: vsdrops gist

ℹ️ Generator diff

Generator Diff: vsdrops (html) vsdrops (raw diff) gist (raw diff) - Please review changes)

Pipeline on Agent
Hash: 67b5bdcb59fc9bb580dcf34ea59fd0c2478cfc4a [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📚 [CI Build] Artifacts 📚

Packages generated

View packages

Pipeline on Agent
Hash: [CI build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀 [CI Build] Test results 🚀

Test results

✅ All tests passed on VSTS: simulator tests.

🎉 All 92 tests passed 🎉

Tests counts

⚠️ bcl: No tests selected. Html Report (VSDrops) Download
✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests: All 1 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ framework: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 1 tests passed. Html Report (VSDrops) Download
✅ interdependent_binding_projects: All 4 tests passed. Html Report (VSDrops) Download
⚠️ install_source: No tests selected. Html Report (VSDrops) Download
✅ introspection: All 4 tests passed. Html Report (VSDrops) Download
✅ linker: All 40 tests passed. Html Report (VSDrops) Download
⚠️ mac_binding_project: No tests selected. Html Report (VSDrops) Download
⚠️ mmp: No tests selected. Html Report (VSDrops) Download
⚠️ mononative: No tests selected. Html Report (VSDrops) Download
✅ monotouch: All 26 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
⚠️ mtouch: No tests selected. Html Report (VSDrops) Download
⚠️ xammac: No tests selected. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

Pipeline on Agent
Hash: 67b5bdcb59fc9bb580dcf34ea59fd0c2478cfc4a [CI build]

Please sign in to comment.