Auto-switching not working #209

Open
mattschultz opened this Issue Oct 6, 2013 · 32 comments

Comments

Projects
None yet
9 participants

Hey everyone,

I'm using chruby installed via Homebrew with ruby-build. I installed Ruby 2.0.0-p247 to /opt/rubies/2.0.0-p247. I can switch to it fine manually, but the auto.sh script is not working when I cd into a directory with a .ruby-version file.

.ruby-version:

2.0.0-p247

.bashrc:

export PS1='\w $ '
homebrew=/usr/local/bin:/usr/local/sbin
export PATH=$homebrew:$PATH

source /usr/local/opt/chruby/share/chruby/chruby.sh
source /usr/local/opt/chruby/share/chruby/auto.sh

.bash_profile

if [ -f ~/.bashrc ]; then
  source ~/.bashrc
fi
Owner

postmodern commented Oct 6, 2013

Can you provide an example of how it's not working?

If I cd into a directory with the above .ruby-version file, it doesn't change to the version specified:

~ $ ruby -v
ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]
~ $ cd ~/code/test/
~/code/test $ ruby -v
ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]

Manually changing works fine:

~ $ ruby -v
ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]
~ $ chruby 2.0
~ $ ruby -v
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.5.0]
Owner

postmodern commented Oct 6, 2013

Are you sure you are not using any other plugins that might also use trap DEBUG to hook every command?

I don't believe so. This is a fresh install of Mountain Lion. The first thing I did was install Homebrew, then git, then chruby.

Not working for me, either on the Mavericks GM

Owner

postmodern commented Oct 9, 2013

Could you run scripts/bug_report.sh and post the output here?

@postmodern Here's the output, with my aliases removed

System

Darwin Nicbook.local 13.0.0 Darwin Kernel Version 13.0.0: Thu Sep 19 22:22:27 PDT 2013; root:xnu-2422.1.72~6/RELEASE_X86_64 x86_64
GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13) (/bin/bash)
zsh 5.0.2 (x86_64-apple-darwin13.0) (/bin/zsh)
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin13.0.0] (/Users/nico/.rubies/ruby-2.0.0-p247/bin/ruby)
Bundler version 1.3.5 (/Users/nico/.gem/ruby/2.0.0/bin/bundle)

Environment

CHRUBY_VERSION=0.3.7
SHELL=/bin/bash
PATH=.bundle/binstubs:/Users/nico/.gem/ruby/2.0.0/bin:/Users/nico/.rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/bin:/Users/nico/.rubies/ruby-2.0.0-p247/bin:/usr/local/share/npm/bin:/usr/local/bin:/usr/local/sbin:/Users/nico/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin:.bundle/binstubs:/usr/local/share/npm/bin:/usr/local/sbin:/Users/nico/bin
HOME=/Users/nico
RUBIES=(/Users/nico/.rubies/jruby-1.7.4
RUBY_ROOT=/Users/nico/.rubies/ruby-2.0.0-p247
RUBY_VERSION=2.0.0
RUBY_ENGINE=ruby
GEM_ROOT=/Users/nico/.rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0
GEM_HOME=/Users/nico/.gem/ruby/2.0.0
GEM_PATH=/Users/nico/.gem/ruby/2.0.0:/Users/nico/.rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0
Owner

postmodern commented Oct 9, 2013

Oops, forgot to output trap -p. Try the new script.

System

Darwin Nicbook.local 13.0.0 Darwin Kernel Version 13.0.0: Thu Sep 19 22:22:27 PDT 2013; root:xnu-2422.1.72~6/RELEASE_X86_64 x86_64
GNU bash, version 3.2.51(1)-release (x86_64-apple-darwin13) (/bin/bash)
zsh 5.0.2 (x86_64-apple-darwin13.0) (/bin/zsh)
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin13.0.0] (/Users/nico/.rubies/ruby-2.0.0-p247/bin/ruby)
Bundler version 1.3.5 (/Users/nico/.gem/ruby/2.0.0/bin/bundle)

Environment

