Skip to content

Make initial logging work in constrained language mode #1222

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

Merged
merged 7 commits into from
Mar 10, 2020
Merged
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
35 changes: 22 additions & 13 deletions src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using System.Runtime.InteropServices;

using SMA = System.Management.Automation;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed for the $outputencoding log

using System.Management.Automation;
using System.Management.Automation.Runspaces;

#if CoreCLR
using System.Runtime.Loader;
Expand Down Expand Up @@ -255,12 +257,9 @@ private void CheckNetFxVersion()
private void CheckLanguageMode()
{
_logger.Log(PsesLogLevel.Diagnostic, "Checking that PSES is running in FullLanguage mode");
using (var pwsh = SMA.PowerShell.Create())
if (Runspace.DefaultRunspace.SessionStateProxy.LanguageMode != PSLanguageMode.FullLanguage)
{
if (pwsh.Runspace.SessionStateProxy.LanguageMode != SMA.PSLanguageMode.FullLanguage)
{
throw new InvalidOperationException("Cannot start PowerShell Editor Services in Constrained Language Mode");
}
throw new InvalidOperationException("Cannot start PowerShell Editor Services in Constrained Language Mode");
}
}

Expand Down Expand Up @@ -339,23 +338,20 @@ private string GetPSOutputEncoding()

private void LogPowerShellDetails()
{
using (var pwsh = SMA.PowerShell.Create(SMA.RunspaceMode.CurrentRunspace))
{
string psVersion = pwsh.AddScript("$PSVersionTable.PSVersion").Invoke()[0].ToString();
PSLanguageMode languageMode = Runspace.DefaultRunspace.SessionStateProxy.LanguageMode;

_logger.Log(PsesLogLevel.Verbose, $@"
_logger.Log(PsesLogLevel.Verbose, $@"
== PowerShell Details ==
- PowerShell version: {psVersion}
- Language mode: {pwsh.Runspace.SessionStateProxy.LanguageMode}
- PowerShell version: {GetPSVersion()}
- Language mode: {languageMode}
");
}
}

private void LogOperatingSystemDetails()
{
_logger.Log(PsesLogLevel.Verbose, $@"
== Environment Details ==
- OS description: {RuntimeInformation.OSDescription}
- OS description: {RuntimeInformation.OSDescription}
- OS architecture: {GetOSArchitecture()}
- Process bitness: {(Environment.Is64BitProcess ? "64" : "32")}
");
Expand Down Expand Up @@ -406,5 +402,18 @@ private void ValidateConfiguration()
throw new ArgumentNullException(nameof(_hostConfig.PSHost));
}
}

private static object GetPSVersion()
{
// In order to read the $PSVersionTable variable,
// we are forced to create a new runspace to avoid concurrency issues,
// which is expensive.
// Rather than do that, we instead go straight to the source,
// which is a static property, internal in WinPS and public in PS 6+
return typeof(PSObject).Assembly
.GetType("System.Management.Automation.PSVersionInfo")
.GetMethod("get_PSVersion", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
.Invoke(null, Array.Empty<object>());
}
}
}