kubectx icon indicating copy to clipboard operation
kubectx copied to clipboard

_arguments:comparguments:325: can only be called from completion function

Open martinmaehlmann opened this issue 4 years ago • 6 comments

The zsh completion scripts make zsh throw an error.

_arguments:comparguments:325: can only be called from completion function

This is due to the fact, that both zsh completion scripts call _arguments outside a function.

Fix

Put the completion scripts into a function, as shown below:

kubectx.zsh

 #compdef kubectx kctx=kubectx

__kubectx() {
  local KUBECTX="${HOME}/.kube/kubectx"
  PREV=""

  local all_contexts="$(kubectl config get-contexts --output='name')"
  if [ -f "$KUBECTX" ]; then
      # show '-' only if there's a saved previous context
      local PREV=$(cat "${KUBECTX}")

      _arguments \
        "-d:*: :(${all_contexts})" \
        "(- *): :(- ${all_contexts})"
  else
      _arguments \
        "-d:*: :(${all_contexts})" \
        "(- *): :(${all_contexts})"
  fi
}

kubens.zsh

#compdef kubens kns=kubens

__kubens() {
  _arguments "1: :(- $(kubectl get namespaces -o=jsonpath='{range .items[*].metadata.name}{@}{"\n"}{end}'))"
}

martinmaehlmann avatar Jan 27 '21 09:01 martinmaehlmann

It doesn't a throw an error for me on my oh-my-zsh setup with zsh 5.8 (x86_64-apple-darwin20.0). Can we first try to figure out what causes the error?

ahmetb avatar Jan 27 '21 19:01 ahmetb

The error is thrown, when the autocompletions are loaded, e.g. when I start a new instance of my zsh (zsh 5.7.1 (x86_64-apple-darwin19.0)). Despite the error thrown, the completion itself does work.

The error thrown due to the fact that the _arguments are called outside a function. This is why I wrapped the completion scripts with an the functions __kubens() and __kubectx() silencing the error.

martinmaehlmann avatar Jan 28 '21 09:01 martinmaehlmann

I was seeing this issue until I realised that rather than sourcing the completions they needed to be put into a directory on zsh's fpath E.g. something like (untested)

# Create a folder ~/.zsh to put files zsh will load
mkdir "${HOME}/.zsh"
flamegraph --completions zsh > "${HOME}/.zsh/.flamegraph-completion.zsh"

# Add .zsh to your fpath when the zsh shell starts
echo 'fpath+=~/.zsh' >>"${HOME}/.zshrc"

thatmarkenglishguy avatar Jun 06 '22 11:06 thatmarkenglishguy

hey @martinmaehlmann @ahmetb @thatmarkenglishguy did you find a solution?

--

About my environment

MacOS Ventura 13.0.1 zsh 5.8.1 (x86_64-apple-darwin22.0) Antibody version dev (built from master)

In the antibody-plugins I loaded kubectx before I loaded ohmyzsh using ahmetb/kubectx path:completion kind:fpath

AlmogBaku avatar Dec 16 '22 08:12 AlmogBaku

ok, I found a solution!

  1. Load the kubectx completion before ohmyzsh (as stated in the docs) (or put autoload -U compinit && compinit after loading these completions)
  2. ignore the error log by changing the _arguments \ statement to _arguments >/dev/null 2>&1 \

sending a pr

AlmogBaku avatar Dec 16 '22 09:12 AlmogBaku

I found this script somwhere and it helped, just copy and paste it into the zsh terminal and the commands will run just fine.

cd ~
mkdir ~/.zsh
swift package completion-tool generate-zsh-script > ~/.zsh/_swift
echo -e "fpath=(~/.zsh \$fpath)\n" >> ~/.zshrc
echo -e "autoload -Uz compinit && compinit" >> ~/.zshrc

Dadaji18 avatar Feb 20 '23 08:02 Dadaji18