CHRUBY_VERSION=0.3.7
SHELL=/bin/bash
PATH=.bundle/binstubs:/Users/nico/.gem/ruby/2.0.0/bin:/Users/nico/.rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0/bin:/Users/nico/.rubies/ruby-2.0.0-p247/bin:/usr/local/share/npm/bin:/usr/local/bin:/usr/local/sbin:/Users/nico/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/texbin:.bundle/binstubs:/usr/local/share/npm/bin:/usr/local/sbin:/Users/nico/bin
HOME=/Users/nico
RUBIES=(/Users/nico/.rubies/jruby-1.7.4
RUBY_ROOT=/Users/nico/.rubies/ruby-2.0.0-p247
RUBY_VERSION=2.0.0
RUBY_ENGINE=ruby
GEM_ROOT=/Users/nico/.rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0
GEM_HOME=/Users/nico/.gem/ruby/2.0.0
GEM_PATH=/Users/nico/.gem/ruby/2.0.0:/Users/nico/.rubies/ruby-2.0.0-p247/lib/ruby/gems/2.0.0

Hooks

trap -- '[[ "$BASH_COMMAND" != "$PROMPT_COMMAND" ]] && chruby_auto' DEBUG
Owner

postmodern commented Oct 9, 2013

Now I'm curious what the value of $RUBY_AUTO_VERSION is before you enter the project, while you are inside the project, and after you leave the project.

it's null

@postmodern The before value of $RUBY_AUTO_VERSION was ruby-2.0, which is my default ruby. The after value is the same.

However, when I am accessing a project in a TrueCrypt container, the before value was again, ruby-2.0, and the after value is blank.

Owner

postmodern commented Oct 11, 2013

@nicosuave I suspect auto.sh is unable to find the .ruby-version file for some reason. That or the contents of .ruby-version already matches the current $RUBY_AUTO_VERSION.

@postmodern It seems to be the former, without a doubt. I'll do some digging, and see if I can figure anything out.

Owner

postmodern commented Oct 12, 2013

@nicosuave note that if $RUBY_AUTO_VERSION already matches .ruby-version, chruby_auto will short-circuit and continue using the currently selected Ruby. https://github.com/postmodern/chruby/blob/master/share/chruby/auto.sh#L8

@postmodern $RUBY_AUTO_VERSION doesn't match .ruby-version, to clarify.

I'm having the same problem (also on OS X). This commit seems to have introduced the issue: 4f51459

Reverting auto.sh fixes the issue for me. Line 7 of the current version is always evaluating as false.

Owner

postmodern commented Oct 24, 2013

@handerson odd, what shell/version are you using?

GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12)

I think I'm seeing a similar thing since upgrading to 0.3.7, in that chruby-auto insists on me using my system ruby (which I need to have installed on my Ubuntu 13.04 to use the heroku toolbelt, meh).

Any update on this? I'm having the same issues as above.

After looking at this more closely, this seems to be a tmux issue. When I cd into the directory, auto-switching works as expected. But if I open a new tmux window from that directory, auto-switching fails. This is because of $RUBY_AUTO_VERSION and .ruby-version short circuit. I removed that check in auto.sh to chruby "$version" regardless of $RUBY_AUTO_VERSION. Not optimal, but it works for now.

Owner

postmodern commented Dec 4, 2013

@trestrantham shouldn't tmux preserve the current PATH (which chruby has auto-modified) when spawning a new window?

Owner

postmodern commented Dec 4, 2013

@trestrantham also we stopped exporting RUBY_AUTO_VERSION in 0.3.7, so it shouldn't be inherited by new tmux windows.

Interesting. I'll have to take another closer look to see what's going on then. I was stabbing in the dark and removing the short circuit check solved the problem.

Not a tmux issue for me, I'm running this on a straight-forward Ubuntu bash shell. The upgrade to 0.3.8 did not fix it, either. This also happens on a vanilla vagrant Ubuntu 12.04 x64 (https://cloud-images.ubuntu.com/vagrant/precise/current/) box when I install some rubies. It insists on the system ruby even if .ruby-version is present.

It would be super-useful to have this resolved as it is terribly annoying :) Not sure what is causing it, but I'd be willing to provide a demo Vagrantfile with simple shell-based provisioning that demonstrates the problem?

Ok, did a bit more digging. Actually this breaks because I enable auto-switching via a /etc/profile.d/chruby-auto.sh, instead of the .bashrc. If I add the sourcing of auto.sh to my bashrc, it works again.

I actually do want the auto-switching to be used globally for all users though, and this used to work up until 0.3.6, so I would be very grateful if this functionality could return :)

Owner

postmodern commented Dec 10, 2013

@colszowka that is odd, since I have chruby 0.3.8 installed, configured in /etc/profile.d/chruby.sh and running bash 4.2.45. Could you provide more information about how it doesn't work? Is the chruby_auto function even loaded? What does trap -p show? You can also run this handy bug_report.sh script.

iamvery commented Jul 8, 2014

I recently switch from zsh to bash, and I've come across an issue that seems to be related to the above discussion. If I change to a directory containing a .ruby-version file and then start tmux, the tmux session has RUBY_AUTO_VERSION set as expected, but the current ruby version does not match.

I realized there is a tricky order dependency in when you set your default ruby and source the auto script. I fixed it in my env.

@iamvery I've experienced this issue too. Thanks for your workaround!

iamvery commented Aug 3, 2015

💯

Thanks @iamvery -- your fix worked for me too! :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment