vscode-powershell: Corrupted text added during save, not during save without formatting

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

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 6
  • Comments: 15 (3 by maintainers)

Most upvoted comments

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