Skip to content

PowerShell completion does not always honour ShellCompDirectiveNoFileComp  #1362

@marckhouzam

Description

@marckhouzam

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions