PowerShell / vscode-powershell

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

Profile fails to load when `System.Windows.Forms` objects are used, hanging extension #3394

Closed baldieavenger closed 3 years ago

baldieavenger commented 3 years ago

Hey team, Since the latest update, i'm not able to use the editor to run any code. Love that I wont get prompted to save now when running, not loving the fact the PowerShell session never completes now when running my auto loading profile (365 module signs in, which appear to be successful - Other than the latest ExchangeManagement module never popping out the creds prompt but I can work around using the lower version for now!) All my modules pop up and sign ins are successful, but I cant figure out why the session hasn't finished to allow me to use the editor to run code now? I've downgraded for now back to v2021.2.2 which lets me auto load my profile and sign in and let me carry on using the editor without issue. VSCodeSessionError

andyleejordan commented 3 years ago

Hi,

Could you please provide more logging and environment information by following these steps?

Thanks!

baldieavenger commented 3 years ago

Hi,

Could you please provide more logging and environment information by following these steps?

Thanks!

Hiya!

Here you go, wasn't sure which so you have all 3!

I updated to v2021.5.1, restarted, entered creds from the prompts and tried to run something using F5 from the editor again and got the same issue.

Thanks! VSCode-Logs-20210603..zip

andyleejordan commented 3 years ago

All three is perfect, thanks!

andyleejordan commented 3 years ago

Ah, I see in the logs that this is with PowerShell 5.1. Can you try PowerShell 7 and see if it still repros?

baldieavenger commented 3 years ago

I can, but, it's not managed Enterprise wide so this wont help for everyone here that uses Code as well as 5.1 on our prod servers. Also, embarrassingly im not sure how to update Code to use Ps7.... Is 5.1 no longer supported?

baldieavenger commented 3 years ago

Ignore this comment, it is still an issue with the latest VSCode, I just hadnt moved editors away from the settings page so didnt see the "Starting Powershell" notification not going away after everything has completed!

baldieavenger commented 3 years ago

Side note.... Now that im not prompted to save changes when using Editor, it doesnt run the code with the changes I made, unless I save it first. Is that intentional? I assumed it would allow me to run whatever was in that editor at that time, regardless of if id saved it or not?

andyleejordan commented 3 years ago

It should only not be prompting for untitled files, and that should only be the case in VS Code Insiders right now. Otherwise that behavior is unchanged. What happened was that we added this default config for their upcoming feature: https://github.com/PowerShell/vscode-powershell/pull/3357/files

For PowerShell files, it is to set to prompt for nonUntitledEditorsInActiveGroup or, "all editors except those that are untitled."

andyleejordan commented 3 years ago

Sorry, haven't had a chance to look at the main part of this issue yet.

baldieavenger commented 3 years ago

No worries, I'm in the UK so the weekend has already started

baldieavenger commented 3 years ago

Happy Monday! Rearing to go to help out where I can for this!

andyleejordan commented 3 years ago

Hey! Sorry, I was on vacation this last week and am just now back today. I should get to this on Monday, currently just triaging everything.

andyleejordan commented 3 years ago

@baldieavenger I'm wondering if this has to do with the main PowerShell 5.1 bug https://github.com/PowerShell/vscode-powershell/issues/3395#issuecomment-859969488 that cropped up this release. Can you test this extension build? PowerShell-insiders.vsix.zip

baldieavenger commented 3 years ago

Hi Andrew, I tried this but it didnt help. I thought it might just be me and my local, but my colleague now sees the same issue after updating to Powershell too so not just me!

baldieavenger commented 3 years ago

Hi Andrew, Anything more you need from me to dig into this? Thanks! John

baldieavenger commented 3 years ago

Something to add! I use a little Microsoft.VSCode_profile.ps1 which contains a pop out then invoke command so I can choose the ps1 profile I want to use at the time and its this causing the issue! If I just use my log in script as Microsoft.VSCode_profile.ps1 it auto loads, prompts, signs in and then has finished doing its thing! If I use my function to open and let me choose which one, it doesnt! Microsoft.VSCode_profile.txt

andyleejordan commented 3 years ago

