Poetry: instructions for enabling zsh completions for oh-my-zsh don't work

Created on 13 Dec 2019  路  4Comments  路  Source: python-poetry/poetry

  • [x] I am on the latest Poetry version.
  • [x] I have searched the issues of this repo and believe that this is not a duplicate.
  • [ ] If an exception occurs when executing a command, I executed it again in debug mode (-vvv option).
  • OS version and name: Fedora 31
  • Poetry version: 1.0.0
  • Link of a Gist with the contents of your pyproject.toml file:

Issue

Instructions on how to enable poetry completions for zsh with oh-my-zsh on https://python-poetry.org/docs/#enable-tab-completion-for-bash-fish-or-zsh don't do anything. Firstly, the oh-my-zsh plugin has to be named poetry.plugin.zsh, not _poetry. Secondly, it's better be installed into .oh-my-zsh/custom/plugins. Thirdly, when it's enabled with plugins(poetry ...), it results in a bunch of compinit error messages on start.

Bug Documentation

Most helpful comment

I found the following instructions to work on MacOS:

print -l $fpath | grep '.oh-my-zsh/completions'
mkdir ~/.oh-my-zsh/completions
poetry completions zsh  > ~/.oh-my-zsh/completions/_poetry
rm ~/.zcompdump*

source ~/.zshrc throws no errors and gives correct completions.

A similar discussion:
https://github.com/gopasspw/gopass/issues/585

All 4 comments

I found the following instructions to work on MacOS:

print -l $fpath | grep '.oh-my-zsh/completions'
mkdir ~/.oh-my-zsh/completions
poetry completions zsh  > ~/.oh-my-zsh/completions/_poetry
rm ~/.zcompdump*

source ~/.zshrc throws no errors and gives correct completions.

A similar discussion:
https://github.com/gopasspw/gopass/issues/585

They work as expected (fedora 31 also here);
the only thing to do after the instuctions is to remove the ~/.zcompdump* file in the home directory.
After that, opening the terminal it will be recreated with poetry's completions enabled

Can confirm it works on MacOS as well, thanks @tsiq-alberto!

Firstly, the oh-my-zsh plugin has to be named poetry.plugin.zsh, not _poetry. Secondly, it's better be installed into .oh-my-zsh/custom/plugins

Indeed, in case of oh-my-zsh it is recommended that custom plugins be installed at $ZSH_CUSTOM/plugins so it does not get deleted in updates.
However, it is not true that the file should be named poetry.plugin.zsh.

The oh-my-zsh.sh script (which is run at shell startup) contains the following lines:

is_plugin() {                                                                                                                                                                                
  local base_dir=$1                                                             
  local name=$2                                                                 
  builtin test -f $base_dir/plugins/$name/$name.plugin.zsh \                    
    || builtin test -f $base_dir/plugins/$name/_$name                              
}                                                                                  

# Add all defined plugins to fpath. This must be done                              
# before running compinit.                                                         
for plugin ($plugins); do                                                          
  if is_plugin $ZSH_CUSTOM $plugin; then                                           
    fpath=($ZSH_CUSTOM/plugins/$plugin $fpath)                                     
  elif is_plugin $ZSH $plugin; then                                                
    fpath=($ZSH/plugins/$plugin $fpath)                                            
  else                                                                             
    echo "[oh-my-zsh] plugin '$plugin' not found"                                  
  fi                                                                               
done

As per function is_plugin, either $name.plugin.zsh or _$name would be accepted and prepended into fpath (used by compinit later).

The difference is that _$name will not be explicitely sourced a bit later:

# Load all of the plugins that were defined in ~/.zshrc                         
for plugin ($plugins); do                                                       
  if [ -f $ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh ]; then                
    source $ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh                       
  elif [ -f $ZSH/plugins/$plugin/$plugin.plugin.zsh ]; then                     
    source $ZSH/plugins/$plugin/$plugin.plugin.zsh                              
  fi                                                                            
done

The only thing that is missing in documentation and I had to do in order to have it working, it is removing ~/.zcompdump* as stated by @tsiq-alberto.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ambv picture ambv  路  3Comments

ulope picture ulope  路  3Comments

ghost picture ghost  路  3Comments

thmo picture thmo  路  3Comments

etijskens picture etijskens  路  3Comments