From f64bfa1e08c3199f3e740b5f96a9dd07b727c368 Mon Sep 17 00:00:00 2001 From: midchildan Date: Sun, 4 Oct 2020 10:25:07 +0900 Subject: [PATCH] Fix zsh completion not working on the first time in a shell session (#1237) The zsh completion script output by cobra is a stub completion function which replaces itself with the actual completion function. This technique enables cobra to define helper functions without splitting the completion script into multiple files. However, the current implementation forgets to call the actual completion function at the end of the stub function, meaning that completion won't work the first time it's invoked in a shell session. This commit is a fix for this problem. --- zsh_completions.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/zsh_completions.go b/zsh_completions.go index c25ce680..92a70394 100644 --- a/zsh_completions.go +++ b/zsh_completions.go @@ -229,6 +229,11 @@ _%[1]s() _describe "completions" completions $(echo $flagPrefix) fi } + +# don't run the completion function when being source-ed or eval-ed +if [ "$funcstack[1]" = "_%[1]s" ]; then + _%[1]s +fi `, name, compCmd, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs))