From 1561f8ba0afce9fccc404453b855680e3c95f371 Mon Sep 17 00:00:00 2001 From: Nathan Mytelka Date: Fri, 24 Sep 2021 16:35:03 -0700 Subject: [PATCH 1/3] Find dotnet.exe instead of .exe Fixes #6782 --- .../Communications/NodeProviderOutOfProcBase.cs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Build/BackEnd/Components/Communications/NodeProviderOutOfProcBase.cs b/src/Build/BackEnd/Components/Communications/NodeProviderOutOfProcBase.cs index b45dab48cbd..ecbe731bcb3 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,8 +465,8 @@ 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(); + BackendNativeMethods.SECURITY_ATTRIBUTES processSecurityAttributes = new(); + BackendNativeMethods.SECURITY_ATTRIBUTES threadSecurityAttributes = new(); processSecurityAttributes.nLength = Marshal.SizeOf(); threadSecurityAttributes.nLength = Marshal.SizeOf(); @@ -480,8 +479,8 @@ private Process LaunchNode(string msbuildLocation, string commandLineArgs) if (!NativeMethodsShared.IsMono) { // Run the child process with the same host as the currently-running process. - exeName = GetCurrentHost(); - commandLineArgs = "\"" + msbuildLocation + "\" " + commandLineArgs; + string dotnetExe = Path.Combine(FileUtilities.GetFolderAbove(exeName, 2), "dotnet.exe"); + exeName = File.Exists(dotnetExe) ? dotnetExe : GetCurrentHost(); } #endif From b231c555ccf9ca72eab05a265a3404b1bba86f19 Mon Sep 17 00:00:00 2001 From: Nathan Mytelka Date: Mon, 27 Sep 2021 11:11:45 -0700 Subject: [PATCH 2/3] Move logic into GetCurrentHost --- .../NodeProviderOutOfProcBase.cs | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/Build/BackEnd/Components/Communications/NodeProviderOutOfProcBase.cs b/src/Build/BackEnd/Components/Communications/NodeProviderOutOfProcBase.cs index ecbe731bcb3..980b2f9632d 100644 --- a/src/Build/BackEnd/Components/Communications/NodeProviderOutOfProcBase.cs +++ b/src/Build/BackEnd/Components/Communications/NodeProviderOutOfProcBase.cs @@ -465,11 +465,6 @@ private Process LaunchNode(string msbuildLocation, string commandLineArgs) creationFlags |= BackendNativeMethods.CREATE_NEW_CONSOLE; } - BackendNativeMethods.SECURITY_ATTRIBUTES processSecurityAttributes = new(); - BackendNativeMethods.SECURITY_ATTRIBUTES threadSecurityAttributes = new(); - processSecurityAttributes.nLength = Marshal.SizeOf(); - threadSecurityAttributes.nLength = Marshal.SizeOf(); - CommunicationsUtilities.Trace("Launching node from {0}", msbuildLocation); string exeName = msbuildLocation; @@ -479,8 +474,7 @@ private Process LaunchNode(string msbuildLocation, string commandLineArgs) if (!NativeMethodsShared.IsMono) { // Run the child process with the same host as the currently-running process. - string dotnetExe = Path.Combine(FileUtilities.GetFolderAbove(exeName, 2), "dotnet.exe"); - exeName = File.Exists(dotnetExe) ? dotnetExe : GetCurrentHost(); + exeName = GetCurrentHost(); } #endif @@ -525,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 ( @@ -595,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; + } } } From 24b18d972974bf16f5a8c2048a8845214b1a72be Mon Sep 17 00:00:00 2001 From: Nathan Mytelka Date: Wed, 29 Sep 2021 09:53:49 -0700 Subject: [PATCH 3/3] Add missing space --- .../Components/Communications/NodeProviderOutOfProcBase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Build/BackEnd/Components/Communications/NodeProviderOutOfProcBase.cs b/src/Build/BackEnd/Components/Communications/NodeProviderOutOfProcBase.cs index 980b2f9632d..ef21df23454 100644 --- a/src/Build/BackEnd/Components/Communications/NodeProviderOutOfProcBase.cs +++ b/src/Build/BackEnd/Components/Communications/NodeProviderOutOfProcBase.cs @@ -520,7 +520,7 @@ private Process LaunchNode(string msbuildLocation, string commandLineArgs) { #if RUNTIME_TYPE_NETCORE // Repeat the executable name in the args to suit CreateProcess - commandLineArgs = $"\"{exeName}\"{commandLineArgs}"; + commandLineArgs = $"\"{exeName}\" {commandLineArgs}"; #endif BackendNativeMethods.PROCESS_INFORMATION processInfo = new();