[Bug]: Herd keeps injecting itself in the $PATH even if it's already added
Operating system version
macOs Ventura 13.5.2
System architecture
ARM64 (M1, M2, etc)
Herd Version
1.2.0 (Build: 14)
PHP Version
No response
Bug description
Hi all, I'm experiencing two minor issues when editing the $PATH variable line Herd injected line into my ~/.zshrc
Issue when removing the trailing slash from the Herd PHP binary folder
When I update the line Herd injected into my ~/.zshrc from:
# Herd injected PHP binary.
export PATH="/Users/brtdv/Library/Application Support/Herd/bin/":$PATH
to
# Herd injected PHP binary.
export PATH="/Users/brtdv/Library/Application Support/Herd/bin":$PATH
(note the removed trailing slash).
When I quit and re-launc Herd, Herd will re-inject an additional line at the end of the file with, causing the ~/.zshrc file to have two lines with the same folder injected into $PATH, causing my ~/.zshrc to contain two duplicate lines:
# Herd injected PHP binary.
export PATH="/Users/brtdv/Library/Application Support/Herd/bin":$PATH
# Herd injected PHP binary.
export PATH="/Users/brtdv/Library/Application Support/Herd/bin/":$PATH
Why would I want to remove this trailing slash? A meaningless thing but: when I output which php, the result is /Users/brtdv/Library/Application Support/Herd/bin//php (note the double slash). If you remove the trailing slash from the path that is injected into the $PATH, the php filepath will be output correctly, without double slash:
❯ which php
/Users/brtdv/Library/Application Support/Herd/bin/php
Herd being aggressive about the index of the PHP binary folder index in the $PATH variable
I'm still trying to use some of the homebrew PHP binaries in combination with Herd. For that reason, I tried to edit the $PATH manually so that Herd's PHP binary path is injected into the $PATH at the last index.
I tried editing
# Herd injected PHP binary.
export PATH="/Users/brtdv/Library/Application Support/Herd/bin/":$PATH
to
# Herd injected PHP binary.
export PATH=$PATH:"/Users/brtdv/Library/Application Support/Herd/bin/"
When I update my local setup like this, when I use brew link php, these binaries will get priority over Herds' php binaries. When i then run brew unlink php, I can then re-use Herds' PHP binaries.
However, next time I launch Herd, it will again reinject its PHP folder at the first index of the $PATH, causing the ~/.zshrc file to contain:
# Herd injected PHP binary.
export PATH=$PATH:"/Users/brtdv/Library/Application Support/Herd/bin/"
# Herd injected PHP binary.
export PATH="/Users/brtdv/Library/Application Support/Herd/bin/":$PATH
I understand that the way this is injected into the $PATH "on first run" is the best way for beginner and novice users and fits the best with Herd's "Zero dependencies. Zero headaches." mantra. This should not be changed! However, when I manually edit the $PATH to have Herd's php binaries folder at the last index, I feel Herd should respect that and not try to re-inject itself at index 0 of the $PATH's folders?
I think both issues are related and could be fixed by Herd treating the $PATH as an array of folders on boot and checking if the "~/Library/Application Support/Herd/bin" folder is present anywhere in the array of folders of $PATH? Right now Herd seems to check only if the folder is present at the first index of $PATH (or at the beginning of the "string" $PATH?)
Steps to reproduce
Issue 1
- Open
~/.zshrc - Update the line containing
export PATH="/Users/brtdv/Library/Application Support/Herd/bin/":$PATHby removing the trailing slash, like so:export PATH="/Users/brtdv/Library/Application Support/Herd/bin":$PATH - Restart the Herd macOS app
- Herd will reinject a line containing
export PATH="/Users/brtdv/Library/Application Support/Herd/bin/":$PATHand your~/.zshrc$PATHvariable will contain two references to the Herd PHP binary folder.
Issue 2
- Open
~/.zshrc - Update the line containing
export PATH="/Users/brtdv/Library/Application Support/Herd/bin/":$PATHby placing the Herd PHP binary folder at the last index in$PATH, like so:export PATH=$PATH:"/Users/brtdv/Library/Application Support/Herd/bin/" - Restart the Herd macOS app
- Herd will reinject a line containing
export PATH="/Users/brtdv/Library/Application Support/Herd/bin/":$PATHand your~/.zshrc$PATHvariable will contain two references to the Herd PHP binary folder.
Relevant log output
No response
Thank you for the in-depth bug report. I agree that this is confusing - I'll modify the way that we detect if Herd is properly configured in your zshrc/bashrc file so that we do not insert this multiple times, if it's already present.
Zoo should probably close this issue
Seems like this May be happening on Windows aswell. Found multiple entries relatibg to herd in path
The way Herd injects into .zshrc seems to be causing problems more than once. What if, instead of injecting directly into .zshrc, Herd, wrote to its own file, and only injected a source command into .zshrc? That would make it easier for people to manage... they could just move that source command around to a good location in .zshrc, and Herd could have carte blanche to write to .herdrc or whatever.
To add to this issue — when using other similar software like Local for WordPress https://localwp.com/ the way Herd writes to .zshrc can cause some conflicts. I just went through a number of steps to try and fix a bug with Local which turned out to be caused by Herd's additional zsh lines, even when Herd isn't running.
It might be worth looking at how Herd manages its versions of PHP. The way Local works is quite elegant, it ships with all its own versions of php like Herd does but they aren't added to the path. If you ever need to you can open a site "shell" from the Local app, which opens terminal with an .sh command something like this:
echo -n -e "\033]0;Shell\007"
export MYSQL_HOME="/Users/username/Library/Application Support/Local/run/cCt6zyuyj/conf/mysql"
export PHPRC="/Users/username/Library/Application Support/Local/run/cCt6zyuyj/conf/php"
export WP_CLI_CONFIG_PATH="/Applications/Local.app/Contents/Resources/extraResources/bin/wp-cli/config.yaml"
export WP_CLI_DISABLE_AUTO_CHECK_UPDATE=1
# Add PHP, MySQL, and WP-CLI to $PATH
echo "Setting Local environment variables..."
export PATH="/Users/username/Library/Application Support/Local/lightning-services/mysql-8.0.16+6/bin/darwin/bin:$PATH"
export PATH="/Users/username/Library/Application Support/Local/lightning-services/php-8.3.0+1/bin/darwin-arm64/bin:$PATH"
export PATH="/Applications/Local.app/Contents/Resources/extraResources/bin/wp-cli/posix:$PATH"
export PATH="/Applications/Local.app/Contents/Resources/extraResources/bin/composer/posix:$PATH"
export MAGICK_CODER_MODULE_PATH="/Users/username/Library/Application Support/Local/lightning-services/php-8.3.0+1/bin/darwin-arm64/ImageMagick/modules-Q16/coders"
echo "----"
echo "WP-CLI: $(wp --version)"
echo "Composer: $(composer --version | cut -f3-4 -d" ")"
echo "PHP: $(php -r "echo PHP_VERSION;")"
echo "MySQL: $(mysql --version)"
echo "----"
cd "/Users/username/Local Sites/sitename/app/public"
echo "Launching shell: $SHELL ..."
exec $SHELL
➜ public
This makes the correct version of PHP show up should you ever need to run terminal commands, but means you can have different sites use different versions of PHP and composer, without overlapping each other or "polluting" the global PHP path.
IMHO with Herd offering to be an Application that means you don't need to get into the terminal so much, it doesn't seem right that it modifies the $PATH anyway.
My .zshrc also contains path information many times.
This problem occurs when the home directory part is replaced with the $HOME keyword, so I would appreciate it if you could fix this.
As @djave-co said above it seems that the way Herd works will conflict directly with localwp.com
Since the Herd injected PHP binary seems to always run after the LocalWP script, the correct PHP binary won't be able to load, and Herd's binary will load instead, eg:
# Herd injected PHP binary.
export PATH="/Users/eduwass/Library/Application Support/Herd/bin/":$PATH
A fix for this would be great, would love to be able to use both products without having to fight the configuration.
This is still a problem with my .zshrc
I needs to be
export PATH="/Users/wesselfeenstra/Library/Application\ Support/Herd/bin/":$PATH instead of export PATH="/Users/wesselfeenstra/Library/Application Support/Herd/bin/":$PATH
Every time i restart my laptop it will add a second entry in my .zshrc
I now just wrap the misbehaving Herd stuff with a condition to disable it.
if false; then
# Herd injected NVM configuration
export NVM_DIR="/Users/mark/Library/Application Support/Herd/config/nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[[ -f "/Applications/Herd.app/Contents/Resources/config/shell/zshrc.zsh" ]] && builtin source "/Applications/Herd.app/Contents/Resources/config/shell/zshrc.zsh"
fi
Herd will think it's fine and not keep re-injecting it, but it won't do anything, and you can handle your config yourself.
I'm still running into this issue with the latest version of Herd, anyone else?
Same
Just this morning, I checked my .bash_profile again and Herd had inserted the path modifier at least 10 times. The annoying thing is I use a custom version of PHP for general development and Herd keeps overwriting it, making me have to remove the unwanted lines from .bash_profile. Not sure why it's not detecting the existing Herd path modifier already in the file, which I moved to before my custom PHP installation path.
@jjroley in all our enthusiasm we missed this: