From 2f73af53df24fce9b704510dbec2a89cf75d877e Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 2 May 2016 13:54:26 -0700 Subject: [PATCH] Remove native host dependency for ConsoleHost When Open PowerShell is built (as .NET Core project), `OPEN` is defined. The `Start` function normally used as the entry point from PowerShell's custom native host is instead the real `Main` entry point of the program, so it can be launched from the `dotnet` host. Because Core PowerShell depends on the native host to setup the custom assembly load context, we do it here in managed code immediately on start-up. --- .../host/msh/ManagedEntrance.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs index 5f516948ce..c1b283e689 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ManagedEntrance.cs @@ -26,7 +26,9 @@ namespace Microsoft.PowerShell /// /// Command line arguments to the managed MSH /// -#if CORECLR +#if OPEN && CORECLR // Open PowerShell needs an actual Main entry-point + public static int Main(string[] args) +#elif CORECLR #pragma warning disable 1573 public static int Start(string consoleFilePath, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeParamIndex = 2)]string[] args, int argc) #pragma warning restore 1573 @@ -34,6 +36,9 @@ namespace Microsoft.PowerShell public int Start(string consoleFilePath, string[] args) #endif { +#if OPEN && CORECLR // Open PowerShell has to set the ALC here, since we don't own the native host + PowerShellAssemblyLoadContextInitializer.SetPowerShellAssemblyLoadContext(string.Empty); +#endif System.Management.Automation.Runspaces.EarlyStartup.Init(); // Set ETW activity Id @@ -69,7 +74,9 @@ namespace Microsoft.PowerShell // PSSnapInException will cause the control to return back to the native code // and stuff the EXCEPINFO field with the message of the exception. // The native code will print this out and exit the process. +#if !OPEN // consoleFilePath is not available in Open PowerShell if (string.IsNullOrEmpty(consoleFilePath)) +#endif { #if DEBUG // Special switches for debug mode to allow self-hosting on InitialSessionState instead @@ -96,12 +103,14 @@ namespace Microsoft.PowerShell configuration = null; #endif } +#if !OPEN // consoleFilePath is not available in Open PowerShell else { //TODO : Deprecate RunspaceConfiguration and use InitialSessionState configuration = RunspaceConfigForSingleShell.Create(consoleFilePath, out warning); } +#endif int exitCode = 0; try {