Skip to content

Commit 3bf1bac

Browse files
initial support of constrainedlanguage mode
1 parent d9fef9c commit 3bf1bac

File tree

7 files changed

+26
-18
lines changed

7 files changed

+26
-18
lines changed

src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
using System.Management.Automation;
1212
using System.Reflection;
1313
using SMA = System.Management.Automation;
14+
using System.Management.Automation.Runspaces;
1415
using Microsoft.PowerShell.EditorServices.Hosting;
1516
using System.Globalization;
1617
using System.Collections;
@@ -358,6 +359,7 @@ private EditorServicesConfig CreateConfigObject()
358359
AdditionalModules = AdditionalModules,
359360
LanguageServiceTransport = GetLanguageServiceTransport(),
360361
DebugServiceTransport = GetDebugServiceTransport(),
362+
LanguageMode = Runspace.DefaultRunspace.SessionStateProxy.LanguageMode,
361363
ProfilePaths = new ProfilePathConfig
362364
{
363365
AllUsersAllHosts = GetProfilePathFromProfileObject(profile, ProfileUserKind.AllUsers, ProfileHostKind.AllHosts),

src/PowerShellEditorServices.Hosting/Configuration/EditorServicesConfig.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55

66
using System.Collections.Generic;
7+
using System.Management.Automation;
78
using System.Management.Automation.Host;
89

910
namespace Microsoft.PowerShell.EditorServices.Hosting
@@ -111,6 +112,11 @@ public EditorServicesConfig(
111112
/// </summary>
112113
public ProfilePathConfig ProfilePaths { get; set; }
113114

115+
/// <summary>
116+
/// The language mode of the original runspace that we will inherit from.
117+
/// </summary>
118+
public PSLanguageMode LanguageMode { get; internal set; }
119+
114120
public string StartupBanner { get; set; } = @"
115121
116122
=====> PowerShell Integrated Console <=====

src/PowerShellEditorServices.Hosting/EditorServicesLoader.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,6 @@ public Task LoadAndRunEditorServicesAsync()
190190
// Make sure the .NET Framework version supports .NET Standard 2.0
191191
CheckNetFxVersion();
192192
#endif
193-
// Ensure the language mode allows us to run
194-
CheckLanguageMode();
195193

196194
// Add the bundled modules to the PSModulePath
197195
UpdatePSModulePath();
@@ -250,19 +248,6 @@ private void CheckNetFxVersion()
250248
}
251249
#endif
252250

253-
/// <summary>
254-
/// PSES currently does not work in Constrained Language Mode, because PSReadLine script invocations won't work in it.
255-
/// Ideally we can find a better way so that PSES will work in CLM.
256-
/// </summary>
257-
private void CheckLanguageMode()
258-
{
259-
_logger.Log(PsesLogLevel.Diagnostic, "Checking that PSES is running in FullLanguage mode");
260-
if (Runspace.DefaultRunspace.SessionStateProxy.LanguageMode != PSLanguageMode.FullLanguage)
261-
{
262-
throw new InvalidOperationException("Cannot start PowerShell Editor Services in Constrained Language Mode");
263-
}
264-
}
265-
266251
private void UpdatePSModulePath()
267252
{
268253
if (string.IsNullOrEmpty(_hostConfig.BundledModulePath))

src/PowerShellEditorServices.Hosting/Internal/EditorServicesRunner.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ private HostStartupInfo CreateHostStartupInfo()
238238
_config.FeatureFlags,
239239
_config.AdditionalModules,
240240
_config.LogPath,
241+
_config.LanguageMode,
241242
(int)_config.LogLevel,
242243
consoleReplEnabled: _config.ConsoleRepl != ConsoleReplKind.None,
243244
usesLegacyReadLine: _config.ConsoleRepl == ConsoleReplKind.LegacyReadLine);

src/PowerShellEditorServices/Hosting/HostStartupInfo.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
using System;
77
using System.Collections.Generic;
8+
using System.Management.Automation;
89
using System.Management.Automation.Host;
910

1011
namespace Microsoft.PowerShell.EditorServices.Hosting
@@ -89,6 +90,11 @@ public sealed class HostStartupInfo
8990
/// </summary>
9091
public string LogPath { get; }
9192

93+
/// <summary>
94+
/// The language mode of the original runspace that we will inherit from.
95+
/// </summary>
96+
public PSLanguageMode LanguageMode { get; }
97+
9298
/// <summary>
9399
/// The minimum log level of log events to be logged.
94100
/// </summary>
@@ -130,6 +136,7 @@ public HostStartupInfo(
130136
IReadOnlyList<string> featureFlags,
131137
IReadOnlyList<string> additionalModules,
132138
string logPath,
139+
PSLanguageMode languageMode,
133140
int logLevel,
134141
bool consoleReplEnabled,
135142
bool usesLegacyReadLine)
@@ -142,6 +149,7 @@ public HostStartupInfo(
142149
FeatureFlags = featureFlags ?? Array.Empty<string>();
143150
AdditionalModules = additionalModules ?? Array.Empty<string>();
144151
LogPath = logPath;
152+
LanguageMode = languageMode;
145153
LogLevel = logLevel;
146154
ConsoleReplEnabled = consoleReplEnabled;
147155
UsesLegacyReadLine = usesLegacyReadLine;

src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public static PowerShellContextService Create(
213213
hostUserInterface,
214214
logger);
215215

216-
Runspace initialRunspace = PowerShellContextService.CreateRunspace(psHost);
216+
Runspace initialRunspace = PowerShellContextService.CreateRunspace(psHost, hostStartupInfo.LanguageMode);
217217
powerShellContext.Initialize(hostStartupInfo.ProfilePaths, initialRunspace, true, hostUserInterface);
218218

219219
powerShellContext.ImportCommandsModuleAsync();
@@ -260,15 +260,15 @@ public static Runspace CreateRunspace(
260260
var psHost = new EditorServicesPSHost(powerShellContext, hostDetails, hostUserInterface, logger);
261261
powerShellContext.ConsoleWriter = hostUserInterface;
262262
powerShellContext.ConsoleReader = hostUserInterface;
263-
return CreateRunspace(psHost);
263+
return CreateRunspace(psHost, hostDetails.LanguageMode);
264264
}
265265

266266
/// <summary>
267267
///
268268
/// </summary>
269269
/// <param name="psHost"></param>
270270
/// <returns></returns>
271-
public static Runspace CreateRunspace(PSHost psHost)
271+
public static Runspace CreateRunspace(PSHost psHost, PSLanguageMode languageMode)
272272
{
273273
InitialSessionState initialSessionState;
274274
if (Environment.GetEnvironmentVariable("PSES_TEST_USE_CREATE_DEFAULT") == "1") {
@@ -277,6 +277,10 @@ public static Runspace CreateRunspace(PSHost psHost)
277277
initialSessionState = InitialSessionState.CreateDefault2();
278278
}
279279

280+
// Create and initialize a new Runspace while honoring the LanguageMode.
281+
Console.WriteLine(languageMode);
282+
initialSessionState.LanguageMode = languageMode;
283+
280284
Runspace runspace = RunspaceFactory.CreateRunspace(psHost, initialSessionState);
281285

282286
// Windows PowerShell must be hosted in STA mode

test/PowerShellEditorServices.Test/PowerShellContextFactory.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System;
1313
using System.Collections.Generic;
1414
using System.IO;
15+
using System.Management.Automation;
1516
using System.Threading;
1617
using System.Threading.Tasks;
1718

@@ -46,6 +47,7 @@ public static PowerShellContextService Create(ILogger logger)
4647
new List<string>(),
4748
new List<string>(),
4849
null,
50+
PSLanguageMode.FullLanguage,
4951
0,
5052
consoleReplEnabled: false,
5153
usesLegacyReadLine: false);

0 commit comments

Comments
 (0)