PowerShell / PSReadLine

A bash inspired readline implementation for PowerShell
BSD 2-Clause "Simplified" License
3.74k stars 295 forks source link

Tab Completion - Opps, something went wrong. #902

Closed ericohlin closed 4 years ago

ericohlin commented 5 years ago

Environment data

PS version: 5.1.14393.2828 PSReadline version: 1.2 os: 10.0.14393.0 (rs1_release.160715-1616) PS file version: 10.0.14393.0 (rs1_release.160715-1616)

Thank you! :)

Steps to reproduce or exception report

  1. Open a powershell.exe
  2. Type the following line Get-ADUser eohlin -Properties * | Format-Table -Property @{Name="MemberOf";Expression={$_.memberof};}
  3. Don't hit enter yet...
  4. Place the cursor on the very last curly bracket and hit tab
  5. The following error is produced.
  6. PS C:\Temp> Get-ADUser eohlin -Properties * | Format-Table -Property @{Name="MemberOf";Expression={$_.memberof};} Oops, something went wrong. Please report this bug with the details below. Report on GitHub: https://github.com/lzybkr/PSReadLine/issues/new

    Last 200 Keys: LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow UpArrow LeftArrow L eftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow Ctrl+LeftArrow Ctrl+LeftArrow Ctrl+LeftArrow Ctrl+LeftArrow R ightArrow RightArrow RightArrow RightArrow RightArrow RightArrow End LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow Ctrl+LeftArrow LeftArrow Ctrl+LeftArrow LeftArrow LeftArrow LeftArrow L eftArrow LeftArrow Ctrl+LeftArrow LeftArrow Ctrl+LeftArrow LeftArrow Ctrl+LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow Ctrl+LeftArrow RightArrow RightArrow RightArrow RightArrow RightArrow RightArrow RightArrow Righ tArrow RightArrow RightArrow RightArrow RightArrow RightArrow RightArrow RightArrow RightArrow RightArrow RightArrow RightArrow Ctrl+RightArrow RightArrow RightArrow Ctrl+RightArrow RightArrow RightArrow Ctrl+RightArrow RightArrow RightArrow Ctrl+RightArrow RightArrow RightArrow Ctrl+RightArrow RightArrow Ctrl+RightArrow RightArrow RightArrow RightArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow Ctrl +LeftArrow LeftArrow Ctrl+LeftArrow LeftArrow Ctrl+LeftArrow LeftArrow Ctrl+LeftArrow LeftArrow Ctrl+LeftArrow LeftArrow Ctrl+LeftArrow LeftArrow Ctrl+LeftArrow LeftArrow LeftArrow LeftArrow RightArrow LeftArrow LeftArro w LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow Backspace Backspace Backspace Backspace t a Tab Enter UpArrow LeftArrow LeftArrow RightArrow Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Tab ; Tab

Exception:

System.ArgumentException: 'start' cannot be less than zero or greater than the length of the buffer
Parameter name: start
   at Microsoft.PowerShell.PSConsoleReadLine.Replace(Int32 start, Int32 length, String replacement, Action`2 instigator, Object instigatorArg)
   at Microsoft.PowerShell.PSConsoleReadLine.DoReplacementForCompletion(CompletionResult completionResult, CommandCompletion completions)
   at Microsoft.PowerShell.PSConsoleReadLine.Complete(Boolean forward)
   at Microsoft.PowerShell.PSConsoleReadLine.ProcessOneKey(ConsoleKeyInfo key, Dictionary`2 dispatchTable, Boolean ignoreIfNoAction, Object arg)
   at Microsoft.PowerShell.PSConsoleReadLine.InputLoop()
SteveL-MSFT commented 5 years ago

Doesn't repro for me with 2.0.0-beta4

lzybkr commented 5 years ago

@SteveL-MSFT - This was reported against 1.2 and sounds familiar, so I'm guessing it was fixed.

smrtRock commented 5 years ago

Still an issue

msftrncs commented 5 years ago

I cannot reproduce this, so there must be something more that might be needed. A confirmation of the placement of the cursor with a snapshot could be helpful. Also, are you on the same version of Windows? I am testing in Windows 10 1903 build 18362.145.

Also, what did you expect to happen? What item was supposed to come up for completion? At the position, I would expect no completion, and that is what I get.

ericohlin commented 5 years ago

I can reproduce this issue on Windows Server 2016, Powershell Version = 5.1.14393.3053

However, I cannot reproduce this error on Windows Server 2019, Powershell Version = 5.1.17763.592

I'm not sure if pursuing this is worth it since it appears to be fixed in newer issues. Just in case though, to reproduce place the cursor on this space and hit Tab.

image

I would expect nothing to happen, yet it errors and directs users to report the issue. image

There are other ways to replicate this error, attempting to tab at certain locations, this is just one example.

Thanks! Eric O

msftrncs commented 5 years ago

The issue is probably dependent on the version of PowerShell, or the version of the AD module, and if a newer version of PSReadLine still produces the error, there is probably something to fix, even though it may be bad results coming from PowerShell's completer logic.

What happens if you press CTRL-SPACE at that point? You will probably still get the error, but just before that, it should build a menu with the possible completions, unless only 1 possible completion is being returned.

I tried this on 5.1.14409.1018 and could not reproduce it. I have some more machines to try, they'll probably be the same version, though.

ericohlin commented 5 years ago

Hey @msftrncs ,

I'm verifying you read my comment above correctly. However, I cannot reproduce this error on Windows Server 2019, Powershell Version = 5.1.17763.592 . There is definitely some variance in results between PSVersions.

Pressing CTRL-SPACE produces the same error.

We might be able to rule out the AD Module as I can replicate the issue using another cmdlet.

Get-ChildItem C:\ConfigMgrAdminUISetup.log | Format-Table -Property @{Name="Test";Expression={$_.FullName};}

image

Format-Table lives in Microsoft.PowerShell.Utility and this version remains the same between my Windows Server 2016 and 2019 test. Version = 3.1.0.0

Thanks!

msftrncs commented 5 years ago

I am thinking its looking at possibly completions for hashtable elements, and it may have returned a null result (not an empty result set), because there is no real completions for hashtable properties during a hashtable declaration, that was probably fixed between build 14393 and 14409.

Reviewing the code in the call stack, I am pretty sure that the older version of PowerShell is incorrectly returning an invalid result set, which then causes PSReadLine to deliberately throw an exception in 'Replace'. The value passed as 'Start' I believe comes from PowerShell, so nothing PSReadLine can do about it.

lzybkr commented 5 years ago

PSReadLine does need to gracefully handle buggy completion results.

This sounds like something I fixed, maybe in 2.0.0-beta1 or beta2.

SteveL-MSFT commented 5 years ago

For those repro'ing this, please confirm you are using latest beta of PSReadLine 2.0.0 as a number of these issues have been fixed

msftrncs commented 5 years ago

@lzybkr, looks like you are referring to #534.

daxian-dbw commented 4 years ago

It looks to me this issue has been fixed in PSReadLine 2.0.0, so I will close it for now. Please try out the 2.0.0-rc1 from the PowerShell Gallery.