virtualenv-init: deduplicate PATH in-process for bash and zsh
VB
5d ago+107+107+012345678901234567890123456789-17-17-01234567890123456789
Summary
eval "$(pyenv virtualenv-init -)" currently has two PATH deduplication gaps:
- Bash spawns a subprocess on every shell startup:
pyenv-virtualenv-init - bashemitsPATH="$(bash --norc -ec '...')"(ported frompyenv-initin #530), spawning an extrabashprocess on every shell startup even when${shims}is not yet onPATH. - Zsh still duplicates
shimsinPATH: Zsh falls through to the* )branch and prepends${shims}:${PATH}unconditionally without removing prior occurrences (the original issue behind #474).
Changes
- Bash (
bin/pyenv-virtualenv-init): Replace thebash --norc -ecsubshell with in-process Bash parameter expansion matching exactPATHsegments (stripping single/consecutive/leading/trailing duplicates without touching partial matches like.../shims-extra), using single-quote escaping compatible with Bash 3.2+ and safe underset -euand special characters (',",$,*,?,[, spaces). - Zsh (
bin/pyenv-virtualenv-init): Add azsh )branch using Zsh's nativepath=("${(@)path:#"..."}")array filter with zero subprocesses (matching the approach inpyenv/pyenv#3638). - Double-quoted path escaping &
/proc/${PPID}/cmdlineshell detection: Escape\,",$,`in emitted double-quotedPATHstrings, and read/proc/${PPID}/cmdlinebefore falling back topswhenPYENV_SHELL/$1is unset (eliminating apssubprocess on systems with/procand matchingpyenv-init).
Benchmark
Isolated eval timing on Windows Git Bash (Bash 5.2, 15 runs each, clean PATH=/usr/bin:/bin):
| Scenario | Before | After |
|---|---|---|
eval "$init_code" (shims already on PATH, warm) | 132 ms/call | 4 ms/call |
eval "$init_code" (shims not on PATH, cold first spawn) | 1046 ms/call | 4 ms/call |
Tests
- Updated
outputs bash-specific syntaxandoutputs zsh-specific syntaxintest/init.bats. - Added
removes existing shims from PATH without spawning bash (bash)(verifies duplicate removal, partial prefix preservation,set -eucompatibility, and zerobashinvocations duringeval). - Added
removes existing shims from PATH with special characters (bash)(covers spaces,',",$,*,?,[withPYENV_VIRTUALENV_ROOTunset beforeeval). - Added
removes existing shims from PATH (zsh)(runs underzsh -dfwhen available). - Verified locally in Git Bash:
bats --tap test/init.batsexits0(11 passed, 1 skipped—zshnot installed locally), anddash -eucleanly evaluatespyenv-virtualenv-init - sh.
Summary by cubic
pyenv-virtualenv-init now deduplicates shims on PATH in-process for bash and zsh, removing subprocess spawns on every shell startup and fixing duplicate shims accumulation in zsh.
- Bash uses parameter expansion that matches exact
PATHsegments, safe underset -euand special characters. - Zsh uses its native
patharray filter to strip priorshimsentries before prepending. - Parent-shell detection reads
/proc/${PPID}/cmdlinebefore falling back to apssubprocess. - Emitted double-quoted paths escape
\,",$, and backticks. - Benchmark:
evalcost drops from ~132 ms to ~4 ms warm, and ~1046 ms to ~4 ms on cold first spawn.
Tests
- Updated
outputs bash-specific syntaxandoutputs zsh-specific syntax; added bash and zsh tests for duplicate removal, special characters, zerobashsubprocesses, andset -eucompatibility.