Skip to content

zsh completion does not handle file completion properly #1212

@marckhouzam

Description

@marckhouzam

While working on #1211 I noticed another bug. The zsh completion script performs file completion only if it receives no completions. Notice the first line of:

cobra/zsh_completions.go

Lines 220 to 227 in 02a0d2f

elif [ ${compCount} -eq 0 ]; then
if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then
__%[1]s_debug "deactivating file completion"
else
# Perform file completion
__%[1]s_debug "activating file completion"
_arguments '*:filename:_files'" ${flagPrefix}"
fi

However, it is valid for ValidArgsFunction to return completions that don't apply to the current toComplete prefix. This means file completion should be performed, but the script does not realize it.

Say I have the following ValidArgsFunction for command withfile:

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.ShellCompDirectiveDefault
},

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

$ ./testprog withfile my<TAB>

I would expect to get file completion and get myfile as a completion, but I don't. This is because

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

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

I will post a PR to handle this better.

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