kubernetes: kubectl bash completion causes bash file path completion to fail

Is this a BUG REPORT or FEATURE REQUEST?:

/kind bug

What happened:

Bash file path completion fails when trying to run kubectl commands and source /usr/local/etc/bash_completion.d/kubectl is in ~/.bash_profile. If I run kubectl delete -f ../<TAB> then I get the error message show below in the section on reproduction.

What you expected to happen:

I espect to see files/directories that ../ can complete to.

How to reproduce it (as minimally and precisely as possible):

Amits-MacBook-Pro:boku amitgupta$ cat ~/.bash_profile
### kubectl ###
source /usr/local/etc/bash_completion.d/kubectl
Amits-MacBook-Pro:boku amitgupta$ exec -l $SHELL
Amits-MacBook-Pro:boku amitgupta$ kubectl ../bash: __ltrim_colon_completions: command not found

I hit <TAB> after kubectl ../.

Anything else we need to know?:

This problem arises only with the kubectl command, not any others.

It arises no matter what I seem to put in after kubectl, even kubectl <SPACE><TAB> brings up the issue, doesn’t have to be something like kubectl delete -f ../<TAB>.

The error message changes. In the example above, it says bash: __ltrim_colon_completions: command not found, but sometimes it says bash: _filedir: command not found in the same place.

When I remove kubectl Bash completions from my ~/.bash_profile (and restart/re-login to the shell), the problem goes away. These file completions came with my Homebrew installation of kubectl.

There is no difference between what Homebrew provided and what kubectl completion bash provides:

$ diff <(kubectl completion bash) /usr/local/etc/bash_completion.d/kubectl

Environment:

  • Kubernetes version (use kubectl version):
    $ kubectl version
    Client Version: version.Info{Major:"1", Minor:"7", GitVersion:"v1.7.0", GitCommit:"d3ada0119e776222f11ec7945e6d860061339aad", GitTreeState:"clean", BuildDate:"2017-06-30T09:51:01Z", GoVersion:"go1.8.3", Compiler:"gc", Platform:"darwin/amd64"}
    Server Version: version.Info{Major:"1", Minor:"6", GitVersion:"v1.6.6", GitCommit:"7fa1c1756d8bc963f1a389f4a6937dc71f08ada2", GitTreeState:"clean", BuildDate:"2017-06-16T18:21:54Z", GoVersion:"go1.7.6", Compiler:"gc", Platform:"linux/amd64"}
    
  • Cloud provider or hardware configuration**: N/A
  • OS (e.g. from /etc/os-release):
    $ sw_vers
    ProductName:	Mac OS X
    ProductVersion:	10.12.5
    BuildVersion:	16F2104
    
  • Kernel (e.g. uname -a):
    $ uname -a
    Darwin Amits-MacBook-Pro.local 16.6.0 Darwin Kernel Version 16.6.0: Fri Apr 14 16:13:31 PDT 2017; root:xnu-3789.60.24~4/RELEASE_X86_64 x86_64
    
  • Install tools:
    $ brew --version
    Homebrew 1.2.4
    Homebrew/homebrew-core (git revision 36f1; last commit 2017-07-06)
    
    $ brew info --json=v1 kubectl | jq '.[0].name,.[0].installed[0].version' -r
    kubernetes-cli
    1.7.0
    
  • Others:
    $ bash --version
    GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
    Copyright (C) 2007 Free Software Foundation, Inc.
    

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 8
  • Comments: 18 (5 by maintainers)

Commits related to this issue

Most upvoted comments

I’m on Windows with MinGW. After digging into system directories, it seems bash_completion script which contains helper functions such as _filedir is missing.

I had to get files from Git repository at https://github.com/scop/bash-completion/blob/master/bash_completion to /usr/share/bash-completion/bash_completion

Then add following line on top of my ~/.bashrc:

source /usr/share/bash-completion/bash_completion

You need to source bash-completion from either /etc/bashrc or ~/.bashrc. See kubectl completion -h for more details.

$ kubectl completion -h
Output shell completion code for the specified shell (bash or zsh). The shell code must be evalutated to provide
interactive completion of kubectl commands.  This can be done by sourcing it from the .bash _profile. 

Note: this requires the bash-completion framework, which is not installed by default on Mac.  This can be installed by
using homebrew: 

  $ brew install bash-completion
  
Once installed, bash completion must be evaluated.  This can be done by adding the following line to the .bash profile 

  $ source $(brew --prefix)/etc/bash_completion
  
Note for zsh users: [1] zsh completions are only supported in versions of zsh >= 5.2

Examples:
  # Install bash completion on a Mac using homebrew
  brew install bash-completion
  printf "
  # Bash completion support
  source $(brew --prefix)/etc/bash_completion
  " >> $HOME/.bash_profile
  source $HOME/.bash_profile
  
  # Load the kubectl completion code for bash into the current shell
  source <(kubectl completion bash)
  
  # Write bash completion code to a file and source if from .bash_profile
  kubectl completion bash > ~/.kube/completion.bash.inc
  printf "
  # Kubectl shell completion
  source '$HOME/.kube/completion.bash.inc'
  " >> $HOME/.bash_profile
  source $HOME/.bash_profile
  
  # Load the kubectl completion code for zsh[1] into the current shell
  source <(kubectl completion zsh)

Usage:
  kubectl completion SHELL [options]

Use "kubectl options" for a list of global command-line options (applies to all commands).

I had same problem on OSX with native bash:

$ kubectl  -bash: __ltrim_colon_completions: command not found

It seems like, the whole thing only works when:

...

if [ -f /usr/local/share/bash-completion/bash_completion ]; then
  . /usr/local/share/bash-completion/bash_completion
fi

source ~/.kube/kubectl_autocompletion

above code snippet is what the final addition to my ~/.bash_profile

I had the same in Manjaro ( archlinux ), in addition to @simonxy : pacman -S bash-completion kubectl completion bash > ~/.kube/kubectl_autocompletion add to .bashrc if [ -f /usr/local/share/bash-completion/bash_completion ]; then . /usr/local/share/bash-completion/bash_completion fi source ~/.kube/kubectl_autocompletion and run source .bashrc

Btw, on macOS if you have upgraded your bash to 4+, make sure to change your shell via chsh. More info here: https://apple.stackexchange.com/a/55998/273340

Think I figured it out, I missed the part in the Examples section of the kubectl completion -h output which says to brew install bash-completion. The completion kind of works even without this, e.g. kubectl ap<TAB> gives:

$ kubectl apbash: __ltrim_colon_completions: command not found

api-versions  apply