chruby icon indicating copy to clipboard operation
chruby copied to clipboard

Auto-switching not working

Open mattschultz opened this issue 12 years ago • 32 comments

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

mattschultz avatar Oct 06 '13 03:10 mattschultz

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

postmodern avatar Oct 06 '13 03:10 postmodern

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]

mattschultz avatar Oct 06 '13 03:10 mattschultz

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

postmodern avatar Oct 06 '13 05:10 postmodern

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.

mattschultz avatar Oct 06 '13 05:10 mattschultz

Not working for me, either on the Mavericks GM

nicosuave avatar Oct 09 '13 17:10 nicosuave

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

postmodern avatar Oct 09 '13 22:10 postmodern

@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

nicosuave avatar Oct 09 '13 22:10 nicosuave

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

postmodern avatar Oct 09 '13 23:10 postmodern

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

nicosuave avatar Oct 09 '13 23:10 nicosuave

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.

postmodern avatar Oct 09 '13 23:10 postmodern

it's null

nicosuave avatar Oct 10 '13 17:10 nicosuave

@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.

nicosuave avatar Oct 11 '13 18:10 nicosuave

@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 avatar Oct 11 '13 23:10 postmodern

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

nicosuave avatar Oct 12 '13 00:10 nicosuave

@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 avatar Oct 12 '13 01:10 postmodern

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

nicosuave avatar Oct 14 '13 17:10 nicosuave

I'm having the same problem (also on OS X). This commit seems to have introduced the issue: https://github.com/postmodern/chruby/commit/4f51459c5995d85a234155e9faa6838337ce491c

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

handerson avatar Oct 24 '13 01:10 handerson

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

postmodern avatar Oct 24 '13 03:10 postmodern

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

handerson avatar Oct 24 '13 13:10 handerson

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).

colszowka avatar Dec 02 '13 08:12 colszowka

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

trestrantham avatar Dec 04 '13 15:12 trestrantham

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.

trestrantham avatar Dec 04 '13 15:12 trestrantham

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

postmodern avatar Dec 04 '13 20:12 postmodern

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

postmodern avatar Dec 04 '13 20:12 postmodern

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.

trestrantham avatar Dec 04 '13 21:12 trestrantham

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?

colszowka avatar Dec 10 '13 10:12 colszowka

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 :)

colszowka avatar Dec 10 '13 11:12 colszowka

@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.

postmodern avatar Dec 10 '13 22:12 postmodern

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 avatar Jul 08 '14 19:07 iamvery

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

bjoernalbers avatar Aug 03 '15 15:08 bjoernalbers

:100:

iamvery avatar Aug 03 '15 19:08 iamvery

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

marklatham avatar Jul 06 '16 22:07 marklatham