Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PowerShell completion does not always honour ShellCompDirectiveNoFileComp #1362

Closed
marckhouzam opened this issue Mar 5, 2021 · 1 comment · Fixed by #1363
Closed

PowerShell completion does not always honour ShellCompDirectiveNoFileComp #1362

marckhouzam opened this issue Mar 5, 2021 · 1 comment · Fixed by #1363

Comments

@marckhouzam
Copy link
Collaborator

This is the same as part of #1248 (fish), as well as #1212 (zsh).

The powershell completion script disables file completion only if it receives no completions from the __complete command. Notice the last line of:

if (($Directive -band $ShellCompDirectiveNoFileComp) -ne 0 ) {
__%[1]s_debug "ShellCompDirectiveNoFileComp is called"
if ($Values.Length -eq 0) {

However, it is valid for ValidArgsFunction to return completions that don't apply to the current toComplete prefix. This means file completion should still be disabled if requested by the directive, but the script does not realize it as it still believes it received valid completions.

Say I have the following ValidArgsFunction for command nofilecomp:

ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    // We do not consider the toComplete prefix and always return a completion
    return []string{"why"}, cobra.ShellCompDirectiveNoFileComp
},

If I have a file called myfile and run in powershell:

$ ./testprog nofilecomp my<TAB>

I would expect not to get file completion, but I do, and myfile gets completed. This is because

$ ./testprog __complete nofilecomp my<ENTER>
why
:0
Completion ended with directive: ShellCompDirectiveNoFileComp

Notice that above, the script receives a completion even though it is not technically valid. This is allowed. But then, the script does not realize it must disable file completion.

/cc @Luap99

Luap99 added a commit to Luap99/cobra that referenced this issue Mar 5, 2021
Make sure to filter the returned completions before we check if
there are valid completions are left.

Fixes spf13#1362

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
Luap99 added a commit to Luap99/cobra that referenced this issue Mar 5, 2021
Make sure to filter the returned completions before we check if
there are valid completions left.

Fixes spf13#1362

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
@Luap99
Copy link
Contributor

Luap99 commented Mar 5, 2021

Opened #1363. A very simple fix.

jpmcb pushed a commit that referenced this issue May 3, 2021
Make sure to filter the returned completions before we check if
there are valid completions left.

Fixes #1362

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants