vscode-powershell icon indicating copy to clipboard operation
vscode-powershell copied to clipboard

not using the Legacy Psreadline experience leads to "custom key handler"-error

Open DEberhardt opened this issue 3 years ago • 17 comments

Type: Bug

Install PSReadline v2.2.6 Use VsCode Use PowerShell or PowerShellPreview Use PowerShell Extension terminal session that is opened to you

[2022-10-02 11:09:22]❯ get-moduleAn exception occurred in custom key handler, see $error for more information: Object reference not set to an instance of an object.

No command I enter (incl. $Error[0].Exception) leads anywhere.

If I then disable the live PSreadline experience (i.e. switch on the "legacy PSreadline experience" in the settings and restart the terminal session, it works fine.

Problem #1) - I can't close the terminal session as it de-facto disables the extensions Problem #2) - I can't use it in legacy mode as most of the PsReadLine goodies do not work anymore Problem #3) - I can't use it in normal operation mode

What can be done here? Anything else I can do to troubleshoot this?

Extension version: 2022.9.2 VS Code version: Code 1.71.2 (74b1f979648cc44d385a2286793c226e611f59e7, 2022-09-14T21:03:37.738Z) OS version: Windows_NT x64 10.0.22623 Modes: Sandboxed: No

System Info
Item Value
CPUs Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz (8 x 3600)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: disabled_off
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_renderer: enabled_on
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: disabled_off
Load (avg) undefined
Memory (System) 31.93GB (19.83GB free)
Process Argv --crash-reporter-id efc3d77d-4b9d-44d6-894f-bd5aee3260eb
Screen Reader no
VM 0%
A/B Experiments
vsliv368cf:30146710
vsreu685:30147344
python383cf:30185419
vspor879:30202332
vspor708:30202333
vspor363:30204092
vslsvsres303:30308271
pythonvspyl392:30443607
vserr242cf:30382550
pythontb:30283811
vsjup518:30340749
pythonptprofiler:30281270
vshan820:30294714
vstes263cf:30335440
vscorecescf:30445987
pythondataviewer:30285071
vscod805:30301674
binariesv615:30325510
bridge0708:30335490
bridge0723:30353136
cmake_vspar411:30557514
vsaa593:30376534
pythonvs932:30410667
cppdebug:30492333
vsclangdc:30486549
c4g48928:30535728
dsvsc012:30540252
azure-dev_surveyone:30548225
2144e591:30553903
i5d37513:30580093
pyindex848:30577860
fc301958:30573243

DEberhardt avatar Oct 02 '22 10:10 DEberhardt

PS: The normal PowerShell terminal works fine, it is this one here btw: image

DEberhardt avatar Oct 02 '22 10:10 DEberhardt

Don't know if this helps:

[Warn  - 10:48:12] 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 =  }'
[Warn  - 10:48:22] OmniSharp.Extensions.LanguageServer.Server.Configuration.DidChangeConfigurationProvider: No ConfigurationItems have been defined, configuration won't surface any configuration from the client! | 
[Warn  - 11:02:28] Microsoft.PowerShell.EditorServices.Handlers.PsesCodeActionHandler: textDocument/codeAction skipping diagnostic with empty Code field: PowerShell The string is missing the terminator: '. | 
[Warn  - 11:02:30] Microsoft.PowerShell.EditorServices.Handlers.PsesCodeActionHandler: textDocument/codeAction skipping diagnostic with empty Code field: PowerShell The string is missing the terminator: '. | 
[Error - 11:02:36] Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost: An error occurred while running the REPL loop - System.ArgumentOutOfRangeException: The value must be greater than or equal to zero and less than the console's buffer size in that dimension.
Parameter name: top
Actual value was 23.
   at System.Console.SetCursorPosition(Int32 left, Int32 top)
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Console.LegacyReadLine.MoveCursorToIndex(Int32 promptStartCol, Int32 promptStartRow, Int32 consoleWidth, Int32 newCursorIndex)
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Console.LegacyReadLine.InsertInput(StringBuilder inputLine, Int32 promptStartCol, Int32 promptStartRow, String insertedInput, Int32 cursorIndex, Int32 insertIndex, Int32 replaceLength, Int32 finalCursorIndex)
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Console.LegacyReadLine.ReadLine(CancellationToken cancellationToken)
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost.InvokeReadLine(CancellationToken cancellationToken)
   at Microsoft.PowerShell.EditorServices.Services.PowerShell.Host.PsesInternalHost.DoOneRepl(CancellationToken cancellationToken) | 
[Error - 11:08:22] Connection to PowerShell Editor Services (the Extension Terminal) was closed. See below prompt to restart!

