wookayin / dotfiles

:house: Personal dotfiles for *NIX systems
https://dotfiles.wook.kr/
MIT License
361 stars 105 forks source link

zsh: Rewrite how $PATH are configured in zsh startup scripts #49

Closed wookayin closed 1 year ago

wookayin commented 1 year ago

zsh: Rewrite how $PATH are configured in zsh startup scripts

Changes:

$PATH (or $path in zsh) configurations for zsh shell and scripts are unified and all moved to ~/.zshenv, from previously being scattered at many different places without a clear sense about what-goes-where.

This could be a more principled solution to the very annoying problem where $PATH being messed up in nested shells and tmux sessions, etc. (see also 97370d32, d7c7cad6, 1fd48b324 for more context and history).

With the following changes, we are now able to have a consistent $PATH environment regardless of whether we are opening a new terminal, using tmux, using a sub-shell (either interactive or non-interactive) where $PATH is set as inherited from the parent shell, etc.

Problem and Solution: (1) Fix broken $PATH on macOS

Problem and Solution: (2) Configure $path in the right place.

Background Knowledge: How does zsh startup work?

FYI, the execution order of zsh startup scripts is (see zsh manual):

   zshenv   (always)
-> zprofile (if login shell, -l)
-> zshrc    (if interactive, -i)
-> zlogin   (if login shell, -l)

Or more specifically, consider the following four cases:

sourced when -l,-i -i ⑶ (script) -l
1. /etc/zshenv always
2. ~/.zshenv always
3. /etc/zprofile login ⚠️ ✅*
4. ~/.zprofile login
5. /etc/zshrc interactive ✅* ✅*
6. ~/.zshrc interatcive
7. /etc/zlogin login ✅* ✅*
8. ~/.zlogin login

Legend:

References:

zsh: Fix incorrect $fpath ordering

antidote puts $fpath entries for the zsh plugins after the system site-functions path.

Since we want custom shell completions shipped with zsh plugins, we manually fix the order of $fpath after sourcing all the antidote plugins as a workaround.