Oh, that is helpful. I see your function uses System.Windows.Forms which I know is causing other issues too: https://github.com/PowerShell/vscode-powershell/issues/3410#issuecomment-860976636

baldieavenger commented 3 years ago

Looks like the same cause then for both of these too! https://github.com/MicrosoftDocs/office-docs-powershell/issues/7895 https://github.com/PowerShell/vscode-powershell/issues/3396

andyleejordan commented 3 years ago

Ah, well, at least we know the problem. My understanding is that it may be difficult to fix due to the way System.Windows.Forms interoperates with a program's threading. But maybe https://github.com/PowerShell/PowerShellEditorServices/issues/1295 will fix it? We shall see...

baldieavenger commented 3 years ago

Thanks! Whilst I'm here...... Any ideas on another way I can open a dialogue box to ask me for a ps1 to load? Lol

andyleejordan commented 3 years ago

@rjmholt thinks that we may have missed a location in PSES where we are not using ConfigureAwait(false) which causes these issues with System.Windows.Forms, so it looks like a bug on our end that we need to investigate. Sadly we cannot think of another way to open a dialogue box (though I suppose you could just use a Y/N prompt on the terminal?)

andyleejordan commented 3 years ago

Hey @baldieavenger, could you test this dev build and let me know if it solves the issue? powershell-preview-2021.8.1.zip

baldieavenger commented 3 years ago

Hi Andrew, Installed and opened Code, prompted for my profile.ps1 and then starts to load but doesnt seem to actually open it and load anything. I also dont get an option for the dropdown integrated terminal now when I did before, so I assume I wouldnt be able to hit f5 to run my script in the editor if it did load anyways (Unsure if thats helpful but thought id mention!)

Logs attached.

Thanks!

John

Logs-ms-vscode.powershell-preview-2021.8.1.zip

youngr137 commented 3 years ago

Ok so I installed the preview and tested with the simple HelloWorld.ps1 that I started with and that worked first time however I could not get it to start again (f5). There is an ETIMEDOUT logged in the output for the extension. I have tried with other scripts that use System.Window.Forms and they all appear to do the same. That is run once on first start up of Visual Studio but can't be started again. Occasionally when a program exits instead of the normal prompt ( PS >) I just get a PS > prompt. When this occurs I was able to start the program again and everything worked as I expected. Once in a while a minimised Console Window Host appeared on starting Visual Studio. I added log files. I did not see either of the 'occasional' events like the prompt stopping with PS or the Console window during the logged test run.

1628603130-5d2c9f62-1894-4d70-a6d2-fc956fc65b441628603105575.zip

SeeminglyScience commented 3 years ago

Hey @youngr137 / @baldieavenger , sorry if this has already been asked, but have you checked if the dialog is opening behind the VSCode window?

andyleejordan commented 3 years ago

@baldieavenger I tested that build with your posted profile.ps1 and it seems to be working exactly as expected now. As @SeeminglyScience mentioned, there's an entirely separate upstream issue with VS Code that GUI windows are displayed behind the VS Code window, and there's sadly nothing we as extension authors can do about it. So while debugging your profile, after I've stepped over the line which creates the GUI window, I then alt-tabbed to it (as it was behind the editor), selected a file, and then was able to continue debugging and hit a breakpoint after that code.

@youngr137 you are probably seeing the same thing. My guess it that it timed out because you never found the (rather hidden) dialogue window behind the editor.

andyleejordan commented 3 years ago

Here's that issue: #1175

youngr137 commented 3 years ago

I am sure there is no hidden dialogue. I do see that when debugging with Visual Studio but the issue is that after working normally on first attempt the second run will timeout and give 10/08/2021 18:05:55 [ERROR] - Error on Debug Adapter: Error: connect ETIMEDOUT \.\pipe\PSES_pmxwttse.nu3 on the output for the Powershell Extension. The code never gets to start under the debugger.

baldieavenger commented 3 years ago

Hi Both, @andschwa + @SeeminglyScience Ive looked for Windows behind and everywhere else but this doesnt help me. Alt + Tab doesnt show anything open either. My Microsoft.VSCode_profile.ps1 contains the choose file prompt.

