Skip to content

Commit f3d32cd

Browse files
NamedPipeConnectionInfo <= Enter-PSHostProcess (#881)
* NamedPipeConnectionInfo <= Enter-PSHostProcess * var * address comments
1 parent 13d8f8a commit f3d32cd

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

+34-27
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
using System.Diagnostics;
1717
using System.IO;
1818
using System.Linq;
19-
using System.Management.Automation;
2019
using System.Management.Automation.Language;
2120
using System.Management.Automation.Runspaces;
2221
using System.Text;
@@ -28,6 +27,8 @@
2827

2928
namespace Microsoft.PowerShell.EditorServices.Protocol.Server
3029
{
30+
using System.Management.Automation;
31+
3132
public class LanguageServer
3233
{
3334
private static CancellationTokenSource s_existingRequestCancellation;
@@ -1225,44 +1226,50 @@ protected async Task HandleGetRunspaceRequestAsync(
12251226
string processId,
12261227
RequestContext<GetRunspaceResponse[]> requestContext)
12271228
{
1228-
var runspaceResponses = new List<GetRunspaceResponse>();
1229+
IEnumerable<PSObject> runspaces = null;
12291230

12301231
if (this.editorSession.PowerShellContext.LocalPowerShellVersion.Version.Major >= 5)
12311232
{
12321233
if (processId == null) {
12331234
processId = "current";
12341235
}
12351236

1236-
var isNotCurrentProcess = processId != null && processId != "current";
1237-
1238-
var psCommand = new PSCommand();
1239-
1240-
if (isNotCurrentProcess) {
1241-
psCommand.AddCommand("Enter-PSHostProcess").AddParameter("Id", processId).AddStatement();
1242-
}
1243-
1244-
psCommand.AddCommand("Get-Runspace");
1245-
1246-
StringBuilder sb = new StringBuilder();
1247-
IEnumerable<Runspace> runspaces = await editorSession.PowerShellContext.ExecuteCommandAsync<Runspace>(psCommand, sb);
1248-
if (runspaces != null)
1237+
// If the processId is a valid int, we need to run Get-Runspace within that process
1238+
// otherwise just use the current runspace.
1239+
if (int.TryParse(processId, out int pid))
12491240
{
1250-
foreach (var p in runspaces)
1241+
// Create a remote runspace that we will invoke Get-Runspace in.
1242+
using(var rs = RunspaceFactory.CreateRunspace(new NamedPipeConnectionInfo(pid)))
1243+
using(var ps = PowerShell.Create())
12511244
{
1252-
runspaceResponses.Add(
1253-
new GetRunspaceResponse
1254-
{
1255-
Id = p.Id,
1256-
Name = p.Name,
1257-
Availability = p.RunspaceAvailability.ToString()
1258-
});
1245+
rs.Open();
1246+
ps.Runspace = rs;
1247+
// Returns deserialized Runspaces. For simpler code, we use PSObject and rely on dynamic later.
1248+
runspaces = ps.AddCommand("Microsoft.PowerShell.Utility\\Get-Runspace").Invoke<PSObject>();
12591249
}
12601250
}
1251+
else
1252+
{
1253+
var psCommand = new PSCommand().AddCommand("Microsoft.PowerShell.Utility\\Get-Runspace");
1254+
var sb = new StringBuilder();
1255+
// returns (not deserialized) Runspaces. For simpler code, we use PSObject and rely on dynamic later.
1256+
runspaces = await editorSession.PowerShellContext.ExecuteCommandAsync<PSObject>(psCommand, sb);
1257+
}
1258+
}
1259+
1260+
var runspaceResponses = new List<GetRunspaceResponse>();
12611261

1262-
if (isNotCurrentProcess) {
1263-
var exitCommand = new PSCommand();
1264-
exitCommand.AddCommand("Exit-PSHostProcess");
1265-
await editorSession.PowerShellContext.ExecuteCommandAsync(exitCommand);
1262+
if (runspaces != null)
1263+
{
1264+
foreach (dynamic runspace in runspaces)
1265+
{
1266+
runspaceResponses.Add(
1267+
new GetRunspaceResponse
1268+
{
1269+
Id = runspace.Id,
1270+
Name = runspace.Name,
1271+
Availability = runspace.RunspaceAvailability.ToString()
1272+
});
12661273
}
12671274
}
12681275

0 commit comments

Comments
 (0)