Skip to content

Corrupted text added during save, not during save without formatting #2569

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

Open
codykonior opened this issue Mar 18, 2020 · 15 comments
Open
Labels

Comments

@codykonior
Copy link

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

@ghost ghost added the Needs: Triage Maintainer attention needed! label Mar 18, 2020
@rjmholt
Copy link
Contributor

rjmholt commented Mar 19, 2020

This looks related to #2555.

Thanks for the detailed report @codykonior.

@TylerLeonhardt
Copy link
Member

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 TylerLeonhardt removed the Needs: Triage Maintainer attention needed! label Mar 19, 2020
@robdmoore
Copy link

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.

@ghost ghost added the Needs: Maintainer Attention Maintainer attention needed! label Nov 30, 2020
@robdmoore
Copy link

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

@SydneyhSmith
Copy link
Collaborator

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

@SydneyhSmith SydneyhSmith removed the Needs: Maintainer Attention Maintainer attention needed! label Dec 1, 2020
@robdmoore
Copy link

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

@ghost ghost added the Needs: Maintainer Attention Maintainer attention needed! label Dec 2, 2020
@SydneyhSmith SydneyhSmith removed the Needs: Maintainer Attention Maintainer attention needed! label Dec 3, 2020
@robdmoore
Copy link

It started happening again, here's the logs:

logs.txt

@ghost ghost added the Needs: Maintainer Attention Maintainer attention needed! label Dec 4, 2020
@SydneyhSmith
Copy link
Collaborator

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.

@efie45
Copy link

efie45 commented Mar 15, 2021

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

@ghost ghost added the Needs: Maintainer Attention Maintainer attention needed! label Mar 15, 2021
@SydneyhSmith SydneyhSmith removed the Needs: Maintainer Attention Maintainer attention needed! label Mar 17, 2021
@efie45
Copy link

efie45 commented Apr 13, 2021

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?

@ghost ghost added the Needs: Maintainer Attention Maintainer attention needed! label Apr 13, 2021
@andyleejordan
Copy link
Member

@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?

@madrum
Copy link

madrum commented Apr 25, 2022

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

@andyleejordan
Copy link
Member

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

@madrum
Copy link

madrum commented Apr 27, 2022 via email

@nathan-chappell
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants