pyenv shim leaks all internal paths (including versions/X/bin) into child process $PATH, breaking pyenv activate for downstream tools

Đang mở
#3,436 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
48/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
python, shell
Lĩnh vực
cli, tooling

Hướng nghiên cứu

Start with the reproduction using ~/.pyenv/shims/python3 and inspect libexec/pyenv, along with the shim or pyenv-exec path mentioned in the issue. Trace how PATH is assembled before the user's program is execed. Done means the child process sees the caller's original PATH without pyenv internals or versions/X/bin, while pyenv activation and version switching continue to work.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

by-design

Reproduction:

$ env PATH=/usr/bin:/bin ~/.pyenv/shims/python3 -c 'import os; print(os.environ["PATH"])'
/home/xandox/.pyenv/versions/3.13.7/bin:/home/xandox/.pyenv/libexec:/home/xandox/.pyenv/plugins/python-build/bin:/home/xandox/.pyenv/plugins/pyenv-virtualenv/bin:/home/xandox/.pyenv/plugins/pyenv-update/bin:/home/xandox/.pyenv/plugins/pyenv-doctor/bin:/usr/bin:/bin

Expected:

/usr/bin:/bin

The python process should see the $PATH the caller gave it, nothing more. Python locates its own libraries via sys.path; it doesn't need pyenv's internal directories in $PATH, nor even its own versions/X/bin.

Why versions/X/bin is the critical leak:

When any tool captures the python subprocess's $PATH and propagates it back to the parent shell (either literally, or by sourcing a bash script that does export PATH=...), the parent shell ends up with ~/.pyenv/versions/<default-version>/bin sitting in $PATH ahead of ~/.pyenv/shims. This completely defeats pyenv's version-switching mechanism: pyenv activate <env> sets PYENV_VERSION correctly, but which python still resolves to the hardcoded default-version bin directory, not the shim.

Concrete example that tripped us up:

Fish shell + bass plugin (runs a bash helper via command python3 to source bash scripts and export the resulting env back into fish). In a standard config.fish with pyenv init followed by bass source some-env-setup.sh, bass's python helper inherits the polluted $PATH, bash sources the env-setup script, and the final set -gx PATH ... pushed back into fish contains ~/.pyenv/versions/3.13.7/bin. From that point on, pyenv activate myenv appears to work (prompt updates, PYENV_VERSION is set) but python --version still shows 3.13.7.

Same class of problem would affect any python-based wrapper (pipx helpers, setuptools build scripts, subprocess invocations from IDEs/editors) that captures and re-exports $PATH.

Fix suggestion:

Before execing the user's program, the shim (or pyenv-exec) should restore $PATH to what it was when pyenv was first invoked, minus pyenv internals. One way: stash the original $PATH in a variable at the top of libexec/pyenv, then export PATH="$PYENV_ORIGINAL_PATH" just before exec.

Ngôn ngữ chính
Shell
Star
45.2k
Fork
3.3k
Merge trung bình
2 ngày 3 phút
Pull request đã merge (30 ngày)
32

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của pyenv/pyenv

Tất cả issue của pyenv/pyenv

Issue tương tự

Thêm issue về Shell/Bash

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.