PowerShell / vscode-powershell

Provides PowerShell language and debugging support for Visual Studio Code
https://marketplace.visualstudio.com/items/ms-vscode.PowerShell
MIT License
1.69k stars 481 forks source link

(PS 7.0 preview) Powershell Integrated Console launches in different apartment state vs using "new terminal" #2216

Open TheManii opened 4 years ago

TheManii commented 4 years ago

Issue Description

I am not able to run my existing WPF + PS based code while specifically in the integrated console due to the fact that the integrated console runs in MTA. The same code does work fine either in a normal, standalone session (which I mean as launching PWSH.exe from the run dialog/similar) of PS 7 or in a standard (non integrated console) session of PWSH in VSC. Standalone sessions and standard sessions in VSC run under STA.

Background

Powershell 7.0 preview 3 (re) added STA as an available apartment state and also set it back to being the default apartment state.

STA is needed to run WPF and WinForm code as they will only run under STA. Previously PS Core removed STA as an option because there was no need to run under anything besides MTA as WPF/WF were no longer supported, but 7.0 preview 3 defaulted back to STA as a feature point to implicitly support WPF/WF.

Attached Logs

Steps to reproduce:

  1. Set Powershell 7 as the default session 1a. Launch the integrated console 2a. run [System.Threading.Thread]::CurrentThread.GetApartmentState() 3a. Output is: MTA

1b. Launch a "normal"/standalone pwsh session 2b. run [System.Threading.Thread]::CurrentThread.GetApartmentState() 3b. Output is: STA

Expected Results for step 3a: Either the output is also STA or an option to use STA by default

Environment Information

Visual Studio Code

Name Version
Operating System Windows_NT x64 10.0.18362
VSCode 1.38.1
PowerShell Extension Version 2019.9.0

PowerShell Information

Name Value
PSVersion 7.0.0-preview.4
PSEdition Core
GitCommitId 7.0.0-preview.4
OS Microsoft Windows 10.0.18362
Platform Win32NT
PSCompatibleVersions 1.0 2.0 3.0 4.0 5.0 5.1.10032.0 6.0.0 6.1.0 6.2.0 7.0.0-preview.4
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0

Visual Studio Code Extensions

Visual Studio Code Extensions(Click to Expand) |Extension|Author|Version| |---|---|---| |code-spell-checker|streetsidesoftware|1.7.17| |cpptools|ms-vscode|0.24.1| |csharp|ms-vscode|1.21.3| |docs-markdown|docsmsft|0.2.13| |githistory|donjayamanne|0.4.6| |java|redhat|0.47.0| |powershell|ms-vscode|2019.9.0| |remote-wsl|ms-vscode-remote|0.38.10| |vscode-java-debug|vscjava|0.20.0| |vscode-java-dependency|vscjava|0.5.1| |vscode-java-pack|vscjava|0.7.1| |vscode-java-test|vscjava|0.19.0| |vscode-maven|vscjava|0.18.1| |vscodeintellicode|VisualStudioExptTeam|1.1.9|
TylerLeonhardt commented 4 years ago

Interesting... We do this bit of reflection in order to set the ApartmentState properly.

@SteveL-MSFT when you added that support back in, did the API change?

SteveL-MSFT commented 4 years ago

@TylerLeonhardt the API did not change. Note that only the default runspace of pwsh is STA, new runspaces are MTA by default (same behavior is WinPS)

TylerLeonhardt commented 4 years ago

Great. We just need to add PS7 to that first if statement then.