Vscode-powershell: Formatting no longer works

Created on 9 Mar 2020  ·  37Comments  ·  Source: PowerShell/vscode-powershell

System Details


System Details Output

### VSCode version: 1.42.1 c47d83b293181d9be64f27ff093689e8e7aed054 x64

### VSCode extensions:
[email protected]        
[email protected]     
[email protected]    
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]


### PSES version: 2.0.0.0

### PowerShell version:

Name                           Value
----                           -----
PSVersion                      7.0.0
PSEdition                      Core
GitCommitId                    7.0.0
OS                             Microsoft Windows 10.0.18363
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Issue Description

Latest version of extension no longer formats scripts

Expected Behaviour

It should format the script like it has forever.

Actual Behaviour

Nothing happens

Attached Logs

Follow the instructions in the troubleshooting docs
about capturing and sending logs.
1583792484-d9b584fd-a4f6-4b7e-9d35-4bec4545762c1583792481203.zip

Copy-ReleaseVariablesToVariableGroup.zip

Area-Code Formatting Issue-Bug

Most helpful comment

Also your script that wasn't formatted correctly would also be hugely helpful.

All 37 comments

Hi @motowilliams, can you please attach your logs as described in the last link in your issue description?

Also your script that wasn't formatted correctly would also be hugely helpful.

apologies if the format is incorrect.
i wanted to comment on this issue instead of starting a new issue

System Details
PowerShell Details
PowerShell version: 5.1.14409.1018
Language mode: FullLanguage

Environment Details
OS description: Microsoft Windows 6.3.9600
OS architecture: X64
Process bitness: 64

.code -v
1.42.1 c47d83b293181d9be64f27ff093689e8e7aed054 x64

.code --list-extensions --show-versions
aws-scripting-guy.[email protected]
bmewburn.[email protected]
DotJoshJohnson.[email protected]
esbenp.[email protected]
felixfbecker.[email protected]
felixfbecker.[email protected]
formulahendry.[email protected]
lonefy.[email protected]
ms-azuretools.[email protected]
ms-dotnettools.[email protected]
ms-mssql.[email protected]
ms-vscode.[email protected]
ms-vscode.[email protected]
mtxr.[email protected]
qcz.[email protected]
redhat.[email protected]
tht13.[email protected]

.$psEditor.EditorServicesVersion
2 0 0 0

.$PSVersionTable
PSVersion 5.1.14409.1018
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14409.1018
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

Issue Description
same as @motowilliams
Latest version of extension no longer formats scripts

Expected Behaviour
same as @motowilliams
It should format the script like it has forever.

Actual Behaviour
same as @motowilliams
Nothing happens

Attached Logs
1583915615-503d9cd1-21e3-4b1a-8553-4faf1ab3e9181583915105557.zip

any .ps1 file will do, here's one example;
Get-Assemblies.zip

Aha - @elrod-johnson yours has something very interesting... a stack trace!

2020-03-11 01:50:40.251 -07:00 [FTL] Failed to handle notification textDocument/formatting
System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.PowerShell.EditorServices.Handlers.DocumentFormattingHandler.<Handle>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at OmniSharp.Extensions.LanguageServer.Server.Pipelines.ResolveCommandPipeline`2.<Handle>d__5.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MediatR.Pipeline.RequestPostProcessorBehavior`2.<Handle>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at MediatR.Pipeline.RequestPreProcessorBehavior`2.<Handle>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at OmniSharp.Extensions.JsonRpc.RequestRouterBase`1.<RouteRequest>d__6.MoveNext()

I'm very suspicious of the exclamation marks in the path... I need to do more testing.

2020-03-11 01:33:56.393 -07:00 [DBG] Resolved path: e:\__FVHS\!!!!!__Azure_AD_etc\_Powershell\Intune_Graph_Samples/RBAC/RBAC_ScopeTags_PolicyUnAssign.ps1

@motowilliams logs didn't have anything useful, I'm afraid. Can you turn on Diagnostic logging and try again?

"powershell.developer.editorServicesLogLevel": "Diagnostic"

My guess is that you both are having the same issue though.

i just tried formatting this file and saw the same response. i.e., no formatting.
E:__FVHS\LDAP_Users_CN.ps1
do you want to see the logs for that?

my powershell files have always been in e:\__FVHS\!!!!!\__Azure_AD_etc\_Powershell\ and they have formatted correctly prior to this latest update.

i just tried formatting this file and saw the same response. i.e., no formatting.
E:__FVHS\LDAP_Users_CN.ps1

Ok, that's useful information

@elrod-johnson yes your logs now would be helpful!

We'll be pushing an update to the preview version of the extension (next week likely) that has some more logging so we can better diagnose this issue.

psvsix.zip

