postmodern / chruby

Changes the current Ruby
MIT License
2.87k stars 189 forks source link

Auto-switching not working #209

Open mattschultz opened 11 years ago

mattschultz commented 11 years ago

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
postmodern commented 11 years ago

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

mattschultz commented 11 years ago

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]
postmodern commented 11 years ago

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

mattschultz commented 11 years ago

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.

nicosuave commented 11 years ago

Not working for me, either on the Mavericks GM

postmodern commented 11 years ago

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

nicosuave commented 11 years ago

@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
postmodern commented 11 years ago

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

nicosuave commented 11 years ago

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
postmodern commented 11 years ago

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.

nicosuave commented 11 years ago

it's null

nicosuave commented 11 years ago

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

postmodern commented 11 years ago

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

nicosuave commented 11 years ago

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

postmodern commented 11 years ago

@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

nicosuave commented 11 years ago

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

handerson commented 10 years ago

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.

postmodern commented 10 years ago

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

handerson commented 10 years ago
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12)
colszowka commented 10 years ago

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

trestrantham commented 10 years ago

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

trestrantham commented 10 years ago

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.

postmodern commented 10 years ago

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

postmodern commented 10 years ago

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

trestrantham commented 10 years ago

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.

colszowka commented 10 years ago

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 commented 10 years ago

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

postmodern commented 10 years ago

@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 10 years ago

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.

bjoernalbers commented 9 years ago

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

iamvery commented 9 years ago

:100:

marklatham commented 8 years ago

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