vscode-powershell icon indicating copy to clipboard operation
vscode-powershell copied to clipboard

Corrupted text added during save, not during save without formatting

Open codykonior opened this issue 5 years ago • 15 comments

System Details

System Details Output

### VSCode version: 1.43.0 78a4c91400152c0f27ba4d363eb56d2835f9903a x64

### VSCode extensions:
[email protected]
[email protected]
[email protected]
[email protected] <-- Disabled

### PSES version:

### PowerShell version:

Name                           Value
----                           -----
PSVersion                      5.1.14393.2879
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14393.2879}
BuildVersion                   10.0.14393.2879
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Issue Description

When I'm making edits to PowerShell scripts in VS Code (after a long day) sometimes Ctrl-S (Save) is adding corrupted text to the file. You can see it appear in the editor window and then get saved.

  • If you hit save repeatedly, more and more corruption occurs.
  • If you use 'File: Save Without Formatting' then the corrupted text doesn't get added.
  • I have 'Editor: Format On Save' disabled. I checked User/Workspace/Folder settings.
  • My workspace has 'Editor: Trim Auto Whitespace', 'Editor: Trim Final Newlines', 'Editor: Trim Trailing Whitespace' enabled, but when I disabled these, and hit Ctrl-S, the corruption was still added.

When the problem occurs, it's only in a single window/file. Reloading VS Code makes the problem go away. I didn't try closing just that code window.

Attached Logs

Unfortunately I got to this bit before I reloaded my session (though this happens every few days so I thought I'd open the issue now). The only thing of note in EditorServices.log is below but I'm not sure if it was related, because I think it was about 10 minutes before this happened, BUT I had the same problem on another file at around the same time:

2020-03-18 18:23:47.706 +08:00 [ERR] Execution of the following command(s) completed with errors:

    Microsoft.PowerShell.Core\Get-Command $true -ErrorAction Ignore


2020-03-18 18:30:16.037 +08:00 [ERR] Invalid Range (Parameter 'range')
2020-03-18 18:30:16.037 +08:00 [ERR] Formatter returned null result
2020-03-18 18:30:32.051 +08:00 [FTL] Failed to handle request textDocument/didChange
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'Position 26:17 is outside of the column range of 1 to 14.')
   at Microsoft.PowerShell.EditorServices.Services.TextDocument.ScriptFile.ValidatePosition(Int32 line, Int32 column) in D:\a\1\s\src\PowerShellEditorServices\Services\TextDocument\ScriptFile.cs:line 308
   at Microsoft.PowerShell.EditorServices.Services.TextDocument.ScriptFile.ApplyChange(FileChange fileChange) in D:\a\1\s\src\PowerShellEditorServices\Services\TextDocument\ScriptFile.cs:line 332
   at Microsoft.PowerShell.EditorServices.Handlers.TextDocumentHandler.Handle(DidChangeTextDocumentParams notification, CancellationToken token) in D:\a\1\s\src\PowerShellEditorServices\Services\TextDocument\Handlers\TextDocumentHandler.cs:line 68
   at OmniSharp.Extensions.LanguageServer.Server.Pipelines.ResolveCommandPipeline`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
   at MediatR.Pipeline.RequestPostProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
   at MediatR.Pipeline.RequestPreProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
   at OmniSharp.Extensions.JsonRpc.RequestRouterBase`1.RouteNotification(TDescriptor descriptor, Notification notification, CancellationToken token)
2020-03-18 18:30:33.024 +08:00 [FTL] Failed to handle request textDocument/didChange
System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. (Parameter 'Position 41:5 is outside of the column range of 1 to 2.')
   at Microsoft.PowerShell.EditorServices.Services.TextDocument.ScriptFile.ValidatePosition(Int32 line, Int32 column) in D:\a\1\s\src\PowerShellEditorServices\Services\TextDocument\ScriptFile.cs:line 308
   at Microsoft.PowerShell.EditorServices.Services.TextDocument.ScriptFile.ApplyChange(FileChange fileChange) in D:\a\1\s\src\PowerShellEditorServices\Services\TextDocument\ScriptFile.cs:line 332
   at Microsoft.PowerShell.EditorServices.Handlers.TextDocumentHandler.Handle(DidChangeTextDocumentParams notification, CancellationToken token) in D:\a\1\s\src\PowerShellEditorServices\Services\TextDocument\Handlers\TextDocumentHandler.cs:line 68
   at OmniSharp.Extensions.LanguageServer.Server.Pipelines.ResolveCommandPipeline`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
   at MediatR.Pipeline.RequestPostProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
   at MediatR.Pipeline.RequestPreProcessorBehavior`2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate`1 next)
   at OmniSharp.Extensions.JsonRpc.RequestRouterBase`1.RouteNotification(TDescriptor descriptor, Notification notification, CancellationToken token)
