You are not logged in.

#1 2021-01-29 02:25:06

adigitoleo
Member
Registered: 2020-08-02
Posts: 28

[SOLVED] Pyenv zsh completions don't work

Just downloaded pyenv for a project that needs python 3.8 and I'm wondering why my pyenv completions aren't working:

❯ pacman -Qo /usr/share/zsh/site-functions/_pyenv
/usr/share/zsh/site-functions/_pyenv is owned by pyenv 1.2.22-1

❯ pacman -Qikk pyenv
Name            : pyenv
Version         : 1.2.22-1
Description     : Easily switch between multiple versions of Python
Architecture    : any
URL             : https://github.com/pyenv/pyenv
Licenses        : MIT
Groups          : None
Provides        : None
Depends On      : bash
Optional Deps   : git: installing development versions [installed]
Required By     : None
Optional For    : None
Conflicts With  : None
Replaces        : None
Installed Size  : 2.24 MiB
Packager        : David Runge <dvzrv@archlinux.org>
Build Date      : Sun 10 Jan 2021 21:50:08
Install Date    : Wed 27 Jan 2021 22:35:42
Install Reason  : Explicitly installed
Install Script  : No
Validated By    : Signature

pyenv: 797 total files, 0 altered files

❯ grep compinit .zshrc
autoload -Uz compinit; compinit

Anything I can do to debug? Does it work for everyone else?
Cheers

Last edited by adigitoleo (2021-02-19 11:17:56)

Offline

#2 2021-01-30 15:19:10

dartheian
Member
Registered: 2019-03-19
Posts: 12

Re: [SOLVED] Pyenv zsh completions don't work

I have the same problem. Trying with an almost empty zshrc without any other plugin/plugin-manager, but can't make it work.

source /usr/share/zsh/plugins/zsh-history-substring-search/zsh-history-substring-search.zsh
bindkey '^[[A' history-substring-search-up
bindkey '^[[B' history-substring-search-down

autoload -Uz compinit
compinit
zstyle ':completion::complete:*' gain-privileges 1
zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'
zstyle ':completion:*' menu select

alias ls="ls --color=auto"
alias icat="kitty +kitten icat"
alias diff="kitty +kitten diff"
>>> pikaur -Qs zsh
local/zsh 5.8-1
    A very advanced and programmable command interpreter (shell) for UNIX
local/zsh-history-substring-search 1.0.2-1
    ZSH port of Fish history search (up arrow)

Adding

eval "$(pyenv init -)"

to my zshrc does not fix the problem.

If I install it using the installer provided on the GitHub page, it works, but it does not if installed with pacman.

UPDATE:

I don't know it this can help, but it seems a strange behavior to me:

>>> which pyenv
pyenv () {
	local command
	command="${1:-}" 
	if [ "$#" -gt 0 ]
	then
		shift
	fi
	case "$command" in
		(activate | deactivate | rehash | shell) eval "$(pyenv "sh-$command" "$@")" ;;
		(*) command pyenv "$command" "$@" ;;
	esac
}
>>> cat /usr/share/zsh/site-functions/_pyenv
if [[ ! -o interactive ]]; then
    return
fi

compctl -K _pyenv pyenv

_pyenv() {
  local words completions
  read -cA words

  if [ "${#words}" -eq 2 ]; then
    completions="$(pyenv commands)"
  else
    completions="$(pyenv completions ${words[2,-2]})"
  fi

  reply=(${(ps:\n:)completions})
}
>>> pyenv init -
export PATH="/home/heian/.pyenv/shims:${PATH}"
export PYENV_SHELL=zsh
command pyenv rehash 2>/dev/null
pyenv() {
  local command
  command="${1:-}"
  if [ "$#" -gt 0 ]; then
    shift
  fi

  case "$command" in
  activate|deactivate|rehash|shell)
    eval "$(pyenv "sh-$command" "$@")";;
  *)
    command pyenv "$command" "$@";;
  esac
}

UPDATE 2:

The same behavior if I install it with the installer: https://github.com/pyenv/pyenv-installer
So maybe it is the expected behavior

Moreover, using the Arch package. I can't activate multiple Python versions...

Last edited by dartheian (2021-01-30 17:26:54)

Offline

#3 2021-01-31 10:57:34

adigitoleo
Member
Registered: 2020-08-02
Posts: 28

Re: [SOLVED] Pyenv zsh completions don't work

