PowerShell / PowerShell/vscode-powershell

Corrupted text added during save, not during save without formatting

Open
#2,569 15 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Area-Code Formatting Issue-Bug
Dominant language
TypeScript
Stars
1.9k
Forks
547
PR merge metrics
No merged PRs in 30d

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue in VS Code with the PowerShell extension by comparing repeated Save with Save Without Formatting, using the captured logs as the starting point. Read ScriptFile.ValidatePosition/ApplyChange and TextDocumentHandler.Handle at the stack-trace locations; done means repeated saves no longer insert corrupted text or produce the reported range errors.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell, typescript, vscode
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.