Search code examples
autocompletezshbazeloh-my-zsh

Bazel tab auto complete in zsh not working


I use oh-my-zsh and I have a bazel project. I want tab complete. So that I can do bazel build //src/<tab> and get auto complete.

The first thing I tried was following:

https://docs.bazel.build/versions/master/completion.html. I included the _bazel file under a directory on my $fpath. Then I restarted my terminal and got no tab complete.

Next I tried using an oh-my-zsh plugin. https://github.com/jackwish/bazel. I cloned the plugin into the correct location, added bazel to my zshrc plugins and did source ~/.zshrc Still no tab completion.

I installed bazel using brew. I have tried brew uninstall bazel and reinstalling it. No luck...

$ echo $fpath
... /Users/<username>/.oh-my-zsh/completions ...
$ ls /Users/<username>/.oh-my-zsh/completions
_bazel

I want to press tab and get a list of options and be able to tab through them.


Solution

  • You need to re-initialize the completion. The completion system in ZSH collects completion file in fpath and record them in a .zcompdump file as an index for existing completions. After a new completion file is added, you need to redo the process.

    Solution

    1. Put the following content into your .zshrc.

      # add the _bazel into path
      fpath+=(/path/to/_bazel)
      
    2. Re-collect completions and generate .zcompdump. Run the following command in a new interactive ZSH shell.

      rm -f ~/.zcompdump; compinit
      

    I remember oh-my-zsh doesn't do the compinit for you. You may need to add the compinit into your .zshrc.

    Check the optimization here to run compinit once a day.

    Extended Reading