Skip to content

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

Conversation

TylerLeonhardt
Copy link
Member

cc @adamdriscoll

Before, we relied on Enter-PSHostProcess... I noticed that this wasn't working and instead the GeRunspace function would return results from the current runspace instead.

This switches to using NamedPipeConnectionInfo API way of invoking something on a remote runspace.

@TylerLeonhardt
Copy link
Member Author

@SeeminglyScience let me know if you see anything wrong with this in the scope of the threading model... I think it will be fine since it's a totally separate runspace...

else
{
var psCommand = new PSCommand().AddCommand("Get-Runspace");
StringBuilder sb = new StringBuilder();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use var here - channeling my inner @rjmholt. :-)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got me!

{
foreach (var p in runspaces)
// Create a remote runspace that we will invoke Get-Runspace in
using(var rs = RunspaceFactory.CreateRunspace(new NamedPipeConnectionInfo(pid)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this work with all supported versions of PowerShell?

Copy link
Member Author

@TylerLeonhardt TylerLeonhardt Mar 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch... this use to be a runtime check using Enter-PSHostProcess but now it's an assembly load check...

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...

This was referenced Mar 18, 2019
Copy link
Collaborator

@SeeminglyScience SeeminglyScience left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :)

Just some optional nits

rs.Open();
ps.Runspace = rs;
// returns deserialized Runspaces. For simpler code, we use PSObject and rely on dynamic later.
runspaces = ps.AddCommand("Get-Runspace").Invoke<PSObject>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
runspaces = ps.AddCommand("Get-Runspace").Invoke<PSObject>();
runspaces = ps.AddCommand("Microsoft.PowerShell.Utility\\Get-Runspace").Invoke<PSObject>();

}
}
else
{
var psCommand = new PSCommand().AddCommand("Get-Runspace");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var psCommand = new PSCommand().AddCommand("Get-Runspace");
var psCommand = new PSCommand().AddCommand("Microsoft.PowerShell.Utility\\Get-Runspace");

{
foreach (var p in runspaces)
// Create a remote runspace that we will invoke Get-Runspace in
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Create a remote runspace that we will invoke Get-Runspace in
// Create a remote runspace that we will invoke Get-Runspace in.

});
rs.Open();
ps.Runspace = rs;
// returns deserialized Runspaces. For simpler code, we use PSObject and rely on dynamic later.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// returns deserialized Runspaces. For simpler code, we use PSObject and rely on dynamic later.
// Returns deserialized Runspaces. For simpler code, we use PSObject and rely on dynamic later.

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// otherwise just use the current runspace
// otherwise just use the current runspace.

await editorSession.PowerShellContext.ExecuteCommandAsync(exitCommand);
if (runspaces != null)
{
foreach (dynamic p in runspaces)
Copy link
Collaborator

@SeeminglyScience SeeminglyScience Mar 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using a more descriptive variable name like runspace or even pso.

Edit: didn't realize this was existing code, but maybe still worth fixing.

@TylerLeonhardt TylerLeonhardt merged commit f3d32cd into PowerShell:master Mar 20, 2019
@TylerLeonhardt TylerLeonhardt deleted the dont-rely-on-enter-pshostprocess branch March 20, 2019 16:52
TylerLeonhardt added a commit to TylerLeonhardt/PowerShellEditorServices that referenced this pull request Mar 20, 2019
TylerLeonhardt added a commit that referenced this pull request Mar 21, 2019
* backport #881 in a different way

* simpler

* switch back to reflecting

* address feedback

* more feedback

* safe check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants