Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] Support launching net taskhost #11393

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Build/BackEnd/Components/Communications/NodeLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public void ShutdownComponent()
public Process Start(string msbuildLocation, string commandLineArgs, int nodeId)
{
// Disable MSBuild server for a child process.
// In case of starting msbuild server it prevents an infinite recurson. In case of starting msbuild node we also do not want this variable to be set.
// In case of starting msbuild server it prevents an infinite recursion. In case of starting msbuild node we also do not want this variable to be set.
return DisableMSBuildServer(() => StartInternal(msbuildLocation, commandLineArgs));
}

/// <summary>
/// Creates a new MSBuild process
/// Creates new MSBuild or dotnet process.
/// </summary>
private Process StartInternal(string msbuildLocation, string commandLineArgs)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ protected void ShutdownAllNodes(bool nodeReuse, NodeContextTerminateDelegate ter
/// <summary>
/// Finds or creates a child processes which can act as a node.
/// </summary>
protected IList<NodeContext> GetNodes(string msbuildLocation,
protected IList<NodeContext> GetNodes(
string msbuildExecutableLocation,
string commandLineArgs,
int nextNodeId,
INodePacketFactory factory,
Expand All @@ -199,19 +200,19 @@ protected IList<NodeContext> GetNodes(string msbuildLocation,
}
#endif

if (String.IsNullOrEmpty(msbuildLocation))
if (String.IsNullOrEmpty(msbuildExecutableLocation))
{
msbuildLocation = _componentHost.BuildParameters.NodeExeLocation;
msbuildExecutableLocation = _componentHost.BuildParameters.NodeExeLocation;
}

if (String.IsNullOrEmpty(msbuildLocation))
if (String.IsNullOrEmpty(msbuildExecutableLocation))
{
string msbuildExeName = Environment.GetEnvironmentVariable("MSBUILD_EXE_NAME");

if (!String.IsNullOrEmpty(msbuildExeName))
{
// we assume that MSBUILD_EXE_NAME is, in fact, just the name.
msbuildLocation = Path.Combine(msbuildExeName, ".exe");
msbuildExecutableLocation = Path.Combine(msbuildExeName, ".exe");
}
}

Expand All @@ -226,7 +227,7 @@ protected IList<NodeContext> GetNodes(string msbuildLocation,
if (_componentHost.BuildParameters.EnableNodeReuse)
{
IList<Process> possibleRunningNodesList;
(expectedProcessName, possibleRunningNodesList) = GetPossibleRunningNodes(msbuildLocation);
(expectedProcessName, possibleRunningNodesList) = GetPossibleRunningNodes(msbuildExecutableLocation);
possibleRunningNodes = new ConcurrentQueue<Process>(possibleRunningNodesList);

if (possibleRunningNodesList.Count > 0)
Expand Down Expand Up @@ -317,13 +318,13 @@ bool StartNewNode(int nodeId)
// It's also a waste of time when we attempt several times to launch multiple MSBuildTaskHost.exe (CLR2 TaskHost)
// nodes because we should never be able to connect in this case.
string taskHostNameForClr2TaskHost = Path.GetFileNameWithoutExtension(NodeProviderOutOfProcTaskHost.TaskHostNameForClr2TaskHost);
if (Path.GetFileNameWithoutExtension(msbuildLocation).Equals(taskHostNameForClr2TaskHost, StringComparison.OrdinalIgnoreCase))
if (Path.GetFileNameWithoutExtension(msbuildExecutableLocation).Equals(taskHostNameForClr2TaskHost, StringComparison.OrdinalIgnoreCase))
{
if (FrameworkLocationHelper.GetPathToDotNetFrameworkV35(DotNetFrameworkArchitecture.Current) == null)
{
CommunicationsUtilities.Trace(
"Failed to launch node from {0}. The required .NET Framework v3.5 is not installed or enabled. CommandLine: {1}",
msbuildLocation,
msbuildExecutableLocation,
commandLineArgs);

string nodeFailedToLaunchError = ResourceUtilities.GetResourceString("TaskHostNodeFailedToLaunchErrorCodeNet35NotInstalled");
Expand All @@ -333,7 +334,7 @@ bool StartNewNode(int nodeId)
#endif
// Create the node process
INodeLauncher nodeLauncher = (INodeLauncher)_componentHost.GetComponent(BuildComponentType.NodeLauncher);
Process msbuildProcess = nodeLauncher.Start(msbuildLocation, commandLineArgs, nodeId);
Process msbuildProcess = nodeLauncher.Start(msbuildExecutableLocation, commandLineArgs, nodeId);
_processesToIgnore.TryAdd(GetProcessesToIgnoreKey(hostHandshake, msbuildProcess.Id), default);

// Note, when running under IMAGEFILEEXECUTIONOPTIONS registry key to debug, the process ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ internal class NodeProviderOutOfProcTaskHost : NodeProviderOutOfProcBase, INodeP
/// </summary>
private static string s_baseTaskHostPathArm64;

/// <summary>
/// Store the NET path for MSBuildTaskHost so that we don't have to keep recalculating it.
/// </summary>
private static string s_baseTaskHostPathNet;

/// <summary>
/// Store the path for the 32-bit MSBuildTaskHost so that we don't have to keep re-calculating it.
/// </summary>
Expand Down Expand Up @@ -385,12 +390,9 @@ internal static string GetTaskHostNameFromHostContext(HandshakeOptions hostConte
{
s_msbuildName = Environment.GetEnvironmentVariable("MSBUILD_EXE_NAME");

if (s_msbuildName == null)
{
s_msbuildName = (hostContext & HandshakeOptions.NET) == HandshakeOptions.NET
? "MSBuild.dll"
s_msbuildName ??= (hostContext & HandshakeOptions.NET) == HandshakeOptions.NET
? "dotnet.exe"
: "MSBuild.exe";
}
}

return s_msbuildName;
Expand All @@ -399,26 +401,28 @@ internal static string GetTaskHostNameFromHostContext(HandshakeOptions hostConte

/// <summary>
/// Given a TaskHostContext, return the appropriate location of the
/// executable (MSBuild or MSBuildTaskHost) that we wish to use, or null
/// if that location cannot be resolved.
/// executable (MSBuild, MSBuildTaskHost or dotnet) and path to MSBuild.dll if we want to use a custom one.
/// null is returned if executable cannot be resolved.
/// </summary>
internal static string GetMSBuildLocationFromHostContext(HandshakeOptions hostContext)
internal static (string msbuildExcutable, string msbuildAssemblyPath) GetMSBuildLocationFromHostContext(HandshakeOptions hostContext)
{
string toolName = GetTaskHostNameFromHostContext(hostContext);
string toolPath = null;
string msbuildAssemblyPath = null;

s_baseTaskHostPath = BuildEnvironmentHelper.Instance.MSBuildToolsDirectory32;
s_baseTaskHostPath64 = BuildEnvironmentHelper.Instance.MSBuildToolsDirectory64;
s_baseTaskHostPathArm64 = BuildEnvironmentHelper.Instance.MSBuildToolsDirectoryArm64;
s_baseTaskHostPathNet = BuildEnvironmentHelper.Instance.MSBuildToolsDirectoryNET;

ErrorUtilities.VerifyThrowInternalErrorUnreachable((hostContext & HandshakeOptions.TaskHost) == HandshakeOptions.TaskHost);
ErrorUtilities.VerifyThrowInternalErrorUnreachable(IsHandshakeOptionEnabled(HandshakeOptions.TaskHost));

if ((hostContext & HandshakeOptions.Arm64) == HandshakeOptions.Arm64 && (hostContext & HandshakeOptions.CLR2) == HandshakeOptions.CLR2)
if (IsHandshakeOptionEnabled(HandshakeOptions.Arm64) && IsHandshakeOptionEnabled(HandshakeOptions.CLR2))
{
// Unsupported, throw.
ErrorUtilities.ThrowInternalError("ARM64 CLR2 task hosts are not supported.");
}
else if ((hostContext & HandshakeOptions.X64) == HandshakeOptions.X64 && (hostContext & HandshakeOptions.CLR2) == HandshakeOptions.CLR2)
else if (IsHandshakeOptionEnabled(HandshakeOptions.X64) && IsHandshakeOptionEnabled(HandshakeOptions.CLR2))
{
if (s_pathToX64Clr2 == null)
{
Expand All @@ -432,7 +436,7 @@ internal static string GetMSBuildLocationFromHostContext(HandshakeOptions hostCo

toolPath = s_pathToX64Clr2;
}
else if ((hostContext & HandshakeOptions.CLR2) == HandshakeOptions.CLR2)
else if (IsHandshakeOptionEnabled(HandshakeOptions.CLR2))
{
if (s_pathToX32Clr2 == null)
{
Expand All @@ -445,40 +449,43 @@ internal static string GetMSBuildLocationFromHostContext(HandshakeOptions hostCo

toolPath = s_pathToX32Clr2;
}
else if ((hostContext & HandshakeOptions.X64) == HandshakeOptions.X64)
else if (IsHandshakeOptionEnabled(HandshakeOptions.X64) && !IsHandshakeOptionEnabled(HandshakeOptions.NET))
{
if (s_pathToX64Clr4 == null)
{
s_pathToX64Clr4 = s_baseTaskHostPath64;
}
s_pathToX64Clr4 ??= s_baseTaskHostPath64;

toolPath = s_pathToX64Clr4;
}
else if ((hostContext & HandshakeOptions.Arm64) == HandshakeOptions.Arm64)
else if (IsHandshakeOptionEnabled(HandshakeOptions.Arm64))
{
if (s_pathToArm64Clr4 == null)
{
s_pathToArm64Clr4 = s_baseTaskHostPathArm64;
}
s_pathToArm64Clr4 ??= s_baseTaskHostPathArm64;

toolPath = s_pathToArm64Clr4;
}
else
else if (IsHandshakeOptionEnabled(HandshakeOptions.NET))
{
if (s_pathToX32Clr4 == null)
// if we want some flexibility in the future, we can add a new environment variable for this.
var envTaskHostPathNet = Environment.GetEnvironmentVariable("DOTNET_HOST_PATH");
if (envTaskHostPathNet != null && FileUtilities.FileExistsNoThrow(Path.Combine(envTaskHostPathNet, toolName)))
{
s_pathToX32Clr4 = s_baseTaskHostPath;
s_baseTaskHostPathNet = envTaskHostPathNet;
}

toolPath = s_pathToX32Clr4;
// TODO Get path to msbuild.dll
msbuildAssemblyPath = Path.Combine(BuildEnvironmentHelper.Instance.MSBuildAssemblyDirectory, "MSBuild.dll");
toolPath = s_baseTaskHostPathNet;
}

if (toolName != null && toolPath != null)
else
{
return Path.Combine(toolPath, toolName);
s_pathToX32Clr4 ??= s_baseTaskHostPath;

toolPath = s_pathToX32Clr4;
}

return null;
return toolName != null && toolPath != null
? (msbuildExcutable: Path.Combine(toolPath, toolName), msbuildAssemblyPath)
: (msbuildExcutable: null, null);

bool IsHandshakeOptionEnabled(HandshakeOptions option) => (hostContext & option) == option;
}

/// <summary>
Expand Down Expand Up @@ -536,24 +543,33 @@ internal bool CreateNode(HandshakeOptions hostContext, INodePacketFactory factor
return false;
}

// Start the new process. We pass in a node mode with a node number of 2, to indicate that we
// want to start up an MSBuild task host node.
string commandLineArgs = $" /nologo /nodemode:2 /nodereuse:{ComponentHost.BuildParameters.EnableNodeReuse} /low:{ComponentHost.BuildParameters.LowPriority} ";

string msbuildLocation = GetMSBuildLocationFromHostContext(hostContext);
(string msbuildExecutable, string msbuildAssemblyLocation) = GetMSBuildLocationFromHostContext(hostContext);

// we couldn't even figure out the location we're trying to launch ... just go ahead and fail.
if (msbuildLocation == null)
if (msbuildExecutable == null)
{
return false;
}

CommunicationsUtilities.Trace("For a host context of {0}, spawning executable from {1}.", hostContext.ToString(), msbuildLocation ?? "MSBuild.exe");
string commandLineArgs;
if (msbuildAssemblyLocation != null)
{
// For dotnet.exe, the dll path must come first, then -- to separate application arguments
commandLineArgs = $"\"{msbuildAssemblyLocation}\" -- /nodemode:2 ";
}
else
{
// Start the new process. We pass in a node mode with a node number of 2, to indicate that we
// want to start up an MSBuild task host node.
commandLineArgs = $"/nologo /nodemode:2 /nodereuse:{ComponentHost.BuildParameters.EnableNodeReuse} /low:{ComponentHost.BuildParameters.LowPriority}";
}

CommunicationsUtilities.Trace("For a host context of {0}, spawning executable from {1}.", hostContext.ToString(), msbuildExecutable ?? "MSBuild.exe");

// There is always one task host per host context so we always create just 1 one task host node here.
int nodeId = (int)hostContext;
IList<NodeContext> nodeContexts = GetNodes(
msbuildLocation,
msbuildExecutable,
commandLineArgs,
nodeId,
this,
Expand Down
10 changes: 6 additions & 4 deletions src/Build/BackEnd/TaskExecutionHost/TaskExecutionHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,11 +332,13 @@ public bool InitializeForBatch(TaskLoggingContext loggingContext, ItemBucket bat
return false;
}

string realTaskAssemblyLoaction = TaskInstance.GetType().Assembly.Location;
if (!string.IsNullOrWhiteSpace(realTaskAssemblyLoaction) &&
realTaskAssemblyLoaction != _taskFactoryWrapper.TaskFactoryLoadedType.Path)
// TODO ask why for net task host it returns false net472\MSBuild\Current\Bin\Microsoft.Build.dll instead of path to a custom task.
// Interestingly TaskInstance._taskType contains the correct path.
string realTaskAssemblyLocation = TaskInstance.GetType().Assembly.Location;
if (!string.IsNullOrWhiteSpace(realTaskAssemblyLocation) &&
realTaskAssemblyLocation != _taskFactoryWrapper.TaskFactoryLoadedType.Path)
{
_taskLoggingContext.LogComment(MessageImportance.Normal, "TaskAssemblyLocationMismatch", realTaskAssemblyLoaction, _taskFactoryWrapper.TaskFactoryLoadedType.Path);
_taskLoggingContext.LogComment(MessageImportance.Normal, "TaskAssemblyLocationMismatch", realTaskAssemblyLocation, _taskFactoryWrapper.TaskFactoryLoadedType.Path);
}

TaskInstance.BuildEngine = _buildEngine;
Expand Down
2 changes: 1 addition & 1 deletion src/Build/Evaluation/IntrinsicFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ internal static bool DoesTaskHostExist(string runtime, string architecture)
parameters.Add(XMakeAttributes.architecture, architecture);

HandshakeOptions desiredContext = CommunicationsUtilities.GetHandshakeOptions(taskHost: true, taskHostParameters: parameters);
string taskHostLocation = NodeProviderOutOfProcTaskHost.GetMSBuildLocationFromHostContext(desiredContext);
string taskHostLocation = NodeProviderOutOfProcTaskHost.GetMSBuildLocationFromHostContext(desiredContext).msbuildExcutable;

if (taskHostLocation != null && FileUtilities.FileExistsNoThrow(taskHostLocation))
{
Expand Down
3 changes: 2 additions & 1 deletion src/Build/Instance/TaskFactories/TaskHostTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ public bool Execute()

TaskHostConfiguration hostConfiguration =
new TaskHostConfiguration(
runtime,
_buildComponentHost.BuildParameters.NodeId,
NativeMethodsShared.GetCurrentDirectory(),
CommunicationsUtilities.GetEnvironmentVariables(),
Expand Down Expand Up @@ -569,7 +570,7 @@ private void HandleLoggedMessage(LogMessagePacket logMessagePacket)
/// </summary>
private void LogErrorUnableToCreateTaskHost(HandshakeOptions requiredContext, string runtime, string architecture, NodeFailedToLaunchException e)
{
string msbuildLocation = NodeProviderOutOfProcTaskHost.GetMSBuildLocationFromHostContext(requiredContext) ??
string msbuildLocation = NodeProviderOutOfProcTaskHost.GetMSBuildLocationFromHostContext(requiredContext).msbuildExcutable ??
// We don't know the path -- probably we're trying to get a 64-bit assembly on a
// 32-bit machine. At least give them the exe name to look for, though ...
((requiredContext & HandshakeOptions.CLR2) == HandshakeOptions.CLR2 ? "MSBuildTaskHost.exe" : "MSBuild.exe");
Expand Down
3 changes: 2 additions & 1 deletion src/MSBuild/MSBuild/Microsoft.Build.Core.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@
<xs:attribute name="Runtime" type="msb:runtime" use="optional">
<xs:annotation>
<xs:documentation>
<!-- _locID_text="UsingTaskType_Runtime" _locComment="" -->Defines the .NET runtime version of the task host that this task should be run in. Currently supported values: CLR2, CLR4, CurrentRuntime, and * (any). If Runtime is not specified, either the task will be run within the MSBuild process, or the task host will be launched using the runtime of the parent MSBuild process
<!-- _locID_text="UsingTaskType_Runtime" _locComment="" -->Defines the .NET runtime version of the task host that this task should be run in. Currently supported values: CLR2, CLR4, NET, CurrentRuntime, and * (any). If Runtime is not specified, either the task will be run within the MSBuild process, or the task host will be launched using the runtime of the parent MSBuild process
</xs:documentation>
</xs:annotation>
</xs:attribute>
Expand Down Expand Up @@ -618,6 +618,7 @@
<xs:enumeration value="CurrentRuntime" />
<xs:enumeration value="CLR2" />
<xs:enumeration value="CLR4" />
<xs:enumeration value="NET" />
</xs:restriction>
</xs:simpleType>
</xs:union>
Expand Down
12 changes: 12 additions & 0 deletions src/Shared/BuildEnvironmentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,8 @@ NativeMethodsShared.ProcessorArchitectures.X64 or NativeMethodsShared.ProcessorA

MSBuildToolsDirectory32 = MSBuildToolsDirectoryRoot;
MSBuildToolsDirectory64 = existsCheck(potentialAmd64FromX86) ? Path.Combine(MSBuildToolsDirectoryRoot, "amd64") : CurrentMSBuildToolsDirectory;
MSBuildToolsDirectoryNET = @"C:\msbuild\msbuild_yk\msbuild\artifacts\bin\bootstrap\core";
MSBuildAssemblyDirectory = @"C:\msbuild\msbuild_yk\msbuild\artifacts\bin\bootstrap\core\sdk\9.0.200-preview.0.24603.3";
#if RUNTIME_TYPE_NETCORE
// Fall back to "current" for any architecture since .NET SDK doesn't
// support cross-arch task invocations.
Expand Down Expand Up @@ -662,6 +664,16 @@ NativeMethodsShared.ProcessorArchitectures.X64 or NativeMethodsShared.ProcessorA
/// </summary>
internal string MSBuildToolsDirectoryArm64 { get; }

/// <summary>
/// Path to the NET tools directory.
/// </summary>
internal string MSBuildToolsDirectoryNET { get; }

/// <summary>
/// Path to the MSBuild assembly.
/// </summary>
internal string MSBuildAssemblyDirectory { get; }

/// <summary>
/// Path to the Sdks folder for this MSBuild instance.
/// </summary>
Expand Down
Loading
Loading