17

I installed rbenv through homebrew, and now I don't know why path_helper put ~/.rbenv/shims at the end of the path instead of the beginning. And most importantly, how did path_helper get this information?

According to the man page of path_helper, it reads entries from /etc/paths and from files in /etc/paths.d. But I cannot find the string ".rbenv/shims" there.

~% cat /etc/paths 
/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin
~% ls -la /etc/paths.d 
total 0
drwxr-xr-x    2 root  wheel    68 Jun 21 03:16 .
drwxr-xr-x  107 root  wheel  3638 Sep 10 09:59 ..
~% /usr/libexec/path_helper
PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/gordon/.rbenv/shims"; export PATH;

2 Answers 2

14

I suspect that your .bash_profile or .bashrc is adding .rbenv/shims to your PATH, and that is running at some point before path_helper is invoked during the shell start-up.

The man page for path_helper opens with:

 The path_helper utility reads the contents of the files in the directo-
 ries /etc/paths.d and /etc/manpaths.d and appends their contents to the
 PATH and MANPATH environment variables respectively.

The crucial point here is that the path_helper utility is intended to add contents to an existing PATH setting, not replace them. (And in actuality, what it really does is prepend contents, not append them, which matters for PATH variables...)

So, if I start out with an entry on my PATH, the setting generated by path_helper will ensure that entry continues on the PATH it generates.

% echo $SHELL
/bin/bash
% uname
Darwin
% /usr/libexec/path_helper 
PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"; export PATH;
% PATH="" /usr/libexec/path_helper 
PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"; export PATH;
% PATH=foo /usr/libexec/path_helper 
PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:foo"; export PATH;

Note that foo has been included in my PATH in the last line, even though the contents of /etc/paths and /etc/paths.d/* have not changed.

At the same time, the path_helper utility also seems to be careful not to produce paths with duplicate entries; it removes duplicate entries after concatenating /etc/paths and /etc/paths.d/* and the current PATH.

This latter detail can be especially confusing since it can cause entry reorderings compared to the original PATH setting (!).

Below are some examples of this behavior: The first case shows a duplicate foo being removed. The second and third case illustrate entry reordering: the generated PATH is the same in both cases, but in the third case, the /usr/bin entry has been moved from in-between foo and bar to the front of the PATH. (This duplicate-entry removal seems to be based on just simple string-matching on the pairs of entries, as illustrated by the fourth case below where the string /usr/bin/ remains between foo/ and bar.)

% PATH=foo:foo /usr/libexec/path_helper 
PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:foo"; export PATH;
% PATH=foo:bar /usr/libexec/path_helper 
PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:foo:bar"; export PATH;
% PATH=foo:/usr/bin:bar /usr/libexec/path_helper 
PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:foo:bar"; export PATH;
% PATH=foo/:/usr/bin/:bar /usr/libexec/path_helper 
PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:foo/:/usr/bin/:bar"; export PATH;

Finally, to give credit where credit is due: While all of the command sequences above are the result of my own investigations, I was originally inspired to look into the behavior of path_helper after reading the note here, which pointed out that path_helper reuses the PATH environment variable set by the parent process.

1
  • 1
    Thanks for pointing out that, contra the docs, path_helper prepends paths and removes duplicates.
    – algal
    Commented Jul 29, 2013 at 12:38
13

The path_helper man page is incorrect. The man page says path_helper is appending /etc/paths.d onto the PATH. But actually, path_helper is APPENDing /etc/paths.d onto the list from /etc/paths and then effectively PREPENDing that result onto actual pre-existing PATH (as well as purging PATH of overridden duplicates from that list).

To be exact, speaking only of PATH for instance, what it does is:

  1. read the list of paths in the file /etc/paths
  2. APPEND onto it the lists of paths in the files in the directory /etc/paths.d
  3. mutate the PATH variable to remove any items in the list
  4. APPEND onto the list the value of the mutated PATH variable
  5. Save this list as the new PATH

To rephrase this in pseudocode, what it's doing is:

  1. NEWPATH = Read(/etc/paths)
  2. NEWPATH = $NEWPATH +append+ Read(/etc/paths.d/*)
  3. PATH = $PATH -minus- $NEWPATH
  4. NEWPATH = $NEWPATH +append+ $PATH
  5. PATH = $NEWPATH

What's treacherous about this is that, if you are manually configuring your PATH, it's probably in order to add components that override system path components. If path_helper gets called when you don't expect it, then it will undo your changes by putting the system-wide path components ahead of your path components.

How would path helper get called unexpectedly? For instance, it gets called by /etc/zshenv, which is called every time you start a zsh shell, whether it's a subshell, or a zsh instance called from another app like emacs, or whatever.

I've written it in more detail as an OpenRadar bug report on path_helper.

2
  • Here's an attempt to improve on it, if anyone feels like using it / checking it over github.com/yb66/path_helper
    – ian
    Commented Mar 9, 2017 at 22:35
  • 5
    In OS El Capitan, path_helper is called in /etc/zprofile rather than /etc/zshenv, which I'd argue is worse. At least /etc/zshenv was the first thing sourced, so you could override the PATH for all shells (interactive, non-interactive) in ~/.zshenv. But since /etc/zprofile is sourced after ~/.zshenv, if you want the same PATH for interactive and non-interactive shells you're forced to repeat the modifications made in ~/.zshenv in ~/.zprofile.
    – rampion
    Commented Sep 14, 2017 at 19:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.