Skip to content

Commit

Permalink
MSBuildLocator: Find dotnet.exe when out-of-proc (#6890)
Browse files Browse the repository at this point in the history
This defaults to looking for a nearby dotnet.exe (if you're on core) and uses that instead, falling back to <name>.exe only on failure. This should handle any case in which you find MSBuild in an sdk installation.
  • Loading branch information
Forgind authored Sep 30, 2021
1 parent 5805e34 commit bbcce1d
Showing 1 changed file with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

using BackendNativeMethods = Microsoft.Build.BackEnd.NativeMethods;
using Task = System.Threading.Tasks.Task;
using DotNetFrameworkArchitecture = Microsoft.Build.Shared.DotNetFrameworkArchitecture;
using Microsoft.Build.Framework;
using Microsoft.Build.BackEnd.Logging;

Expand Down Expand Up @@ -434,9 +433,9 @@ private Process LaunchNode(string msbuildLocation, string commandLineArgs)

// Repeat the executable name as the first token of the command line because the command line
// parser logic expects it and will otherwise skip the first argument
commandLineArgs = msbuildLocation + " " + commandLineArgs;
commandLineArgs = $"\"{msbuildLocation}\" {commandLineArgs}";

BackendNativeMethods.STARTUP_INFO startInfo = new BackendNativeMethods.STARTUP_INFO();
BackendNativeMethods.STARTUP_INFO startInfo = new();
startInfo.cb = Marshal.SizeOf<BackendNativeMethods.STARTUP_INFO>();

// Null out the process handles so that the parent process does not wait for the child process
Expand Down Expand Up @@ -466,11 +465,6 @@ private Process LaunchNode(string msbuildLocation, string commandLineArgs)
creationFlags |= BackendNativeMethods.CREATE_NEW_CONSOLE;
}

BackendNativeMethods.SECURITY_ATTRIBUTES processSecurityAttributes = new BackendNativeMethods.SECURITY_ATTRIBUTES();
BackendNativeMethods.SECURITY_ATTRIBUTES threadSecurityAttributes = new BackendNativeMethods.SECURITY_ATTRIBUTES();
processSecurityAttributes.nLength = Marshal.SizeOf<BackendNativeMethods.SECURITY_ATTRIBUTES>();
threadSecurityAttributes.nLength = Marshal.SizeOf<BackendNativeMethods.SECURITY_ATTRIBUTES>();

CommunicationsUtilities.Trace("Launching node from {0}", msbuildLocation);

string exeName = msbuildLocation;
Expand All @@ -481,7 +475,6 @@ private Process LaunchNode(string msbuildLocation, string commandLineArgs)
{
// Run the child process with the same host as the currently-running process.
exeName = GetCurrentHost();
commandLineArgs = "\"" + msbuildLocation + "\" " + commandLineArgs;
}
#endif

Expand Down Expand Up @@ -526,14 +519,15 @@ private Process LaunchNode(string msbuildLocation, string commandLineArgs)
else
{
#if RUNTIME_TYPE_NETCORE
if (NativeMethodsShared.IsWindows)
{
// Repeat the executable name in the args to suit CreateProcess
commandLineArgs = "\"" + exeName + "\" " + commandLineArgs;
}
// Repeat the executable name in the args to suit CreateProcess
commandLineArgs = $"\"{exeName}\" {commandLineArgs}";
#endif

BackendNativeMethods.PROCESS_INFORMATION processInfo = new BackendNativeMethods.PROCESS_INFORMATION();
BackendNativeMethods.PROCESS_INFORMATION processInfo = new();
BackendNativeMethods.SECURITY_ATTRIBUTES processSecurityAttributes = new();
BackendNativeMethods.SECURITY_ATTRIBUTES threadSecurityAttributes = new();
processSecurityAttributes.nLength = Marshal.SizeOf<BackendNativeMethods.SECURITY_ATTRIBUTES>();
threadSecurityAttributes.nLength = Marshal.SizeOf<BackendNativeMethods.SECURITY_ATTRIBUTES>();

bool result = BackendNativeMethods.CreateProcess
(
Expand Down Expand Up @@ -596,9 +590,18 @@ private static string GetCurrentHost()
#if RUNTIME_TYPE_NETCORE || MONO
if (CurrentHost == null)
{
using (Process currentProcess = Process.GetCurrentProcess())
string dotnetExe = Path.Combine(FileUtilities.GetFolderAbove(BuildEnvironmentHelper.Instance.CurrentMSBuildExePath, 2),
NativeMethodsShared.IsWindows ? "dotnet.exe" : "dotnet");
if (File.Exists(dotnetExe))
{
CurrentHost = dotnetExe;
}
else
{
CurrentHost = currentProcess.MainModule.FileName;
using (Process currentProcess = Process.GetCurrentProcess())
{
CurrentHost = currentProcess.MainModule.FileName;
}
}
}

Expand Down

0 comments on commit bbcce1d

Please sign in to comment.