virtualenv-init: deduplicate PATH in-process for bash and zsh

VB
5d ago
+107-17

Summary

eval "$(pyenv virtualenv-init -)" currently has two PATH deduplication gaps:

  1. Bash spawns a subprocess on every shell startup: pyenv-virtualenv-init - bash emits PATH="$(bash --norc -ec '...')" (ported from pyenv-init in #530), spawning an extra bash process on every shell startup even when ${shims} is not yet on PATH.
  2. Zsh still duplicates shims in PATH: 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 the bash --norc -ec subshell with in-process Bash parameter expansion matching exact PATH segments (stripping single/consecutive/leading/trailing duplicates without touching partial matches like .../shims-extra), using single-quote escaping compatible with Bash 3.2+ and safe under set -eu and special characters (', ", $, *, ?, [, spaces).
  • Zsh (bin/pyenv-virtualenv-init): Add a zsh ) branch using Zsh's native path=("${(@)path:#"..."}") array filter with zero subprocesses (matching the approach in pyenv/pyenv#3638).
  • Double-quoted path escaping & /proc/${PPID}/cmdline shell detection: Escape \, ", $, ` in emitted double-quoted PATH strings, and read /proc/${PPID}/cmdline before falling back to ps when PYENV_SHELL / $1 is unset (eliminating a ps subprocess on systems with /proc and matching pyenv-init).

Benchmark

Isolated eval timing on Windows Git Bash (Bash 5.2, 15 runs each, clean PATH=/usr/bin:/bin):

ScenarioBeforeAfter
eval "$init_code" (shims already on PATH, warm)132 ms/call4 ms/call
eval "$init_code" (shims not on PATH, cold first spawn)1046 ms/call4 ms/call

Tests

  • Updated outputs bash-specific syntax and outputs zsh-specific syntax in test/init.bats.
  • Added removes existing shims from PATH without spawning bash (bash) (verifies duplicate removal, partial prefix preservation, set -eu compatibility, and zero bash invocations during eval).
  • Added removes existing shims from PATH with special characters (bash) (covers spaces, ', ", $, *, ?, [ with PYENV_VIRTUALENV_ROOT unset before eval).
  • Added removes existing shims from PATH (zsh) (runs under zsh -df when available).
  • Verified locally in Git Bash: bats --tap test/init.bats exits 0 (11 passed, 1 skipped — zsh not installed locally), and dash -eu cleanly evaluates pyenv-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 PATH segments, safe under set -eu and special characters.
  • Zsh uses its native path array filter to strip prior shims entries before prepending.
  • Parent-shell detection reads /proc/${PPID}/cmdline before falling back to a ps subprocess.
  • Emitted double-quoted paths escape \, ", $, and backticks.
  • Benchmark: eval cost drops from ~132 ms to ~4 ms warm, and ~1046 ms to ~4 ms on cold first spawn.

Tests

  • Updated outputs bash-specific syntax and outputs zsh-specific syntax; added bash and zsh tests for duplicate removal, special characters, zero bash subprocesses, and set -eu compatibility.