2020-03-18 18:31:04.029 +08:00 [WRN] textDocument/codeAction skipping diagnostic with empty Code field: PowerShell Unexpected token '$ParameterValue' in expression or statement.
2020-03-18 18:31:13.261 +08:00 [WRN] textDocument/codeAction skipping diagnostic with empty Code field: PowerShell unexpected token '$ParameterValue', expected 'begin', 'process', 'end', or 'dynamicparam'.

What I did capture though were images of the corruption. I had just changed the type and property name, so, it seemed to be corrupting with part of what was previously removed.

Pre save

1  Pre Save

Save 1

2  Save 1

Save 2

3  Save 2

Save 3

4  Save 3

codykonior avatar Mar 18 '20 11:03 codykonior

This looks related to https://github.com/PowerShell/vscode-powershell/issues/2555.

Thanks for the detailed report @codykonior.

rjmholt avatar Mar 19 '20 21:03 rjmholt

It would also be great if you could provide the payload logs, information for how to do that is here: https://github.com/PowerShell/vscode-powershell/blob/master/docs/troubleshooting.md#provide-language-server-protocol-payload-logs

TylerLeonhardt avatar Mar 19 '20 21:03 TylerLeonhardt

Just confirming that this has been happening lately to myself and some colleagues of mine.

Next time it happens I'll try and pull the payload logs.

robdmoore avatar Nov 30 '20 08:11 robdmoore

Can you confirm that the link you sent is correct? I just tried setting the trace settings to verbose for powershell, but it doesn't appear to be a setting?

image

robdmoore avatar Nov 30 '20 08:11 robdmoore

The bottom one is correct @robdmoore it is a hidden setting

SydneyhSmith avatar Dec 01 '20 22:12 SydneyhSmith

Noted - I'll add that setting and loop back with logs if I notice the bug happening again.

robdmoore avatar Dec 02 '20 01:12 robdmoore

It started happening again, here's the logs:

logs.txt

robdmoore avatar Dec 04 '20 17:12 robdmoore

Thanks @robdmoore, sorry that you are hitting this--definitely a frustrating issue! For now you may need to proceed without "Format on Save", it looks like this may be an issue with the way the formatter interacts with our file abstraction layer, but we have not had time to do a full investigation.

SydneyhSmith avatar Dec 08 '20 23:12 SydneyhSmith

I've been having problems with this as well. I have a build script that injects the contents of module files into scripts for an application that doesn't support the use of modules currently. Unfortunately I'm having to build every script by hand for the time being as when I save the file the entire thing gets corrupted - duplicate lines, duplicate sections of 50+ lines of code, random characters and brackets added in and sections completely removed.

However, even when I save without the formatting, as soon as I format the document this happens again.

Edit: It is happening on preview as well. I'm not even able to save somewhere else and open it in VSCode. This renders VSCode unusable for me until this is fixed. I'd really love to hear any ideas for workarounds if anyone has any ideas.

Here's an interesting example. This is with all extensions disabled but PowerShell and semantic highlighting disabled. This is what my codes looks like throughout the entire file.

Screen Shot 2021-03-15 at 12 58 49 PM

efie45 avatar Mar 15 '21 14:03 efie45

Just checking on this to see if this has gotten any attention? I currently have no ability to use formatting with or without it being attached to 'on save'. Whenever I format PowerShell scripts, particularly larger files, this happens. I've been entirely unable to use the formatting functionality for almost a month now and it's really bumming me out. Is there anything I can do to help move this forward?

efie45 avatar Apr 13 '21 01:04 efie45

@efie45 your coloring issue looks more like #3295

Can any of you test if the formatting issues happen when using PSScriptAnalyzer's Invoke-Formatter by itself?

andyleejordan avatar Apr 23 '21 19:04 andyleejordan

Restarting VSCode helped me work around this issue. So, maybe a memory leak contributing to the issue?

madrum avatar Apr 25 '22 15:04 madrum

@efie45 @madrum is this still happening with the PowerShell Preview for VS Code?

andyleejordan avatar Apr 25 '22 16:04 andyleejordan

Yes, the issue happened for me this week. I restarted VSCode and it stopped happening.

They file I was working on had about 780 lines. The last 8-10 lines were getting duplicated each time I made a small change and saved.

madrum avatar Apr 27 '22 00:04 madrum

Same here, happening when I try to format the content. I've attached a gif. I probably won't send you guys all kinds of logs and debugging information, it's clearly super broken and you guys should easily be able to reproduce the issue (if you can't that's also an issue).

@andschwa Invoke-Formatter did not cause a problem for me.

Do you guys accept pull requests? There is apparently no decent formatter for powershell scripts, and I would like to have one.

powershell-whitespace-fuckup

nathan-chappell avatar Mar 02 '23 11:03 nathan-chappell