Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Win7 OSArchitecture crash workaround #808

Merged
merged 5 commits into from
Dec 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions module/PowerShellEditorServices/Start-EditorServices.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,21 @@ 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-ItemProperty "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) {
Expand Down
30 changes: 27 additions & 3 deletions src/PowerShellEditorServices.Host/EditorServicesHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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) ?? "<unspecified>";

string logHeader = $@"
Expand All @@ -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:

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -493,6 +495,28 @@ private IServerListener CreateServiceListener(MessageProtocolType protocol, Edit
}
}

/// <summary>
/// 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.
///
/// <returns></returns>
private string GetOSArchitecture()
{
#if !CoreCLR
// If on win7 (version 6.1.x), avoid System.Runtime.InteropServices.RuntimeInformation
if (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version < new Version(6, 2))
{
if (Environment.Is64BitProcess)
{
return "X64";
}

return "X86";
}
#endif
return RuntimeInformation.OSArchitecture.ToString();
}

#endregion
}
}