dartheian wrote:

I don't know it this can help, but it seems a strange behavior to me:

That's just what happens when you run eval "$(pyenv init -)", it wraps the executable in that shell function. From the README:

Installs the sh dispatcher. This bit is also optional, but allows pyenv and plugins to change variables in your current shell, making commands like pyenv shell possible. The sh dispatcher doesn't do anything crazy like override cd or hack your shell prompt, but if for some reason you need pyenv to be a real script rather than a shell function, you can safely skip it.

dartheian wrote:

If I install it using the installer provided on the GitHub page, it works

I'll have to try it, and maybe digging into what it does and comparing with the PKGBUILD is the next step forward. Thanks for your input smile

Offline

#4 2021-01-31 11:28:22

adigitoleo
Member
Registered: 2020-08-02
Posts: 28

Re: [SOLVED] Pyenv zsh completions don't work

Aha! For me, at least, the _pyenv completion function is just not being found at all:

❯ type _pyenv
_pyenv not found

❯ type _pacman  # for comparison
_pacman is an autoload shell function

This is despite the file being at the correct path as shown before. I'll need to investigate how zsh completion works a bit more before going any further.

Offline

#5 2021-02-05 19:15:38

dartheian
Member
Registered: 2019-03-19
Posts: 12

Re: [SOLVED] Pyenv zsh completions don't work

If I install the Arch package:

>>> which pyenv
/usr/bin/pyenv

So, it seems the package install it under /usr/bin and not in ~/.pyenv, so the _pyenv function is not invoked (which triggers autocompletion, right?).
No idea on how to solve this though sad

EDIT:

I forgot to add pyenv init to my zshrc... now:

>>> which pyenv
pyenv () {
	local command
	command="${1:-}" 
	if [ "$#" -gt 0 ]
	then
		shift
	fi
	case "$command" in
		(rehash | shell) eval "$(pyenv "sh-$command" "$@")" ;;
		(*) command pyenv "$command" "$@" ;;
	esac
}

Still no autocompletion though sad

Last edited by dartheian (2021-02-05 20:06:53)

Offline

#6 2021-02-06 07:16:57

adigitoleo
Member
Registered: 2020-08-02
Posts: 28

Re: [SOLVED] Pyenv zsh completions don't work

dartheian wrote:

So, it seems the package install it under /usr/bin and not in ~/.pyenv, so the _pyenv function is not invoked (which triggers autocompletion, right?).

This shouldn't be a problem, since zsh should look at /usr/share/zsh/site-functions/ to find the completion scripts. The one for pacman itself is there, for example. For some reason ZLE is not finding /usr/share/zsh/site-functions/_pyenv or not reading it properly.

I can reproduce the same problem with a clean zshrc (only running compinit) so I don't think the zshrc function is the problem. I haven't had time to look into this much more though... I've never written shell completion functions so I would need to read more in the zsh manuals. Maybe it's worth opening an issue at the pyenv repo?

Last edited by adigitoleo (2021-02-06 07:18:08)

Offline

#7 2021-02-19 11:17:22

adigitoleo
Member
Registered: 2020-08-02
Posts: 28

Re: [SOLVED] Pyenv zsh completions don't work

The upstream completion scripts are broken, apparently they are meant to be sourced from zshrc directly or something.

There are two (!) open PR's to fix it but it doesn't seem to be a priority:

https://github.com/pyenv/pyenv/pull/1458

https://github.com/pyenv/pyenv/pull/1644

The workaround is to use a custom completion script for pyenv:

#compdef pyenv
if [[ ! -o interactive ]]; then
    return
fi

local state line
typeset -A opt_args

_arguments -C \
    {--help,-h}'[Show help]' \
    {--version,-v}'[Show pyenv version]' \
    '(-): :->command' \
    '*:: :->option-or-argument'

case "$state" in
    (command)
        local -a commands
        commands=(${(f)"$(pyenv commands)"})
        _describe -t commands 'command' commands
        ;;
    (option-or-argument)
        local -a args
        args=(${(f)"$(pyenv completions ${line[1]})"})
        _describe -t args 'arg' args
        ;;
esac

return

Save it somewhere as `_pyenv` and add the folder to `$fpath`. See also http://bewatermyfriend.org/p/2012/003/

Makring as solved since it's an upstream issue.

Offline

Board footer

Powered by FluxBB