ohmyzsh / ohmyzsh

🙃 A delightful community-driven (with 2,300+ contributors) framework for managing your zsh configuration. Includes 300+ optional plugins (rails, git, macOS, hub, docker, homebrew, node, php, python, etc), 140+ themes to spice up your morning, and an auto-update tool so that makes it easy to keep up with the latest updates from the community.

Home Page:https://ohmyz.sh

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Double `$PATH` binary directories after `omz reload`

driesvints opened this issue · comments

Describe the bug

When I echo the $PATH contents after doing a omz reload it shows all paths twice. I also immediately get this issue when starting the shell in VS Code (bit related).

Steps to reproduce

  1. Create a path.zsh file in your ZSH_CUSTOM directory
  2. Add a path which points to binaries like export PATH="$HOME/.node/bin:$PATH"
  3. Open shell en echo $PATH: observe $PATH contents are okay
  4. Run omz reload
  5. Echo $PATH: observe $PATH contents are duplicate

Expected behavior

I'd expect the correct $PATH contents without duplicates even after a omz reload.

Screenshots and recordings

Last login: Tue Apr  2 10:53:19 on ttys002
$ echo $PATH
/Users/driesvints/Library/Application Support/Herd/config/nvm/versions/node/v16.20.2/bin:/Users/driesvints/Library/Application Support/Herd/bin/:/Users/driesvints/.composer/vendor/bin:node_modules/.bin:vendor/bin:/Users/driesvints/.node/bin:/Users/driesvints/.dotfiles/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
$ omz reload
$ echo $PATH
/Users/driesvints/Library/Application Support/Herd/config/nvm/versions/node/v16.20.2/bin:/Users/driesvints/Library/Application Support/Herd/bin/:/Users/driesvints/.composer/vendor/bin:node_modules/.bin:vendor/bin:/Users/driesvints/.node/bin:/Users/driesvints/.dotfiles/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:/Users/driesvints/Library/Application Support/Herd/config/nvm/versions/node/v16.20.2/bin:/Users/driesvints/Library/Application Support/Herd/bin/:/Users/driesvints/.composer/vendor/bin:node_modules/.bin:vendor/bin:/Users/driesvints/.node/bin:/Users/driesvints/.dotfiles/bin:/opt/homebrew/bin:/opt/homebrew/sbin

OS / Linux distribution

macOS 14.4.1

Zsh version

5.9

Terminal emulator

Terminal.app

If using WSL on Windows, which version of WSL

None

Additional context

This behaviour occurs on Terminal.app but on VS Code it immediately occurs when loading any shell. I do not know if VS Code runs omz reload behind the scenes before showing the shell.

This is expected behavior if you are adding paths to $PATH without checking if they're already there. The same happens if you reload the shell with source ~/.zshrc or other equivalent approaches.

There is no way to undo that, as by the time you run exec zsh to replace the shell, the $PATH value (already changed) gets passed down to the new shell. The only possible way to undo this if we force a new environment on omz reload, but that would have unintended consequences and possibly break stuff much worse.

The 2 approaches you have are:

  1. Check if a directory is in $PATH before adding it: https://stackoverflow.com/a/1397020
  2. Use typeset -U path (zsh-only) which will remove all non-unique entries of $PATH (path is a special variable in zsh which is the array form of $PATH, -U says remove make the array contents unique).

Thanks a lot for the explanation @mcornella! typeset -U path helped!