発生ケース
エディター起動時の terminal に表示される。
ログ
pyenv: cannot rehash: couldn't acquire lock /Users/username/.pyenv/shims/.pyenv-shim for 60 seconds. Last error message:
/opt/homebrew/Cellar/pyenv/2.6.23/libexec/pyenv-rehash: 行 22: /Users/username/.pyenv/shims/.pyenv-shim: cannot overwrite existing file
pyenv: cannot rehash: couldn't acquire lock /Users/username/.pyenv/shims/.pyenv-shim for 60 seconds. Last error message:
/opt/homebrew/Cellar/pyenv/2.6.23/libexec/pyenv-rehash: 行 22: /Users/username/.pyenv/shims/.pyenv-shim: cannot overwrite existing file
原因
- pyenv の
rehashは/Users/username/.pyenv/shims/.pyenv-shimを一時的なロックファイルとして使う - シェル起動時に
.zprofileのpyenv init --pathと.zshrcのpyenv init -が rehash を走らせる -
.pyenv-shim(前回の rehash が中断され、正常終了時の削除が行われなかったもの)が邪魔をし、新しい rehash がロックを取れなかった -
cannot overwrite existing fileは「noclobber で空ファイルを作ろうとしたが、既にファイルが存在する」という意味 - エディターが複数ターミナルを同時起動するため、同じエラーが2回出ていた
対応
- 古いロックファイルを削除:
rm -f ~/.pyenv/shims/.pyenv-shim -
pyenv rehashを実行して正常完了を確認
再発時の対処
同じエラーが出た場合は、以下を実行する。
rm -f ~/.pyenv/shims/.pyenv-shim && pyenv rehash
これでエラーは出なくなるはず。