Skip to content

Commit b5dc308

Browse files
authored
Win7 OSArchitecture crash workaround (#808)
* Avoid System.Runtime.InteropServices.RuntimeInformation.OSArchitecture in win7 on .NET Framework * Warn users on lower than .NET 451 that they are not supported
1 parent bb65b2a commit b5dc308

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

module/PowerShellEditorServices/Start-EditorServices.ps1

+15
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,21 @@ if ($host.Runspace.LanguageMode -eq 'ConstrainedLanguage') {
161161
ExitWithError "PowerShell is configured with an unsupported LanguageMode (ConstrainedLanguage), language features are disabled."
162162
}
163163

164+
# net45 is not supported, only net451 and up
165+
if ($PSVersionTable.PSVersion.Major -le 5) {
166+
$net451Version = 378675
167+
$dotnetVersion = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\").Release
168+
if ($dotnetVersion -lt $net451Version) {
169+
Write-SessionFile @{
170+
status = failed
171+
reason = "netversion"
172+
detail = "$netVersion"
173+
}
174+
175+
ExitWithError "Your .NET version is too low. Upgrade to net451 or higher to run the PowerShell extension."
176+
}
177+
}
178+
164179
# If PSReadline is present in the session, remove it so that runspace
165180
# management is easier
166181
if ((Microsoft.PowerShell.Core\Get-Module PSReadline).Count -gt 0) {

src/PowerShellEditorServices.Host/EditorServicesHost.cs

+27-3
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ public void StartLogging(string logFilePath, LogLevel logLevel)
154154

155155
string osVersion = RuntimeInformation.OSDescription;
156156

157+
string osArch = GetOSArchitecture();
158+
157159
string buildTime = BuildInfo.BuildTime?.ToString("s", System.Globalization.CultureInfo.InvariantCulture) ?? "<unspecified>";
158160

159161
string logHeader = $@"
@@ -164,12 +166,12 @@ public void StartLogging(string logFilePath, LogLevel logLevel)
164166
Name: {this.hostDetails.Name}
165167
Version: {this.hostDetails.Version}
166168
ProfileId: {this.hostDetails.ProfileId}
167-
Arch: {RuntimeInformation.OSArchitecture}
169+
Arch: {osArch}
168170
169171
Operating system details:
170172
171173
Version: {osVersion}
172-
Arch: {RuntimeInformation.OSArchitecture}
174+
Arch: {osArch}
173175
174176
Build information:
175177
@@ -245,7 +247,7 @@ await this.editorSession.PowerShellContext.ImportCommandsModule(
245247
// gets initialized when that is done earlier than LanguageServer.Initialize
246248
foreach (string module in this.additionalModules)
247249
{
248-
var command =
250+
var command =
249251
new System.Management.Automation.PSCommand()
250252
.AddCommand("Microsoft.PowerShell.Core\\Import-Module")
251253
.AddParameter("Name", module);
@@ -493,6 +495,28 @@ private IServerListener CreateServiceListener(MessageProtocolType protocol, Edit
493495
}
494496
}
495497

498+
/// <summary>
499+
/// Gets the OSArchitecture for logging. Cannot use System.Runtime.InteropServices.RuntimeInformation.OSArchitecture
500+
/// directly, since this tries to load API set DLLs in win7 and crashes.
501+
///
502+
/// <returns></returns>
503+
private string GetOSArchitecture()
504+
{
505+
#if !CoreCLR
506+
// If on win7 (version 6.1.x), avoid System.Runtime.InteropServices.RuntimeInformation
507+
if (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version < new Version(6, 2))
508+
{
509+
if (Environment.Is64BitProcess)
510+
{
511+
return "X64";
512+
}
513+
514+
return "X86";
515+
}
516+
#endif
517+
return RuntimeInformation.OSArchitecture.ToString();
518+
}
519+
496520
#endregion
497521
}
498522
}

0 commit comments

Comments
 (0)