rbenv: The master branch of rbenv breaks inside of tmux.

When using the master branch version of rbenv, tmux no longer respects rbenv. For example I have global ruby setup to 2.0.0-p0 and when I run rbenv version it correctly reports 2.0.0-p0, it is also correctly reported when I run ruby --version. Now in the same session I start a new tmux session using tmux new. Now when I run rbenv version it still correctly reports 2.0.0-p0, however when I run ruby --version it now reports 1.8.7. When I run which ruby it shows me /usr/bin/ruby instead of the ruby shim.

About this issue

  • Original URL
  • State: closed
  • Created 11 years ago
  • Comments: 29 (10 by maintainers)

Commits related to this issue

Most upvoted comments

After a couple of hours of tinkering, the optimal way to solve this (without disabling path_helper entirely) is to make the following patch to /etc/zshenv:

--- /etc/zshenv.orig
+++ /etc/zshenv
@@ -1,4 +1,5 @@
 # system-wide environment settings for zsh(1)
 if [ -x /usr/libexec/path_helper ]; then
+ PATH=""
  eval `/usr/libexec/path_helper -s`
 fi

The underlying problem is that all your shell configuration files are loaded twice when you create a tmux session/window. So the following happens:

  1. /etc/zshenv is loaded, which runs /usr/libexec/path_helper, which sets your PATH variable.
  2. Your own configuration scripts, which load rbenv, which puts the shims directory at the front of your PATH.
  3. You run tmux.
  4. path_helper runs again, putting stuff before your shims directory.
  5. Your configuration scripts run again, and rbenv sees that the shims directory already exists in your PATH, and does nothing.

This patch ensures that your shell configuration is loaded starting from a blank path. This has two benefits:

  1. You won’t end up with duplicate paths and an even messier PATH variable.
  2. At step 5, rbenv will see that the shims directory doesn’t exist in your PATH yet, and load it properly.

@mislav Thanks, good point.

I think I found a fix that works now. I’m running bash, and I wanted a solution that worked only for my current user (no messing with /etc/profile), so I ended up with a ~/.bash_profile which looks like this:

if [ -f /etc/profile ]; then
    PATH=""
    source /etc/profile
fi
. ~/.profile
. ~/.bashrc

Now my $PATH is the same with or without tmux.

via http://superuser.com/a/583502

EDIT: Don’t do this. It sorts alphabetically, which you probably don’t want.

Running tmux + bash on OS X 10.9 with this in my ~/.profile:

PATH=`echo $PATH | tr ':' '\n' | sort | uniq | tr '\n' ':'`

via http://linuxg.net/oneliners-for-removing-the-duplicates-in-your-path/

It seems like tmux will always call the profile for your shell, not just the rc (I’m using bash, and it’s definitely calling /etc/profile, which also has a call to path_helper).

What I’m doing to fix it is changing /etc/profile to this:

if [[ -z $TMUX ]] && [ -x /usr/libexec/path_helper ]; then
        eval `/usr/libexec/path_helper -s`
fi

instead of

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