@motowilliams @elrod-johnson can you install this VSIX with the following steps:

  • Expand archive to get the VSIX
  • code --install-extension ./PowerShell-insiders.vsix
  • Disable the PowerShell extension in VSCode and ensure the PowerShell Preview extension is now installed
  • Restart VSCode

I've added some logging here to give us a better idea of the specific issue that's occurring

Can you all give the PowerShell Preview extension a try? We just did a release of it.

Don't forget to disable the regular PowerShell extension for VS Code when you enable the PowerShell Preview extension for VS Code

logs running ms-vscode.[email protected]

.code --list-extensions --show-versions
aws-scripting-guy.[email protected]
bmewburn.[email protected]
DotJoshJohnson.[email protected]
esbenp.[email protected]
felixfbecker.[email protected]
felixfbecker.[email protected]
formulahendry.[email protected]
lonefy.[email protected]
ms-azuretools.[email protected]
ms-dotnettools.[email protected]
ms-mssql.[email protected]
ms-vscode.[email protected]
ms-vscode.[email protected] << disabled
ms-vscode.[email protected]
mtxr.[email protected]
qcz.[email protected]
redhat.[email protected]
tht13.[email protected]

a number of format attempts on several files, none succeeded
1584096315-e492a07e-5241-484f-973f-59e365b09ea61584096313716.zip

thanks for working on it.
great extension, especially when formatting works!

@elrod-johnson can you try running the steps in https://github.com/PowerShell/vscode-powershell/issues/2543#issuecomment-598378220 as well.

I put together the extension in that comment to log the formatting issue better so that we might be able to trace the null-ref exception

here you go Tyler
let me know if you need anything else.
1584186319-1a75101c-90f0-4d66-8dc0-e8b4e0fa578f1584186150151.zip

@elrod-johnson were you also able try running the steps in #2543 (comment)?
Thanks for the additional logs as well!

@SydneyhSmith
I tried to do those steps.
are you seeing that i didn't do them?
here's a new log after i tried to do them again.
here's what i did:

  1. disabled PowerShell Preview since it was already installed,
  2. ran code --install-extension ./PowerShell-insiders.vsix
  3. it installed successfully
  4. reenabled PowerShell Preview
  5. closed and reopened vscode
  6. tried to format a ps1

regular Powershell ext was disabled for all of this
thanks

1584522859-b2c730ed-d03a-437d-a7f1-7d9e01abf0c11584522856881.zip

Thanks @elrod-johnson! Unfortunately we still don't have enough information to determine an exact cause but it looks to be a race condition issue on the surface. We will have to do more investigation here, thanks again for all the info you have provided, we will update you when we have more steps to try!

any progress on this?
the world is pretty much upside down right now and much more important things are staring us in the face.
but if you're all still healthy and able to work, can you please let me know when you think you might have a solution.
it really sucks writing powershell without a formatter.
on the other hand, i'm grateful that i still have the luxury to care about something like this.
thanks

@elrod-johnson sorry for the delay. Some higher priority issues came up around intellisense performance that needed immediate attention...

From your last logs it looks like this was the problem script:

WVD_Create-Users__Add-to-RdsAppGroup.ps1

Can we see that one so I can try to repro locally? I'll have another build for you to try out (with even more logging for us to get to the bottom of this) in about an hour or so.

@elrod-johnson here's the new vsix for you. Same steps as before...

  • download,
  • unzip,
  • run code --install-extension ./PowerShell-insiders.vsix

psvsix.zip

Hopefully this will be the last time I will have to ask this of you. Please share your logs after the fact.

sorry for the delay.
haven't checked this page in a few days.
WVD_Create-Users__Add-to-RdsAppGroup.ps1 contains too much confidential info for upload.
i've attached the file i used for this log.
thanks

1586228579-6a476008-35e0-4e35-a0f2-1f767d6a3a051586228576229.zip

Registry-Load-Hive.zip

@elrod-johnson that is VERY helpful. Ok. We are getting closer I can feel it. I have 2 suggestions:

  1. Can you run the formatter manually like so in the PowerShell Integrated Console:
# With Registry-Load-Hive.ps1 open
Invoke-Formatter -ScriptDefinition $psEditor.GetEditorContext().CurrentFile.GetText()
# Or
Invoke-Formatter -ScriptDefinition (Get-Content -Raw ./path/to/Registry-Load-Hive.ps1)

And let me know if the output is what you expect.

  1. I have another vsix for you! This time with some logging closer to where the issue lies:
    psvsix.zip

Thank you soooo much for your help. I want to get to the bottom of this for you.

you're definitely making progress!
here's some nit-picky notes comparing formatted and unformatted versions in zip
the 'old' version formatted all these things perfectly

my settings:
  "powershell.codeFormatting.useCorrectCasing": true,
  "powershell.codeFormatting.preset": "OTBS",
  "powershell.codeFormatting.autoCorrectAliases": true,
  "powershell.scriptAnalysis.enable": false,
