Skip to content

Set Runspaces to use STA when running in Windows PowerShell #769

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

Merged
merged 5 commits into from
Oct 12, 2018

Conversation

rjmholt
Copy link
Contributor

@rjmholt rjmholt commented Oct 10, 2018

Fixes PowerShell/vscode-powershell#1571.

I carelessly took out the runspace.ApartmentState = ApartmentState.STA line of PSES v1.x without thinking and forgot to reinstate it.

This PR puts it back in.

Copy link
Member

@TylerLeonhardt TylerLeonhardt left a comment

Choose a reason for hiding this comment

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

LGTM. Consider opening an issue in PSStandard about the Apartment state


static PowerShellContext()
{
// PowerShell ApartmentState APIs aren't available in PSStandard, so we need to use reflection
Copy link
Member

Choose a reason for hiding this comment

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

Should this be in PowerShellStandard?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's no such property in PSCore, and PSStandard is PSCore ∩ Windows PowerShell. So it makes sense it's not in PSStandard.

Copy link
Member

Choose a reason for hiding this comment

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

I think it could possibly be in PSStandard but then throw Not Implemented Exception for PSCore.

We shouldn't need to use reflection.

This is an edge case for sure but we should talk about it.

Cc @SteveL-MSFT @JamesWTruher

Copy link
Collaborator

Choose a reason for hiding this comment

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

@tylerl0706 If the member has been removed completely, wouldn't that throw a missing member exception at runtime?

Copy link
Member

Choose a reason for hiding this comment

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

PSCore6 currently doesn't support setting apartmentstate. There's currently no plan to add it back. One of the things I dislike about .NET Std is APIs that exist but throw PlatformNotSupportedException at runtime meaning I can't rely on build time to find api problems for cross platform.

Copy link
Member

Choose a reason for hiding this comment

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

PSCore6 currently doesn't support setting apartmentstate. There's currently no plan to add it back.

Makes sense and I'm definitely not asking for it. More concerned about:

One of the things I dislike about .NET Std is APIs that exist but throw PlatformNotSupportedException at runtime meaning I can't rely on build time to find api problems for cross platform

I can understand your dislike for that but the alternative is to use reflection which isn't really elegant, right?

I don't know the .NET Std team's reasoning behind going down that route but I wouldn't be surprised if this was part of that reasoning:

"not having to rely on reflection for scenarios when you have to do extra work on one platform to get the expected behavior on all"

Copy link
Member

Choose a reason for hiding this comment

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

In this case, it's the lesser of two evils. I don't think reflection in itself is necessary a bad thing as long as you use a publicly documented API.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm personally in favour of compile time checking helping to identify what would otherwise be an unforeseeable runtime crash. But either way, the reflection done here is (1) done once, (2) efficiently compiled to a delegate and (3) called as readably as I could make it -- so hopefully that smooths over the clumsiness of reflection a little bit.

Copy link
Member

Choose a reason for hiding this comment

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

That's totally fine. Just wanted to bring it up. If we want to go down this route we should at least have a doc on best practices for these sorts of things.

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

@@ -174,6 +191,13 @@ public static Runspace CreateRunspace(PSHost psHost)
}

Runspace runspace = RunspaceFactory.CreateRunspace(psHost, initialSessionState);

// Windows PowerShell must be hosted in STA mode
if (RuntimeInformation.FrameworkDescription == DotNetFrameworkDescription)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ends up being the same thing I believe, but I usually write this as

if (RuntimeInformation.FrameworkDescription.Equals(DotNetFrameworkDescription, StringComparison.Ordinal))

If we ever start using code factor or some other static analyzer that often gets flagged.

Copy link
Contributor

@rkeithhill rkeithhill left a comment

Choose a reason for hiding this comment

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

LGTM

@rjmholt rjmholt merged commit 30010f7 into PowerShell:2.0.0 Oct 12, 2018
@rjmholt rjmholt deleted the use-sta-in-winps branch December 12, 2018 05:59
TylerLeonhardt pushed a commit to TylerLeonhardt/PowerShellEditorServices that referenced this pull request Dec 14, 2018
rjmholt added a commit to rjmholt/PowerShellEditorServices that referenced this pull request Jan 18, 2019
rjmholt added a commit to rjmholt/PowerShellEditorServices that referenced this pull request Jan 18, 2019
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.

5 participants