antigen icon indicating copy to clipboard operation
antigen copied to clipboard

Autocompletions from oh-my-zsh kubectl plugins don't work

Open deas opened this issue 5 years ago • 10 comments

Description

Completions from ~/.antigen/bundles/robbyrussell/oh-my-zsh/plugins/kubectl/kubectl.plugin.zsh do not work for me:

In the module it reads

if (( $+commands[kubectl] )); then
    __KUBECTL_COMPLETION_FILE="${ZSH_CACHE_DIR}/kubectl_completion"

    if [[ ! -f $__KUBECTL_COMPLETION_FILE ]]; then
        kubectl completion zsh >! $__KUBECTL_COMPLETION_FILE
    fi

    [[ -f $__KUBECTL_COMPLETION_FILE ]] && source $__KUBECTL_COMPLETION_FILE

    unset __KUBECTL_COMPLETION_FILE
fi

Completion code gets written and sourced, but completion is not available at the prompt. All aliases defined by the plugin are there.

kubectl completion zsh > ~/.antigen/bundles/robbyrussell/oh-my-zsh/plugins/kubectl/_kubectl

Gets me around, but there are are are other plugins exhibiting similar behaviour - e.g. npm.

I expected putting

source <(kubectl completion zsh)

at the end of .zshrc (after antigen apply) to also get me around, but that is not the case.

Steps to reproduce

Make .zshrc read:

source $HOME/.zsh/antigen.zsh

antigen use oh-my-zsh
antigen bundle kubectl
antigen apply

Expected behavior:

- Tab completion of `kubectl` command.

Software version

  • antigen version : 2.2.2
  • zsh --version : 5.7.1
  • uname -a : 5.3.0-40

Configuration

n/a

deas avatar Feb 19 '20 13:02 deas

Per https://github.com/ohmyzsh/ohmyzsh/issues/6323 this can be fixed by adding this to your .zshrc:

# The following two modules (compinit and bashcompinit) are needed to get auto
# completion to work: https://github.com/ohmyzsh/ohmyzsh/issues/6323
autoload -U +X compinit && compinit
autoload -U +X bashcompinit && bashcompinit

You may have to run compaudit and secure some directories to not be prompted each time about insecure directories.

allen-servedio avatar Jan 16 '21 02:01 allen-servedio

Strangely I have currently somewhat similar problem, but maybe not exactly. Completion for subcommands work, for example k conf -> k config and k config use -> k config use-context However, following dynamic content (context name) - does not work: k config use-context -> .....

But if I execute source <(k completion zsh) - it solves the problem and I see list of contexts when expanding command above. And when I put it to the end if .zshrc - shell starts a tad slower, but the problem is gone

strowk avatar Apr 21 '21 17:04 strowk

Hmm, same here. zsh 5.5 and kubectl autocompletion doesn't work anymore without manual source ~/.zshrc, but some other completion works fine, like pip or docker.

python completion doesn't work too :\

Frodox avatar Jun 05 '21 10:06 Frodox

Same for me with kubectl, but other completions work fine. Also adding the following doesn't work:

autoload -U +X compinit && compinit
autoload -U +X bashcompinit && bashcompinit

Strangely if running source <(kubectl completion zsh) directly in the console it starts working even though this command is the last line in my .zshrc so no other commands run after that

UPD: For me the way of fixing it was to comment out in the .antigen/init.zsh the following lines:

# autoload -Uz add-zsh-hook; add-zsh-hook precmd _antigen_compinit
# compdef () {}

I know it's not the proper way of fixing it and that it can cause some other errors, but I'm heavily using kubectl so I need autocompletion so much

SeriyBg avatar Sep 08 '21 12:09 SeriyBg

Thank you for the workaround. It didn't work for me, because .antigen/init.zsh is (re-)generated by antigen, but it led me to a different workaround that did the trick for me: I put autoload -U +X compinit && compinit before my compdefs in my .zshrc.

darioseidl avatar Sep 28 '21 07:09 darioseidl

Hi all, compaudit helped me to fix that issue.

on my macos apparently /usr/local/share/zsh/site-functions and /usr/local/share/zsh had 775 permission and were identified by compaudit.

After fixing permission to 755 everything started to work

iasha102 avatar Nov 21 '21 17:11 iasha102

Hi all, I had a situation where I had to downgrade kubectl (via apt on Ubuntu). Afterwards kubectl autocompletion was not available any more, though it was in my plugins list in my ~/.zshrc file. It seems it can be fixed by deleting the file ~/.oh-my-zsh/cache/kubectl_completion and restarting the shell, at least that's what I did and it worked out without manually sourcing anything.

tilmanmayer avatar Dec 13 '21 16:12 tilmanmayer

UPD: For me the way of fixing it was to comment out in the .antigen/init.zsh the following lines:

# autoload -Uz add-zsh-hook; add-zsh-hook precmd _antigen_compinit
# compdef () {}

This works me! But as @darioseidl mentioned .antigen/init.zsh gets re-generated. You would need to comment this in the antigen.zsh which is sourced before performing any antigen operations shown below

source /path-to-antigen/antigen.zsh # <------------ This file

# Load the oh-my-zsh's library.
antigen use oh-my-zsh

...

or

source /path-to-antigen/antigen.zsh # <------------ This file
antigen init .antigenrc

libinvarghese avatar Jan 22 '22 04:01 libinvarghese

UPD: For me the way of fixing it was to comment out in the .antigen/init.zsh the following lines:

# autoload -Uz add-zsh-hook; add-zsh-hook precmd _antigen_compinit
# compdef () {}

This works me! But as @darioseidl mentioned .antigen/init.zsh gets re-generated. You would need to comment this in the antigen.zsh which is sourced before performing any antigen operations shown below

source /path-to-antigen/antigen.zsh # <------------ This file

# Load the oh-my-zsh's library.
antigen use oh-my-zsh

...

or

source /path-to-antigen/antigen.zsh # <------------ This file
antigen init .antigenrc

I have removed the line

compdef () {}

from antigen.zsh and replaced it with

autoload -Uz compinit && compinit 

reggiiie avatar Feb 22 '22 22:02 reggiiie

Make sure cache have completions sub-directory.

mkdir $ZSH_CACHE_DIR/completions

Then open new tab, and everything is working fine.

amritpalnagrame avatar May 18 '22 09:05 amritpalnagrame