abbrev key:
un 10-14; 10-13`
    means lines 10-14 in unformatted file, compared to lines 10-13 in formatted file

'%' at un 38 should have become 'Where-Object' at 37`
    '%' at un 38 means '%' at line 38 in unformatted file
    'Where-Object' at 37 means 'Where-Object' at line 37 in formatted file

un 10-14; 10-13
indented correctly
cap: select > Select
select should have become Select-Object

un 17-19; 16-18
indented incorrectly
un version indenting was more correct
cap: select > Select good
select should have become Select-Object

un 22-23; 21-22
indented incorrectly
un version indenting was more correct
cap: select > Select good
select should have become Select-Object

un 37-40; 36-39
'%' at un 38 should have become 'Where-Object' at 37
'foreach' at un 40 should have become 'Foreach-Object' at 39

_Powershell.zip

1586438290-1c0620fc-f489-41ce-9bae-2e4f139811271586438063707.zip

thanks Tyler

@elrod-johnson wait I don't see a stack trace in your log.

Does formatting still crash for you or does it at least run now?

I should clarify, running manually doesn't prove that it works, it just proves that it's probably not a PSScriptAnalyzer issue.

You should try running the Formatter from VS Code just as you did before to see if it's fixed. I don't think it'll be fixed but I should have more info.

sorry, i was groggy this morning. here ya go.

1586485494-93ca9de9-3e8e-454f-a4a2-1d9eb8d5cd511586443543789.zip

@elrod-johnson still not seeing the crash anymore... Can you elaborate on what you're seeing (or not seeing) when you run Format in VS Code?

i tried to start over from scratch with the extension.
please read the readme file for details.

vscode.powershell-preview_README.txt

1586509074-407a2197-18d8-4b06-a23b-bc3dac17cd971586509071629.zip

@elrod-johnson from the logs you've sent me, it doesn't show that you ran the Formatter.

Can you run it a few times in VS Code and attach those logs?

Another thing, can you also try selecting all the text in your file and run the "Format Selection" command in VS Code? I'm curious if that behaves differently.

Before you do this make sure you're still using the build I gave you. You can see the version number in the Extension pane.

Once again thank you for going through this with me 😅 there was another person who opened a similar issue so I'm hoping they'll come over here to help as a tester for me as well

i disabled extension auto-update after installing ms-vscode.powershell-preview-2020.4.0-CI.49627.
that might have been causing log problems.
i clicked 'format document', then ctrl-a, 'format selection' for the files in this log.

1586603996-5f580f96-8801-43e7-a8ac-b8629678e0601586603598431.zip

Ha! Today I learned that the text editor I use on my phone to open your logs cut out the content about half way 😅now on my laptop, I can see the crash perfectly.

It looks like I was also groggy when I gave you that last build to try as I forgot to push my changes for more logging when I snapped a dev build to give you... 🤦‍♂️

This build has the logging in it:
psvsix.zip

Thanks for your patience, @elrod-johnson

this log is using ms-vscode.powershell-preview-2020.4.2-CI.49933

1586682518-cdf71ef4-4bb7-4057-8aad-e422c88775921586682515734.zip

Ok @elrod-johnson nothing useful in that... how about this one? I've basically added line-by-line logging (nothing sensitive just a bunch of null checks).

psvsix.zip

Keep in mind, the vsix's name is now:

powershell-preview-2020.4.3-CI.50071-Windows.vsix 

attached the two scripts i tried to format.
ran format document and format selection for each.

1587017167-4fd38891-1e6a-4d96-b2c2-43c721cc30991587017164072.zip

__powershell-preview.zip

@elrod-johnson that put the pieces all together for me...

here's what was happening... you had this setting:

"powershell.scriptAnalysis.enable": false,

this disables PSScriptAnalyzer capabilities in the extension including:

  • "Problems" in your open editor file
  • Formatting

Since that was off, formatting wasn't working.

With that said, I don't see _why_ this needs to be the case. You should be able to run the formatter and have "Problems" turned off.

I think I have an idea for a fix and will report back.

@elrod-johnson I think this build might just fix your problem 😄 please let me know!

psvsix.zip

sure looks fixed to me guys!
i hammered on it as much as i could and it does everything the pre-update version did.

thanks a lot for staying on this for so long.
it sure makes my job a lot easier.
i have a hard time reading my own code when it's not formatted consistently.
i spend almost all my time writing posh code these days and it's been very frustrating.
i really appreciate your work.

Glad to hear @elrod-johnson ! This will probably be in the PowerShell Preview extension next week so you'll be able to update to that then. My guess is that this well make it into Stable probably in a few weeks.

Was this page helpful?
0 / 5 - 0 ratings