Microsoft.VSCode_profile.txt

That still seems to pop out for me in front and I can choose my "365AdminSignIns.ps1" to invoke, which is my previous version of "Microsoft.VSCode_profile.ps1" (connect to Exchange, Azure, SPO etc)

365AdminSignIns.txt

It starts and I can see its starting to connect, but at the point normally a pop up would happen which is a Microsoft authenticaiton page to enter credentials and verify via MFA if applicable (Im using a encrypted txt file would sign me in without input so I dont need to see it) its just freezing and not moving past this point. (Looks like line 6 in the attached 365AdminSignIns.txt - was a ps1 but cant attach ps1 here)

Its not that I need to see it or interactive with it but its being blocked from popping up and doing its own thing in the first place which then means it cant sign in.

Nothing in the debug console or output or problems tabs either.

Logs2-ms-vscode.powershell-preview-2021.8.1.zip

This does work fine in ISE with the pop up, file selection and then invoke.

My 365AdminSignIns.txt file does work perfectly too if I move the contents into "Microsoft.VSCode_profile.ps1"

:(

andyleejordan commented 3 years ago

10/08/2021 18:05:55 [ERROR] - Error on Debug Adapter: Error: connect ETIMEDOUT .\pipe\PSES_pmxwttse.nu3

Well that's strange, and seems very different 😢

andyleejordan commented 3 years ago

My 365AdminSignIns.txt file does work perfectly too if I move the contents into "Microsoft.VSCode_profile.ps1"

Ok, wait, so at this point the issue is only happening when Connect-ExchangeOnline -UserPrincipalName adm-User@domain.com -ShowProgress $true (which presumably uses System.Windows.Forms) is piped e.g. "&'$file'" | invoke-expression, is that right?

@rjmholt any ideas?

andyleejordan commented 3 years ago

@youngr137 You seem to be seeing the same thing as what was just reported in #3505

Abax378 commented 3 years ago

Hey @baldieavenger, could you test this dev build and let me know if it solves the issue? powershell-preview-2021.8.1.zip

I disabled the current PS extension in VS Code and loaded the .vsix file referenced above. On startup of VS Code, the terminal posts a PID like this

PID: 15156

then keeps posting the same line in a repeating loop. Eventually, VS Code reports that loading PS is taking longer than usual and sometimes reports it could not load the language. Killed the terminal window and started a new one, but subsequent terminals don't try to load PS, so no effect. Re-booted Windows 10, tried again, same result.

Debugger won't run without PS, so unable to test.

I'm running the latest update for VS Code:

Version: 1.59.0 (system setup) Commit: 379476f0e13988d90fab105c5c19e7abc8b1dea8 Date: 2021-08-04T23:13:12.822Z Electron: 13.1.7 Chrome: 91.0.4472.124 Node.js: 14.16.0 V8: 9.1.269.36-electron.0 OS: Windows_NT x64 10.0.19042

andyleejordan commented 3 years ago

@Abax378

I disabled the current PS extension in VS Code and loaded the .vsix file referenced above. The terminal posts a PID like this

PID: 15156

then keeps posting the same line in a repeating loop.

It is waiting for a debugger to be attached to the PowerShell process. That is due to a user setting:

"powershell.developer.editorServicesWaitForDebugger": true,

Please set that to false instead and try again!

Abax378 commented 3 years ago
"powershell.developer.editorServicesWaitForDebugger": true,

Did that. Debugger works as expected several times in a row. Same result with a restart of VS Code.

baldieavenger commented 3 years ago

I didnt have this setting so added it, if set to true I get either PID:0 7836, PID: 22228, PID: 14684, PID: 16512 when opening. (Is it meant to be different every time? Should I even be looking to add this and let you know what it does??) If set to fale, I get the same behaviour which is as you asked @andschwa above

andyleejordan commented 3 years ago

I didnt have this setting so added it, if set to true I get either PID:0 7836, PID: 22228, PID: 14684, PID: 16512 when opening. (Is it meant to be different every time? Should I even be looking to add this and let you know what it does??)

This setting instructs the extension to wait for a debugger to be attached to the PowerShell process that it starts. That's why it's emitting the PID (which is different every time because new processes each have a new Process ID). It's used by developers for debugging the extension. Unless you want to attach a debugger to the extension's PowerShell process, leave it set to false.

I get the same behaviour which is as you asked @andschwa above

Can you state what that behavior is? Got a lot of issues and discussions happening, trying to keep this all straight is hard.

baldieavenger commented 3 years ago

No problem! After using Microsoft.VSCode_profile.txt to open a prompt to allow me to select 365AdminSignIns.txt which should auto sign me in to Exchange Online, I dont get the authentication box pop up whih then passes through my creds to sign me in.

andyleejordan commented 3 years ago

Instead of:

    $file = Get-FileName -initialDirectory "C:\Users\USER\PATH\Documents\WindowsPowerShell"

   "&'$file'" | invoke-expression

Can you try:

    $file = Get-FileName -initialDirectory "C:\Users\USER\PATH\Documents\WindowsPowerShell"

   & $file
baldieavenger commented 3 years ago

No change :(

andyleejordan commented 3 years ago

You're certain you've uninstalled the PowerShell extension from the marketplace and manually installed the VSIX I posted? I can no longer reproduce your issue with the dev build I posted, while I could reproduce it from the marketplace version. What version are you seeing on startup in the integrated console?

baldieavenger commented 3 years ago

I think so! image

This is interesting though, if I skip Connect-ExchangeOnline -UserPrincipalName adm-User@domain.com -ShowProgress $true, the next one to pop out is Connect-SPOService -Url "https://domain-admin.sharepoint.com" and this one does pop out and let me enter my credentials.

baldieavenger commented 3 years ago

I tried changing Connect-ExchangeOnline -UserPrincipalName adm-User@domain.com -ShowProgress $true to $cred = Get-Credential Connect-ExchangeOnline -Credential $cred -ShowProgress $false This allows me to type my creds in the terminal and I wondered if that would mean it doesnt need to open the browser to authenticate, but its still the same so it must try and use tha pop up browser to authenticate regardless of what I do.

andyleejordan commented 3 years ago

Since the original bug in this issue is fixed, and that fix is now merged and will be in the next release, I will mark this issue as resolved. For your new problem, could you kindly open a new issue? You may want to wait until the extension is updated so you can try to reproduce it...take a look on Monday!

ghost commented 3 years ago

This issue has been marked as fixed. It has been automatically closed for housekeeping purposes.

baldieavenger commented 3 years ago

I mean, its the same issue just worked through symptoms and the Beta isnt working for me as mentioned and after trying everything requested and providing all the logs but hey ho.... Ill come back if I still have issues and can confirm a colleague has the same issues after we update on Monday. Thanks!

andyleejordan commented 3 years ago

So, your issue goes away when you execute the code directly in your profile script, right? It now only fails when it's executed through "&'$file'" | invoke-expression? That seems to me to be a completely separate (albeit obviously still extant and annoying! I know) issue from the the bug around System.Windows.Forms.

strickdd commented 3 years ago

Hey @baldieavenger, could you test this dev build and let me know if it solves the issue? powershell-preview-2021.8.1.zip

@andschwa I installed the preview linked and initial tests have allowed me to debug as expected. I'll continue to run some tests today. If debugging fails again, should I open a new issue or is there one I can reference since this has been closed?

rjmholt commented 3 years ago

If debugging fails again, should I open a new issue or is there one I can reference since this has been closed?

If it fails again it's probably a new (if related) problem, so worth a new issue for fresh discussion

baldieavenger commented 3 years ago

So, your issue goes away when you execute the code directly in your profile script, right? It now only fails when it's executed through "&'$file'" | invoke-expression? That seems to me to be a completely separate (albeit obviously still extant and annoying! I know) issue from the the bug around System.Windows.Forms.

It always worked when executing the code directly, I've only ever had the problem when using the "&'$file'" | invoke-expression but ill give it a try, just in case when the new version is released! Thanks.