DEberhardt avatar Oct 02 '22 10:10 DEberhardt

What custom key handler are you using? I don't expect the legacy readline to be a stable experience, it's just a fallback for debugging and is not covered in tests.

andyleejordan avatar Oct 03 '22 16:10 andyleejordan

I am at a loss, how would I find out? (I am not aware changing the key handler...)

DEberhardt avatar Oct 04 '22 19:10 DEberhardt

Mm, try running Get-PSReadLineKeyHandler.

andyleejordan avatar Oct 06 '22 19:10 andyleejordan

d'oh! Could have guessed this one... Thank you.

User defined functions
======================

Key   Function                Description
---   --------                -----------
Enter OhMyPoshEnterKeyHandler User defined action

Just updating to v12.0.1 to see whether this fixes things - This did appear after the most recent update to v11.x

DEberhardt avatar Oct 07 '22 08:10 DEberhardt

ok. no change after update. Shall I report this to Jan as well? (I have no issues with this setup on Windows Terminal, just in VsCode for some reason and only in the terminal instance created by PowerShell Extension...

Edit: the Normal PowerShell Terminal that starts with VsCode works fine as well.

DEberhardt avatar Oct 07 '22 09:10 DEberhardt

Let's try without OhMyPoshEnterKeyHandler and see if that's the problem! Can remove Posh from your profile setup?

andyleejordan avatar Oct 07 '22 16:10 andyleejordan

Yes, this works - not calling this line restores functionality

oh-my-posh init pwsh --config $UserPsScripts\ParagnosterPlus2.omp.json | Invoke-Expression

DEberhardt avatar Oct 08 '22 11:10 DEberhardt

Ok, seems like a bug with the oh-my-posh key handler, I would recommend reaching out to those developers to get it fixed!

andyleejordan avatar Oct 10 '22 19:10 andyleejordan

This issue has been marked as external. It has been automatically closed for housekeeping purposes.

ghost avatar Oct 10 '22 20:10 ghost

Sorry, did not mean to allow the bot to close this, I'm actively investigating it. I was able to repro, though only with Windows PowerShell.

andyleejordan avatar Oct 10 '22 22:10 andyleejordan

@DEberhardt Please let me know when you get a change to try PowerShell 7 AKA PowerShell Core and if it repros there too. I can repro but only with PowerShell 5.1 AKA Windows PowerShell, and what happens is weird, I'm getting this exception:

Exception has occurred: CLR/System.ArgumentOutOfRangeException
Exception thrown: 'System.ArgumentOutOfRangeException' in mscorlib.dll: 'The value must be greater than or equal to zero and less than the console's buffer size in that dimension.'
   at System.Console.SetCursorPosition(Int32 left, Int32 top)

With this stacktrace:

mscorlib.dll!System.Console.SetCursorPosition(int left, int top) (Unknown Source:0)
Microsoft.PowerShell.PSReadLine2.dll!Microsoft.PowerShell.PSConsoleReadLine.InvokePrompt(System.ConsoleKeyInfo? key, object arg) (Unknown Source:0)
[Lightweight Function] (Unknown Source:0)
System.Management.Automation.dll!System.Management.Automation.Interpreter.DynamicInstruction<System.Type, object>.Run(System.Management.Automation.Interpreter.InterpretedFrame frame) (Unknown Source:0)
System.Management.Automation.dll!System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(System.Management.Automation.Interpreter.InterpretedFrame frame) (Unknown Source:0)
System.Management.Automation.dll!System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(System.Management.Automation.Interpreter.InterpretedFrame frame) (Unknown Source:0)
System.Management.Automation.dll!System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(System.Management.Automation.Interpreter.InterpretedFrame frame) (Unknown Source:0)
System.Management.Automation.dll!System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(System.Management.Automation.Interpreter.InterpretedFrame frame) (Unknown Source:0)
System.Management.Automation.dll!System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(System.Management.Automation.Interpreter.InterpretedFrame frame) (Unknown Source:0)
System.Management.Automation.dll!System.Management.Automation.Interpreter.Interpreter.Run(System.Management.Automation.Interpreter.InterpretedFrame frame) (Unknown Source:0)
System.Management.Automation.dll!System.Management.Automation.Interpreter.LightLambda.RunVoid1<System.Management.Automation.Language.FunctionContext>(System.Management.Automation.Language.FunctionContext arg0) (Unknown Source:0)
System.Management.Automation.dll!System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(System.Management.Automation.ScriptBlockClauseToInvoke clauseToInvoke, bool createLocalScope, System.Collections.Generic.Dictionary<string, System.Management.Automation.ScriptBlock> functionsToDefine, System.Collections.Generic.List<System.Management.Automation.PSVariable> variablesToDefine, System.Management.Automation.ScriptBlock.ErrorHandlingBehavior errorHandlingBehavior, object dollarUnder, object input, object scriptThis, System.Management.Automation.Internal.Pipe outputPipe, System.Management.Automation.InvocationInfo invocationInfo, object[] args) (Unknown Source:0)
System.Management.Automation.dll!System.Management.Automation.ScriptBlock.InvokeWithPipe.AnonymousMethod__0() (Unknown Source:0)
System.Management.Automation.dll!System.Management.Automation.Runspaces.RunspaceBase.RunActionIfNoRunningPipelinesWithThreadCheck(System.Action action) (Unknown Source:0)
System.Management.Automation.dll!System.Management.Automation.ScriptBlock.InvokeWithPipe(bool useLocalScope, System.Management.Automation.ScriptBlock.ErrorHandlingBehavior errorHandlingBehavior, object dollarUnder, object input, object scriptThis, System.Management.Automation.Internal.Pipe outputPipe, System.Management.Automation.InvocationInfo invocationInfo, bool propagateAllExceptionsToTop, System.Collections.Generic.List<System.Management.Automation.PSVariable> variablesToDefine, System.Collections.Generic.Dictionary<string, System.Management.Automation.ScriptBlock> functionsToDefine, object[] args) (Unknown Source:0)
System.Management.Automation.dll!System.Management.Automation.ScriptBlock.DoInvoke(object dollarUnder, object input, object[] args) (Unknown Source:0)
Microsoft.PowerShell.PSReadLine2.dll!Microsoft.PowerShell.PSConsoleReadLine.SetKeyHandler.__HandlerWrapper|0(System.ConsoleKeyInfo? k, object arg) (Unknown Source:0)
Microsoft.PowerShell.PSReadLine2.dll!Microsoft.PowerShell.PSConsoleReadLine.ProcessOneKey.AnonymousMethod__0() (Unknown Source:0)
Microsoft.PowerShell.PSReadLine2.dll!Microsoft.PowerShell.PSConsoleReadLine.CallPossibleExternalApplication.AnonymousMethod__0() (Unknown Source:0)
Microsoft.PowerShell.PSReadLine2.dll!PlatformWindows.CallPossibleExternalApplication<System.__Canon>(System.Func<System.__Canon> func) (Unknown Source:0)
Microsoft.PowerShell.PSReadLine2.dll!Microsoft.PowerShell.PSConsoleReadLine.CallPossibleExternalApplication<object>(System.Func<object> func) (Unknown Source:0)
Microsoft.PowerShell.PSReadLine2.dll!Microsoft.PowerShell.PSConsoleReadLine.InputLoop() (Unknown Source:0)
Microsoft.PowerShell.PSReadLine2.dll!Microsoft.PowerShell.PSConsoleReadLine.ReadLine(System.Management.Automation.Runspaces.Runspace runspace, System.Management.Automation.EngineIntrinsics engineIntrinsics, System.Threading.CancellationToken cancellationToken, bool? lastRunStatus) (Unknown Source:0)
Microsoft.PowerShell.EditorServices.dll!Microsoft.PowerShell.EditorServices.Services.PowerShell.Console.PsrlReadLine.InvokePSReadLine(System.Threading.CancellationToken cancellationToken) Line 48 (c:\Users\andschwa\src\PowerShellEditorServices\src\PowerShellEditorServices\Services\PowerShell\Console\PsrlReadLine.cs:48)
...

andyleejordan avatar Oct 10 '22 22:10 andyleejordan

@DEberhardt Responding to https://github.com/JanDeDobbeleer/oh-my-posh/issues/2910#issuecomment-1273827732 here so we don't flood oh-my-posh.

Regarding your profile not loading with PowerShell 7, please note that PowerShell 5.1 and 7 load (some) different profile paths.

I would run $PROFILE | Get-Member -Type NoteProperty to see which paths are being loaded in each environment, and then try moving/copying your profile to one that loads in PowerShell 7 (in the VS Code host).

andyleejordan avatar Oct 10 '22 22:10 andyleejordan

Don't mind me, I have forgotten how profiles work. 😄 - Having never used PowerShell Core in VsCode (still working with the AzureAd module 😐 ), I had never created a Profile for VsCode, only for the normal terminal. All good on that front now.

With the Extension Terminal on PowerShell 7 Core (x64) I do not get an error

DEberhardt avatar Oct 11 '22 08:10 DEberhardt

Ok cool, thanks for confirming this is just a PowerShell 5.1 issue.

andyleejordan avatar Oct 11 '22 17:10 andyleejordan