From e627ad654e274f7016e98a2954d0c2b279b45a9f Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Fri, 7 Dec 2018 15:18:51 -0800 Subject: [PATCH 1/5] Avoid System.Runtime.InteropServices.RuntimeInformation.OSArchitecture in win7 on .NET Framework --- .../EditorServicesHost.cs | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/PowerShellEditorServices.Host/EditorServicesHost.cs b/src/PowerShellEditorServices.Host/EditorServicesHost.cs index 503e03068..105d1b249 100644 --- a/src/PowerShellEditorServices.Host/EditorServicesHost.cs +++ b/src/PowerShellEditorServices.Host/EditorServicesHost.cs @@ -154,6 +154,8 @@ public void StartLogging(string logFilePath, LogLevel logLevel) string osVersion = RuntimeInformation.OSDescription; + string osArch = GetOSArchitecture(); + string buildTime = BuildInfo.BuildTime?.ToString("s", System.Globalization.CultureInfo.InvariantCulture) ?? ""; string logHeader = $@" @@ -164,12 +166,12 @@ public void StartLogging(string logFilePath, LogLevel logLevel) Name: {this.hostDetails.Name} Version: {this.hostDetails.Version} ProfileId: {this.hostDetails.ProfileId} - Arch: {RuntimeInformation.OSArchitecture} + Arch: {osArch} Operating system details: Version: {osVersion} - Arch: {RuntimeInformation.OSArchitecture} + Arch: {osArch} Build information: @@ -245,7 +247,7 @@ await this.editorSession.PowerShellContext.ImportCommandsModule( // gets initialized when that is done earlier than LanguageServer.Initialize foreach (string module in this.additionalModules) { - var command = + var command = new System.Management.Automation.PSCommand() .AddCommand("Microsoft.PowerShell.Core\\Import-Module") .AddParameter("Name", module); @@ -493,6 +495,29 @@ private IServerListener CreateServiceListener(MessageProtocolType protocol, Edit } } + /// + /// Gets the OSArchitecture for logging. Cannot use System.Runtime.InteropServices.RuntimeInformation.OSArchitecture + /// directly, since this tries to load API set DLLs in win7 and crashes. + /// + /// + private string GetOSArchitecture() + { +#if !CoreCLR + // If on win7 (version 6.1.x), avoid System.Runtime.InteropServices.RuntimeInformation + if (Environment.OSVersion.Platform.Equals("Win32NT") && Environment.OSVersion.Version < new Version(6, 2)) + { + if (Environment.Is64BitProcess) + { + return "X64"; + } + + return "X86"; + } +#endif + + return RuntimeInformation.OSArchitecture.ToString(); + } + #endregion } } From 97a7c3a09dbe87401446dd296fcb98258f102af9 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Fri, 7 Dec 2018 15:24:42 -0800 Subject: [PATCH 2/5] Fix whitespace --- src/PowerShellEditorServices.Host/EditorServicesHost.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/PowerShellEditorServices.Host/EditorServicesHost.cs b/src/PowerShellEditorServices.Host/EditorServicesHost.cs index 105d1b249..a3a9bf22f 100644 --- a/src/PowerShellEditorServices.Host/EditorServicesHost.cs +++ b/src/PowerShellEditorServices.Host/EditorServicesHost.cs @@ -514,7 +514,6 @@ private string GetOSArchitecture() return "X86"; } #endif - return RuntimeInformation.OSArchitecture.ToString(); } From d800c91ca3a320e87616c2b8321dd39dc8027e95 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Fri, 7 Dec 2018 15:32:48 -0800 Subject: [PATCH 3/5] Add net451 startup error --- .../Start-EditorServices.ps1 | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/module/PowerShellEditorServices/Start-EditorServices.ps1 b/module/PowerShellEditorServices/Start-EditorServices.ps1 index 499bf816f..6728a80ba 100644 --- a/module/PowerShellEditorServices/Start-EditorServices.ps1 +++ b/module/PowerShellEditorServices/Start-EditorServices.ps1 @@ -161,6 +161,20 @@ if ($host.Runspace.LanguageMode -eq 'ConstrainedLanguage') { ExitWithError "PowerShell is configured with an unsupported LanguageMode (ConstrainedLanguage), language features are disabled." } +if ($PSVersionTable.PSVersion.Major -le 5) { + $net451Version = 378675 + $dotnetVersion = Get-ItemPropertyValue "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\" "Release" + if ($dotnetVersion -lt $net451Version) { + Write-SessionFile @{ + status = failed + reason = "netversion" + detail = "$netVersion" + } + + ExitWithError "Your .NET version is too low. Upgrade to net451 or higher to run the PowerShell extension." + } +} + # If PSReadline is present in the session, remove it so that runspace # management is easier if ((Microsoft.PowerShell.Core\Get-Module PSReadline).Count -gt 0) { From 98f46d560a992ae2e0d263f90a3fd7126d4ef4ed Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Fri, 7 Dec 2018 15:43:12 -0800 Subject: [PATCH 4/5] Get registry property in a PSv3 compatible way --- module/PowerShellEditorServices/Start-EditorServices.ps1 | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/module/PowerShellEditorServices/Start-EditorServices.ps1 b/module/PowerShellEditorServices/Start-EditorServices.ps1 index 6728a80ba..08f9e1bc4 100644 --- a/module/PowerShellEditorServices/Start-EditorServices.ps1 +++ b/module/PowerShellEditorServices/Start-EditorServices.ps1 @@ -161,9 +161,10 @@ if ($host.Runspace.LanguageMode -eq 'ConstrainedLanguage') { ExitWithError "PowerShell is configured with an unsupported LanguageMode (ConstrainedLanguage), language features are disabled." } +# net45 is not supported, only net451 and up if ($PSVersionTable.PSVersion.Major -le 5) { $net451Version = 378675 - $dotnetVersion = Get-ItemPropertyValue "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\" "Release" + $dotnetVersion = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\").Release if ($dotnetVersion -lt $net451Version) { Write-SessionFile @{ status = failed From 0464fb8847a3cc7377c0521795fda1c5a0fec84b Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Fri, 7 Dec 2018 15:58:02 -0800 Subject: [PATCH 5/5] Fix platform comparison --- src/PowerShellEditorServices.Host/EditorServicesHost.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PowerShellEditorServices.Host/EditorServicesHost.cs b/src/PowerShellEditorServices.Host/EditorServicesHost.cs index a3a9bf22f..516b0bba9 100644 --- a/src/PowerShellEditorServices.Host/EditorServicesHost.cs +++ b/src/PowerShellEditorServices.Host/EditorServicesHost.cs @@ -504,7 +504,7 @@ private string GetOSArchitecture() { #if !CoreCLR // If on win7 (version 6.1.x), avoid System.Runtime.InteropServices.RuntimeInformation - if (Environment.OSVersion.Platform.Equals("Win32NT") && Environment.OSVersion.Version < new Version(6, 2)) + if (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version < new Version(6, 2)) { if (Environment.Is64BitProcess) {