-
Notifications
You must be signed in to change notification settings - Fork 234
NamedPipeConnectionInfo <= Enter-PSHostProcess #881
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -16,7 +16,6 @@ | |||||
using System.Diagnostics; | ||||||
using System.IO; | ||||||
using System.Linq; | ||||||
using System.Management.Automation; | ||||||
using System.Management.Automation.Language; | ||||||
using System.Management.Automation.Runspaces; | ||||||
using System.Text; | ||||||
|
@@ -28,6 +27,8 @@ | |||||
|
||||||
namespace Microsoft.PowerShell.EditorServices.Protocol.Server | ||||||
{ | ||||||
using System.Management.Automation; | ||||||
|
||||||
public class LanguageServer | ||||||
{ | ||||||
private static CancellationTokenSource s_existingRequestCancellation; | ||||||
|
@@ -1225,44 +1226,50 @@ protected async Task HandleGetRunspaceRequestAsync( | |||||
string processId, | ||||||
RequestContext<GetRunspaceResponse[]> requestContext) | ||||||
{ | ||||||
var runspaceResponses = new List<GetRunspaceResponse>(); | ||||||
IEnumerable<PSObject> runspaces = null; | ||||||
|
||||||
if (this.editorSession.PowerShellContext.LocalPowerShellVersion.Version.Major >= 5) | ||||||
{ | ||||||
if (processId == null) { | ||||||
processId = "current"; | ||||||
} | ||||||
|
||||||
var isNotCurrentProcess = processId != null && processId != "current"; | ||||||
|
||||||
var psCommand = new PSCommand(); | ||||||
|
||||||
if (isNotCurrentProcess) { | ||||||
psCommand.AddCommand("Enter-PSHostProcess").AddParameter("Id", processId).AddStatement(); | ||||||
} | ||||||
|
||||||
psCommand.AddCommand("Get-Runspace"); | ||||||
|
||||||
StringBuilder sb = new StringBuilder(); | ||||||
IEnumerable<Runspace> runspaces = await editorSession.PowerShellContext.ExecuteCommandAsync<Runspace>(psCommand, sb); | ||||||
if (runspaces != null) | ||||||
// If the processId is a valid int, we need to run Get-Runspace within that process | ||||||
// otherwise just use the current runspace | ||||||
if (int.TryParse(processId, out int pid)) | ||||||
{ | ||||||
foreach (var p in runspaces) | ||||||
// Create a remote runspace that we will invoke Get-Runspace in | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
using(var rs = RunspaceFactory.CreateRunspace(new NamedPipeConnectionInfo(pid))) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will this work with all supported versions of PowerShell? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch... this use to be a runtime check using This will work in PowerShell 5.1+ so it's not a problem in this PR. But when I backport I need to ifdef this properly... |
||||||
using(var ps = PowerShell.Create()) | ||||||
{ | ||||||
runspaceResponses.Add( | ||||||
new GetRunspaceResponse | ||||||
{ | ||||||
Id = p.Id, | ||||||
Name = p.Name, | ||||||
Availability = p.RunspaceAvailability.ToString() | ||||||
}); | ||||||
rs.Open(); | ||||||
ps.Runspace = rs; | ||||||
// returns deserialized Runspaces. For simpler code, we use PSObject and rely on dynamic later. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
runspaces = ps.AddCommand("Get-Runspace").Invoke<PSObject>(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} | ||||||
else | ||||||
{ | ||||||
var psCommand = new PSCommand().AddCommand("Get-Runspace"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
StringBuilder sb = new StringBuilder(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. got me! |
||||||
// returns (not deserialized) Runspaces. For simpler code, we use PSObject and rely on dynamic later. | ||||||
runspaces = await editorSession.PowerShellContext.ExecuteCommandAsync<PSObject>(psCommand, sb); | ||||||
} | ||||||
} | ||||||
|
||||||
var runspaceResponses = new List<GetRunspaceResponse>(); | ||||||
|
||||||
if (isNotCurrentProcess) { | ||||||
var exitCommand = new PSCommand(); | ||||||
exitCommand.AddCommand("Exit-PSHostProcess"); | ||||||
await editorSession.PowerShellContext.ExecuteCommandAsync(exitCommand); | ||||||
if (runspaces != null) | ||||||
{ | ||||||
foreach (dynamic p in runspaces) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using a more descriptive variable name like Edit: didn't realize this was existing code, but maybe still worth fixing. |
||||||
{ | ||||||
runspaceResponses.Add( | ||||||
new GetRunspaceResponse | ||||||
{ | ||||||
Id = p.Id, | ||||||
Name = p.Name, | ||||||
Availability = p.RunspaceAvailability.ToString() | ||||||
}); | ||||||
} | ||||||
} | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.