Skip to content

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

Open

Description

System Details

System Details Output

### VSCode version: 1.43.0 78a4c91400152c0f27ba4d363eb56d2835f9903a x64

### VSCode extensions:
ms-mssql.mssql@1.9.0
ms-vscode.csharp@1.21.3
ms-vscode.powershell@2020.3.0
ms-vscode.powershell-preview@2020.2.0 <-- 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

Activity

  1. ghost added
    Needs: TriageMaintainer attention needed!
    on Mar 18, 2020
  2. rjmholt commented on Mar 19, 2020

    @rjmholt
    Contributor

    This looks related to #2555.

    Thanks for the detailed report Cody Konior (@codykonior).

  3. TylerLeonhardt commented on Mar 19, 2020

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

  4. robdmoore commented on Nov 30, 2020

    @robdmoore

    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.

  5. robdmoore commented on Nov 30, 2020

    @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

  6. SydneyhSmith commented on Dec 1, 2020

    @SydneyhSmith
    Collaborator

    The bottom one is correct Rob Moore (MakerX) (@robdmoore) it is a hidden setting

  7. robdmoore commented on Dec 2, 2020

    @robdmoore

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

  8. robdmoore commented on Dec 4, 2020

    @robdmoore

    It started happening again, here's the logs:

    logs.txt

  9. 1 remaining item

  10. SydneyhSmith commented on Dec 8, 2020

    @SydneyhSmith
    Collaborator

    Thanks Rob Moore (MakerX) (@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.

  11. added this to the milestone on Dec 8, 2020
  12. removed this from the milestone on Jan 26, 2021
  13. efie45 commented on Mar 15, 2021

    @efie45

    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

  14. efie45 commented on Apr 13, 2021

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

  15. andyleejordan commented on Apr 23, 2021

    @andyleejordan
    Member

    Ethan Fie (@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?

  16. madrum commented on Apr 25, 2022

    @madrum

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

  17. andyleejordan commented on Apr 25, 2022

    @andyleejordan
    Member
  18. madrum commented on Apr 27, 2022

    @madrum
  19. nathan-chappell commented on Mar 2, 2023

    @nathan-chappell

    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).

    Andy Jordan (@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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions