Skip to content

Commit ce265d7

Browse files
authored
Make initial logging work in constrained language mode (#1222)
1 parent f936fbc commit ce265d7

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
using System.Runtime.InteropServices;
1212

1313
using SMA = System.Management.Automation;
14+
using System.Management.Automation;
15+
using System.Management.Automation.Runspaces;
1416

1517
#if CoreCLR
1618
using System.Runtime.Loader;
@@ -255,12 +257,9 @@ private void CheckNetFxVersion()
255257
private void CheckLanguageMode()
256258
{
257259
_logger.Log(PsesLogLevel.Diagnostic, "Checking that PSES is running in FullLanguage mode");
258-
using (var pwsh = SMA.PowerShell.Create())
260+
if (Runspace.DefaultRunspace.SessionStateProxy.LanguageMode != PSLanguageMode.FullLanguage)
259261
{
260-
if (pwsh.Runspace.SessionStateProxy.LanguageMode != SMA.PSLanguageMode.FullLanguage)
261-
{
262-
throw new InvalidOperationException("Cannot start PowerShell Editor Services in Constrained Language Mode");
263-
}
262+
throw new InvalidOperationException("Cannot start PowerShell Editor Services in Constrained Language Mode");
264263
}
265264
}
266265

@@ -339,23 +338,20 @@ private string GetPSOutputEncoding()
339338

340339
private void LogPowerShellDetails()
341340
{
342-
using (var pwsh = SMA.PowerShell.Create(SMA.RunspaceMode.CurrentRunspace))
343-
{
344-
string psVersion = pwsh.AddScript("$PSVersionTable.PSVersion").Invoke()[0].ToString();
341+
PSLanguageMode languageMode = Runspace.DefaultRunspace.SessionStateProxy.LanguageMode;
345342

346-
_logger.Log(PsesLogLevel.Verbose, $@"
343+
_logger.Log(PsesLogLevel.Verbose, $@"
347344
== PowerShell Details ==
348-
- PowerShell version: {psVersion}
349-
- Language mode: {pwsh.Runspace.SessionStateProxy.LanguageMode}
345+
- PowerShell version: {GetPSVersion()}
346+
- Language mode: {languageMode}
350347
");
351-
}
352348
}
353349

354350
private void LogOperatingSystemDetails()
355351
{
356352
_logger.Log(PsesLogLevel.Verbose, $@"
357353
== Environment Details ==
358-
- OS description: {RuntimeInformation.OSDescription}
354+
- OS description: {RuntimeInformation.OSDescription}
359355
- OS architecture: {GetOSArchitecture()}
360356
- Process bitness: {(Environment.Is64BitProcess ? "64" : "32")}
361357
");
@@ -406,5 +402,18 @@ private void ValidateConfiguration()
406402
throw new ArgumentNullException(nameof(_hostConfig.PSHost));
407403
}
408404
}
405+
406+
private static object GetPSVersion()
407+
{
408+
// In order to read the $PSVersionTable variable,
409+
// we are forced to create a new runspace to avoid concurrency issues,
410+
// which is expensive.
411+
// Rather than do that, we instead go straight to the source,
412+
// which is a static property, internal in WinPS and public in PS 6+
413+
return typeof(PSObject).Assembly
414+
.GetType("System.Management.Automation.PSVersionInfo")
415+
.GetMethod("get_PSVersion", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)
416+
.Invoke(null, Array.Empty<object>());
417+
}
409418
}
410419
}

0 commit comments

Comments
 (0)