PowerShell / PowerShellEditorServices

A common platform for PowerShell development support in any editor or application!
MIT License
623 stars 214 forks source link

RequestRefactor hotkey throws an `InternalError` when calling `$CommandSuite.RequestRefactor($PSCmdlet)` #1959

Closed ninmonkey closed 1 year ago

ninmonkey commented 1 year ago

Theorized Causes?

It seems to always be about a cancellation, and the UI was flickering back and forth. Could the new quick fix feature be fighting for control with the PSES?

Prerequisites

Steps to reproduce

Refactor is crashing.

As far as I can tell I can't get more exception information (using powershell)

Location of the exception:

Documents\PowerShell\Modules\EditorServicesCommandSuite\1.0.0\EditorServicesCommandSuite.psm1

$null = $CommandSuite.RequestRefactor($PSCmdlet).
    ConfigureAwait($true).
    GetAwaiter().
    GetResult()

single exception

using a key with nothing bound to it other than this

   {        
        "key": "ctrl+k",
        "when": "editorLangId == 'powershell'",
        "command": "PowerShell.InvokeRegisteredEditorCommand",
        "args": {
            "commandName": "Invoke-DocumentRefactor"
        },
    },
    {
        "key": "ctrl+shift+s",
        "command": "PowerShell.InvokeRegisteredEditorCommand",
        "args": {
            "commandName": "ConvertTo-SplatExpression"
        },
        "when": "editorLangId == 'powershell'"
    },

to reproduce

import-module EditorServicesCommandSuite -Force
Import-CommandSuite

Expected behavior

Show the refactor menu

Actual behavior

The SplatExpression hotkey works
The Refactor one throws.

Error details

Environment data

