Skip to content

pyenv-virtualenv-init v1.3.0 fails with POSIX sh (dash) due to bash-specific array syntax #525

@chrisolof

Description

@chrisolof

Prerequisite

  • Make sure no duplicated issue has already been reported in the pyenv-virtualenv issues. You should look in closed issues, too.
  • Make sure you are reporting a problem in Pyenv-Virtualenv and not seeking consultation with Pyenv-Virtualenv use.
  • Make sure your problem is not derived from packaging (e.g. Homebrew).
    • Please refer to the package documentation for the installation issues, etc.
  • Make sure your problem is not derived from other plugins.
    • This repository is maintaining the pyenv-virtualenv plugin only. Please refrain from reporting issues of other plugins here.

Describe the bug

In v1.3.0, pyenv virtualenv-init generates shell hook code that uses bash array syntax, which is incompatible with POSIX /bin/sh. This causes scripts running with #!/bin/sh to fail with:

sh: 25: eval: Syntax error: "(" unexpected (expecting "}")

Exit code: 2

Reproduction steps

# On a system with /bin/sh (dash)
sh -c "$(pyenv virtualenv-init -)"
# OR run via any hook/script using #!/bin/sh
eval "$(pyenv virtualenv-init -)"

Likely cause

Commit 72cb35b ("Drastically speed up precommand hook by caching active version") introduced a performance optimization using bash arrays:

local -a _PYENV_VH_PATHS=()
_PYENV_VH_PATHS+=("/path/to/file")
"${_PYENV_VH_PATHS[@]}"

These constructs are not POSIX-compliant and fail when the shell is /bin/sh (dash).

Diagnostic details

  • Platform information (e.g. Ubuntu Linux 20.04): Linux, various distributions
  • OS architecture (e.g. amd64): amd64
  • pyenv version: v2.6.27
  • pyenv-virtualenv version: v1.3.0
  • Python version: 3.14.4
  • virtualenv version (if installed): N/A
  • Please attach the debug log of a faulty Pyenv invocation as a gist
    • If the problem happens in a Pyenv invocation, you can turn on debug logging by setting PYENV_DEBUG=1, e.g. env PYENV_DEBUG=1 pyenv install -v 3.6.4
    • If the problem happens outside of a Pyenv invocation, get the debug log like this:
      # for Bash
      export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
      # for Zsh
      export PS4='+(%x:%I): %N(%i): '
      
      set -x
      <reproduce the problem>
      set +x

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions