惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

GbyAI
GbyAI
Martin Fowler
Martin Fowler
I
InfoQ
腾讯CDC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
爱范儿
爱范儿
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
D
DataBreaches.Net
云风的 BLOG
云风的 BLOG
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
博客园 - 聂微东
Microsoft Azure Blog
Microsoft Azure Blog
D
Docker
博客园 - 三生石上(FineUI控件)
Y
Y Combinator Blog
博客园 - Franky
Engineering at Meta
Engineering at Meta
B
Blog
罗磊的独立博客
Apple Machine Learning Research
Apple Machine Learning Research
Jina AI
Jina AI
V
Visual Studio Blog

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
PATH Variable Changed inside Tmux on macOS?
2023-06-13 · via jdhao's digital space

Recently, I met a strange issue related to Tmux. After open a tmux session, the PATH variable is changed and is different from outside tmux.

This strange behavior breaks my Python:

  • outside tmux: python3 points to Python installed by Miniconda
  • inside tmux: python3 points to Python3 installed by Apple

The cause#

The reason is that when you start a tmux session, it will start a shell (in the latest macOS, it is zsh by default) in login mode. So certain zsh config files are sourced again (the culprit is /etc/zprofile), which leads to messed up PATH variable. This is also caused by macOS’s special way to construct the PATH variable.

The content of /etc/zprofile is:

if [ -x /usr/libexec/path_helper ]; then
	eval `/usr/libexec/path_helper -s`
fi

When zsh is used as a login shell, the file /etc/zprofile will be sourced. So when we run tmux command and it initialize a zsh shell, this file is source again, which messes up our PATH variable due to the behavior of path_helper.

In order to confirm this, we can do an experiment (credit here):

> PATH=BEG:$PATH:END
> echo $PATH

# output: BEG:/opt/homebrew/Caskroom/miniconda/base/bin:/opt/homebrew/Caskroom/miniconda/base/condabin:/Users/hao/.local/share/zinit/polaris/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:END

> source /etc/zprofile
> echo $PATH

# output: /usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:BEG:/opt/homebrew/Caskroom/miniconda/base/bin:/opt/homebrew/Caskroom/miniconda/base/condabin:/Users/hao/.local/share/zinit/polaris/bin:/opt/homebrew/bin:/opt/homebrew/sbin:END

What path_helper does is to construct PATH from /etc/paths and /etc/path.d. Append the current PATH variable to the constructed PATH and remove the duplicated items: I think it is just looping through the item in the PATH variable and remove items that have appeared before.

This explains why unique item inside BEG:END structure is kept and duplicate items are removed.

How to prevent#

One thing I see people do is to change /etc/zprofile, adding one line to empty PATH:

if [ -x /usr/libexec/path_helper ]; then
    PATH=""  #empty the PATH
    eval `/usr/libexec/path_helper -s`
fi

For me, it is hacky. I do not want to change the system default settings.

Another method is to tell tmux to start a non-login shell instead via default-command option:

# default command should be path to zsh executable on your system
set -g default-command /bin/zsh

If zsh is started as a non-login shell, /etc/zprofile won’t be sourced, so PATH is not changed. To check if we are running a login shell or not (zsh-only), run the following (source here):

if [[ -o login ]]; then
    print yes
else
    print no
fi

After this setup, to make sure it work, run killall tmux and exit your terminal and restart it again.

References#