Skip to content

Commit 1816a72

Browse files
committed
Created internal overload for CreateRunspace that accepts InitialSessionState.
Renamed initialRunspace to runspace Typecasted AdditionalModules instead of using .ToArray() to prevent cannot convert from 'System.Collections.Generic.IReadOnlyList<string>' to 'System.Collections.Generic.IEnumerable<Microsoft.PowerShell.Commands.ModuleSpecification>' compile time error. This is not ready to be merged as it needs to be rebased on master to fix test issues, and the primary objective of the changes has not been met as CommandCompletion.CompleteInput returns 0 CompletionResults when spawned from a Constrained Runspace. This may or may not require a fix from the PowerShell team as we are spawning CompleteInput from an overload of CompleteCommand that uses TabExpansion2 and there is a comment in their code indicating that this (or something related to it) is currently broken: https://github.com/PowerShell/PowerShell/blob/f4382202ae4622bf26795e29a7b39b9d7cdfb3fb/src/System.Management.Automation/engine/CommandCompletion/CommandCompletion.cs#L547
1 parent 90367c5 commit 1816a72

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs

+1
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ private EditorServicesConfig CreateConfigObject()
351351
var profile = (PSObject)GetVariableValue("profile");
352352

353353
var hostInfo = new HostInfo(HostName, HostProfileId, HostVersion);
354+
// This ensures that all Runspaces are created with the same InitialSessionState as the runspace that started EditorServices
354355
var initialSessionState = Runspace.DefaultRunspace.InitialSessionState;
355356
var editorServicesConfig = new EditorServicesConfig(hostInfo, Host, SessionDetailsPath, bundledModulesPath, LogPath)
356357
{

src/PowerShellEditorServices/PowerShellEditorServices.csproj

-4
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,4 @@
5555
<ItemGroup>
5656
<Compile Remove="Extensions\Api\DocumentSymbolService.cs" />
5757
</ItemGroup>
58-
59-
<ItemGroup>
60-
<Folder Include="Properties\" />
61-
</ItemGroup>
6258
</Project>

src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs

+23-16
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,11 @@ public static PowerShellContextService Create(
215215

216216
if (hostStartupInfo.InitialSessionState.LanguageMode != PSLanguageMode.FullLanguage)
217217
{
218-
hostStartupInfo.InitialSessionState.ImportPSModule(hostStartupInfo.AdditionalModules.ToArray());
218+
hostStartupInfo.InitialSessionState.ImportPSModule((IEnumerable<Commands.ModuleSpecification>)hostStartupInfo.AdditionalModules);
219219
hostStartupInfo.InitialSessionState.ImportPSModule(new string[] { s_commandsModulePath });
220220
}
221-
Runspace initialRunspace = PowerShellContextService.CreateRunspace(psHost, hostStartupInfo.InitialSessionState);
222-
powerShellContext.Initialize(hostStartupInfo.ProfilePaths, initialRunspace, true, hostUserInterface);
221+
Runspace runspace = PowerShellContextService.CreateRunspace(psHost, hostStartupInfo.InitialSessionState);
222+
powerShellContext.Initialize(hostStartupInfo.ProfilePaths, runspace, true, hostUserInterface);
223223
// TODO: This can be moved to the point after the $psEditor object
224224
// gets initialized when that is done earlier than LanguageServer.Initialize
225225
// Darren Kattan: I haven't tested it, but I have a feeling this entire bit of logic can be replaced with the bit above for non-FullLanguage mode. I think that was is cleaner anyway.
@@ -267,26 +267,15 @@ public static Runspace CreateRunspace(
267267
powerShellContext.ConsoleReader = hostUserInterface;
268268
return CreateRunspace(psHost, hostDetails.InitialSessionState);
269269
}
270-
270+
271271
/// <summary>
272272
///
273273
/// </summary>
274274
/// <param name="psHost">The PSHost that will be used for this Runspace.</param>
275275
/// <param name="initialSessionState">The initialSessionState inherited from the orginal PowerShell process. This will be used when creating runspaces so that we honor the same initialSessionState including allowed modules, cmdlets and language mode.</param>
276276
/// <returns></returns>
277-
public static Runspace CreateRunspace(PSHost psHost, InitialSessionState initialSessionState)
277+
internal static Runspace CreateRunspace(PSHost psHost, InitialSessionState initialSessionState)
278278
{
279-
if (initialSessionState == null)
280-
{
281-
if (Environment.GetEnvironmentVariable("PSES_TEST_USE_CREATE_DEFAULT") == "1")
282-
{
283-
initialSessionState = InitialSessionState.CreateDefault();
284-
}
285-
else
286-
{
287-
initialSessionState = InitialSessionState.CreateDefault2();
288-
}
289-
}
290279
Runspace runspace = RunspaceFactory.CreateRunspace(psHost, initialSessionState);
291280

292281
// Windows PowerShell must be hosted in STA mode
@@ -301,6 +290,24 @@ public static Runspace CreateRunspace(PSHost psHost, InitialSessionState initial
301290

302291
return runspace;
303292
}
293+
/// <summary>
294+
///
295+
/// </summary>
296+
/// <param name="psHost">The PSHost that will be used for this Runspace.</param>
297+
/// <returns>Runspace object created from the specified PSHost</returns>
298+
public static Runspace CreateRunspace(PSHost psHost)
299+
{
300+
InitialSessionState initialSessionState;
301+
if (Environment.GetEnvironmentVariable("PSES_TEST_USE_CREATE_DEFAULT") == "1")
302+
{
303+
initialSessionState = InitialSessionState.CreateDefault();
304+
}
305+
else
306+
{
307+
initialSessionState = InitialSessionState.CreateDefault2();
308+
}
309+
return CreateRunspace(psHost, initialSessionState);
310+
}
304311

305312
/// <summary>
306313
/// Initializes a new instance of the PowerShellContext class using

0 commit comments

Comments
 (0)