Name                           Value
----                           -----
PSVersion                      7.3.0
PSEdition                      Core
GitCommitId                    7.3.0
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     1.0.0      beta4      EditorServicesCommandSuite          {Get-CommandSuiteSetting, New-CommandSuiteSettingF…
Script     0.2.0                 PowerShellEditorServices.Commands   {Clear-Host, ConvertFrom-ScriptExtent, ConvertTo-S…
Binary     0.2.0                 PowerShellEditorServices.VSCode     {Close-VSCodeHtmlContentView, New-VSCodeHtmlConten…

Version

ms-vscode.powershell@2022.11.0

Visuals

image

SeeminglyScience commented 1 year ago

Thanks Jake! Lookin' into it

SeeminglyScience commented 1 year ago

Non-ESCS repro:

$eoeType = [Microsoft.PowerShell.EditorServices.Extensions.EditorObjectExtensions, Microsoft.PowerShell.EditorServices]
$pcdType = [Microsoft.PowerShell.EditorServices.Extensions.Services.PromptChoiceDetails, Microsoft.PowerShell.EditorServices]

$task = $eoeType::GetExtensionServiceProvider($psEditor).EditorUI.PromptSelectionAsync(
    'Example message',
    @(
        $pcdType::new('Label 1', 'Greyed out bit I think'),
        $pcdType::new('Label 2', 'woooot')
    ) -as $pcdType.MakeArrayType())

while (-not $task.AsyncWaitHandle.WaitOne(200)) { }
$result = $task.GetAwaiter().GetResult()
$result
ninmonkey commented 1 year ago

I get the same behavior with v2022.11.1 of ms-vscode.powershell-preview .


[Warn  - 10:13:08 PM] OmniSharp.Extensions.LanguageServer.Server.LspServerOutputFilter: Tried to send request or notification before initialization was completed and will be sent later OutgoingNotification { Method = powerShell/extensionCommandAdded, Params = Microsoft.PowerShell.EditorServices.Extensions.ExtensionCommandAddedNotification, TraceParent = , TraceState =  } | @Request='OutgoingNotification { Method = powerShell/extensionCommandAdded, Params = Microsoft.PowerShell.EditorServices.Extensions.ExtensionCommandAddedNotification, TraceParent = , TraceState =  }'
# ... repeats 10 times

[Warn  - 10:13:14 PM] OmniSharp.Extensions.LanguageServer.Server.LspServerOutputFilter: Tried to send request or notification before initialization was completed and will be sent later OutgoingNotification { Method = powerShell/extensionCommandUpdated, Params = Microsoft.PowerShell.EditorServices.Extensions.ExtensionCommandUpdatedNotification, TraceParent = , TraceState =  } | @Request='OutgoingNotification { Method = powerShell/extensionCommandUpdated, Params = Microsoft.PowerShell.EditorServices.Extensions.ExtensionCommandUpdatedNotification, TraceParent = , TraceState =  }'
[Warn  - 10:13:14 PM] OmniSharp.Extensions.LanguageServer.Server.Configuration.DidChangeConfigurationProvider: No ConfigurationItems have been defined, configuration won't surface any configuration from the client! | 
[Warn  - 10:21:02 PM] Microsoft.PowerShell.EditorServices.Handlers.PsesCompletionHandler: Exception occurred while running handling completion request - System.Threading.Tasks.TaskCanceledException: A task was canceled.
   at Microsoft.PowerShell.EditorServices.Services.Symbols.AstOperations.GetCompletionsAsync(Ast scriptAst, Token[] currentTokens, Int32 fileOffset, IInternalPowerShellExecutionService executionService, ILogger logger, CancellationToken cancellationToken) in D:\a\_work\1\s\src\PowerShellEditorServices\Services\Symbols\Vistors\AstOperations.cs:line 86
   at Microsoft.PowerShell.EditorServices.Handlers.PsesCompletionHandler.GetCompletionsInFileAsync(ScriptFile scriptFile, Int32 lineNumber, Int32 columnNumber, CancellationToken cancellationToken) in D:\a\_work\1\s\src\PowerShellEditorServices\Services\TextDocument\Handlers\CompletionHandler.cs:line 205
   at Microsoft.PowerShell.EditorServices.Handlers.PsesCompletionHandler.Handle(CompletionParams request, CancellationToken cancellationToken) in D:\a\_work\1\s\src\PowerShellEditorServices\Services\TextDocument\Handlers\CompletionHandler.cs:line 74 | 
[Warn  - 10:21:03 PM] Microsoft.PowerShell.EditorServices.Handlers.PsesCompletionHandler: Exception occurred while running handling completion request - System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.PowerShell.EditorServices.Services.Symbols.AstOperations.GetCompletionsAsync(Ast scriptAst, Token[] currentTokens, Int32 fileOffset, IInternalPowerShellExecutionService executionService, ILogger logger, CancellationToken cancellationToken) in D:\a\_work\1\s\src\PowerShellEditorServices\Services\Symbols\Vistors\AstOperations.cs:line 131
   at Microsoft.PowerShell.EditorServices.Handlers.PsesCompletionHandler.GetCompletionsInFileAsync(ScriptFile scriptFile, Int32 lineNumber, Int32 columnNumber, CancellationToken cancellationToken) in D:\a\_work\1\s\src\PowerShellEditorServices\Services\TextDocument\Handlers\CompletionHandler.cs:line 205
   at Microsoft.PowerShell.EditorServices.Handlers.PsesCompletionHandler.Handle(CompletionParams request, CancellationToken cancellationToken) in D:\a\_work\1\s\src\PowerShellEditorServices\Services\TextDocument\Handlers\CompletionHandler.cs:line 74 | 
[Warn  - 10:21:34 PM] Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost: Runtime exception occurred while executing command:

System.Management.Automation.RuntimeException: Exception calling "RequestRefactor" with "1" argument(s): "Internal error. "
 ---> System.Management.Automation.MethodInvocationException: Exception calling "RequestRefactor" with "1" argument(s): "Internal error. "
 ---> OmniSharp.Extensions.JsonRpc.Server.InternalErrorException: Internal error. 
   at OmniSharp.Extensions.JsonRpc.ResponseRouter.ResponseRouterReturnsImpl.Returning[TResponse](CancellationToken cancellationToken)
   at Microsoft.PowerShell.EditorServices.Extensions.Services.EditorUIService.PromptSelectionAsync(String message, IReadOnlyList`1 choices, Int32 defaultChoiceIndex) in D:\a\_work\1\s\src\PowerShellEditorServices\Extensions\Api\EditorUIService.cs:line 163
   at EditorServicesCommandSuite.EditorServices.UIService.ShowChoicePromptAsync[TItem](String caption, String message, TItem[] items, Func`2 labelSelector, Func`2 helpMessageSelector) in D:\a\1\s\src\EditorServicesCommandSuite.EditorServices\UIService.cs:line 97
   at EditorServicesCommandSuite.EditorServices.UIService.ShowChoicePromptAsync[TItem](String caption, String message, TItem[] items, Func`2 labelSelector) in D:\a\1\s\src\EditorServicesCommandSuite.EditorServices\UIService.cs:line 77
   at EditorServicesCommandSuite.Internal.CommandSuite.RequestRefactor(PSCmdlet cmdlet, DocumentContextBase request) in D:\a\1\s\src\EditorServicesCommandSuite\Internal\CommandSuite.cs:line 163
   at EditorServicesCommandSuite.Internal.CommandSuite.<>c__DisplayClass30_0.<<RequestRefactor>b__0>d.MoveNext() in D:\a\1\s\src\EditorServicesCommandSuite\Internal\CommandSuite.cs:line 127
--- End of stack trace from previous location ---
   at EditorServicesCommandSuite.Utility.ThreadController.GiveControl(Task requestTask, CancellationToken cancellationToken) in D:\a\1\s\src\EditorServicesCommandSuite\Utility\ThreadController.cs:line 55
   at EditorServicesCommandSuite.Internal.CommandSuite.RequestRefactor(PSCmdlet cmdlet) in D:\a\1\s\src\EditorServicesCommandSuite\Internal\CommandSuite.cs:line 136
   at CallSite.Target(Closure, CallSite, Object, PSScriptCmdlet)
   --- End of inner exception stack trace ---
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   --- End of inner exception stack trace ---
   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke)
   at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke[T](IEnumerable input, PSInvocationSettings settings)
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility.PowerShellExtensions.InvokeAndClear[TResult](PowerShell pwsh, PSInvocationSettings invocationSettings) in D:\a\_work\1\s\src\PowerShellEditorServices\Services\PowerShell\Utility\PowerShellExtensions.cs:line 82
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousPowerShellTask`1.ExecuteNormally(CancellationToken cancellationToken) in D:\a\_work\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 187 | 
[Warn  - 10:23:22 PM] Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost: Runtime exception occurred while executing command:

System.Management.Automation.RuntimeException: Exception calling "RequestRefactor" with "1" argument(s): "Internal error. "
 ---> System.Management.Automation.MethodInvocationException: Exception calling "RequestRefactor" with "1" argument(s): "Internal error. "
 ---> OmniSharp.Extensions.JsonRpc.Server.InternalErrorException: Internal error. 
   at OmniSharp.Extensions.JsonRpc.ResponseRouter.ResponseRouterReturnsImpl.Returning[TResponse](CancellationToken cancellationToken)
   at Microsoft.PowerShell.EditorServices.Extensions.Services.EditorUIService.PromptSelectionAsync(String message, IReadOnlyList`1 choices, Int32 defaultChoiceIndex) in D:\a\_work\1\s\src\PowerShellEditorServices\Extensions\Api\EditorUIService.cs:line 163
   at EditorServicesCommandSuite.EditorServices.UIService.ShowChoicePromptAsync[TItem](String caption, String message, TItem[] items, Func`2 labelSelector, Func`2 helpMessageSelector) in D:\a\1\s\src\EditorServicesCommandSuite.EditorServices\UIService.cs:line 97
   at EditorServicesCommandSuite.EditorServices.UIService.ShowChoicePromptAsync[TItem](String caption, String message, TItem[] items, Func`2 labelSelector) in D:\a\1\s\src\EditorServicesCommandSuite.EditorServices\UIService.cs:line 77
   at EditorServicesCommandSuite.Internal.CommandSuite.RequestRefactor(PSCmdlet cmdlet, DocumentContextBase request) in D:\a\1\s\src\EditorServicesCommandSuite\Internal\CommandSuite.cs:line 163
   at EditorServicesCommandSuite.Internal.CommandSuite.<>c__DisplayClass30_0.<<RequestRefactor>b__0>d.MoveNext() in D:\a\1\s\src\EditorServicesCommandSuite\Internal\CommandSuite.cs:line 127
--- End of stack trace from previous location ---
   at EditorServicesCommandSuite.Utility.ThreadController.GiveControl(Task requestTask, CancellationToken cancellationToken) in D:\a\1\s\src\EditorServicesCommandSuite\Utility\ThreadController.cs:line 55
   at EditorServicesCommandSuite.Internal.CommandSuite.RequestRefactor(PSCmdlet cmdlet) in D:\a\1\s\src\EditorServicesCommandSuite\Internal\CommandSuite.cs:line 136
   at CallSite.Target(Closure, CallSite, Object, PSScriptCmdlet)
   --- End of inner exception stack trace ---
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   --- End of inner exception stack trace ---
   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke)
   at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke[T](IEnumerable input, PSInvocationSettings settings)
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility.PowerShellExtensions.InvokeAndClear[TResult](PowerShell pwsh, PSInvocationSettings invocationSettings) in D:\a\_work\1\s\src\PowerShellEditorServices\Services\PowerShell\Utility\PowerShellExtensions.cs:line 82
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution.SynchronousPowerShellTask`1.ExecuteNormally(CancellationToken cancellationToken) in D:\a\_work\1\s\src\PowerShellEditorServices\Services\PowerShell\Execution\SynchronousPowerShellTask.cs:line 187 | 
SeeminglyScience commented 1 year ago

Nah nothing like that, it's just the ShowChoicePromptAsync public API that is broken. Still workin' on it.