kelleyma49 / PSFzf

A PowerShell wrapper around the fuzzy finder fzf
MIT License
787 stars 34 forks source link

Exception in custom key handler when kubectl completion finds no results #95

Closed Aankhen closed 2 years ago

Aankhen commented 2 years ago

The pre-release versions of kubectl v1.23.0 include PowerShell completion, which is provided under the hood by spf13/cobra. This works beautifully most of the time in conjunction with Set-PSReadLineKeyHandler -Key Tab -ScriptBlock { Invoke-FzfTabCompletion }:

Pressing Tab retrieves and shows the list of resources to complete

However, there seems to be an issue when there are no completions available:

Pressing Tab results in an exception message when no completions are available

(Note that pressing Ctrl + C has no effect. I have to press Enter to run the command before I can do anything else.)

I’ll reproduce it in text for legibility, though of course without the formatting:

❯❯ kubectl get pods fAn exception occurred in custom key handler, see $error for more information: Exception calling "CompleteInput" with "3" argument(s): "Cannot process argument because the value of argument "completionText" is null. Change the value of argument "completionText" to a non-null value."
~
❯❯ kubectl get pods f
Error from server (NotFound): pods "f" not found
~ took 1s157ms
❯❯ kubectl asdasdAn exception occurred in custom key handler, see $error for more information: Exception calling "CompleteInput" with "3" argument(s): "Cannot process argument because the value of argument "completionText" is null. Change the value of argument "completionText" to a non-null value."
~ took 1s157ms
❯❯ kubectl asdasd
E1128 12:56:48.823585   19468 run.go:120] "command failed" err="unknown command \"asdasd\" for \"kubectl\""

Now, if I run the command that the application’s completion script runs under the hood, this is what I see:

❯❯ k __complete asdasd
:4
Completion ended with directive: ShellCompDirectiveNoFileComp

Which is the result of this code:

    if (($Directive -band $ShellCompDirectiveNoFileComp) -ne 0 ) {
        __kubectl_debug "ShellCompDirectiveNoFileComp is called"

        if ($Values.Length -eq 0) {
            # Just print an empty string here so the
            # shell does not start to complete paths.
            # We cannot use CompletionResult here because
            # it does not accept an empty string as argument.
            ""
            return
        }
    }

As far as I can tell, the completion script is doing the right thing. Without PsFzf, pressing Tab does nothing if there are no completions, as you’d expect, and lets you continue your input. With PsFzf, pressing Tab with no completions available causes an error and prevents you from using Ctrl + C to cancel the input.

I’m running PsFzf v2.3.1-alpha (which I upgraded to from v2.2.9 in case it fixed this bug) with PowerShell v7.2.0 under Windows 10.0.19043 (64-bit).

kelleyma49 commented 2 years ago

Thank you for the bug report.

Can you print out the value of $error? I'd like to see the stack traceback where CompleteInput is being called.

Aankhen commented 2 years ago

Sure, here you go:

C:\Users\A> k get pod asdasdAn exception occurred in custom key handler, see $error for more information: Exception call
C:\Users\A> $error
MethodInvocationException: Exception calling "CompleteInput" with "3" argument(s): "Cannot process argument because the value of argument
"completionText" is null. Change the value of argument "completionText" to a non-null value."

MethodInvocationException: Exception calling "CompleteInput" with "3" argument(s): "Cannot process argument because the value of argument
"completionText" is null. Change the value of argument "completionText" to a non-null value."
kelleyma49 commented 2 years ago

Apologies it took so long to look at this.

I can recreate the issue on my local machine. Note that the same error is thrown even if PSFzf isn't used, but it's likely consumed so you don't see it from the command line. I'll try and make PSFzf more robust in cases like this.

kelleyma49 commented 2 years ago

This change is available in https://www.powershellgallery.com/packages/PSFzf/2.5.4-alpha