From bbcce1dff18a5c7cb8751ad240e921774b50ebeb Mon Sep 17 00:00:00 2001 From: Forgind Date: Thu, 30 Sep 2021 10:20:30 -0700 Subject: [PATCH] MSBuildLocator: Find dotnet.exe when out-of-proc (#6890) This defaults to looking for a nearby dotnet.exe (if you're on core) and uses that instead, falling back to .exe only on failure. This should handle any case in which you find MSBuild in an sdk installation. --- .../NodeProviderOutOfProcBase.cs | 37 ++++++++++--------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/src/Build/BackEnd/Components/Communications/NodeProviderOutOfProcBase.cs b/src/Build/BackEnd/Components/Communications/NodeProviderOutOfProcBase.cs index b45dab48cbd..ef21df23454 100644 --- a/src/Build/BackEnd/Components/Communications/NodeProviderOutOfProcBase.cs +++ b/src/Build/BackEnd/Components/Communications/NodeProviderOutOfProcBase.cs @@ -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; @@ -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(); // Null out the process handles so that the parent process does not wait for the child process @@ -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(); - threadSecurityAttributes.nLength = Marshal.SizeOf(); - CommunicationsUtilities.Trace("Launching node from {0}", msbuildLocation); string exeName = msbuildLocation; @@ -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 @@ -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(); + threadSecurityAttributes.nLength = Marshal.SizeOf(); bool result = BackendNativeMethods.CreateProcess ( @@ -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; + } } }