Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Automatic ruby switching #120

Closed
colszowka opened this Issue · 6 comments

2 participants

@colszowka

I have chruby installed system-wide, including the auto.sh in my /etc/profile.d/chruby.sh. Now, I have a user called rubyuser. Consider his home dir structure:

- /home/rubyuser
  - .ruby-version # => 1.9.3
  - 187/
    - .ruby-version # => 1.8.7

When executing su - rubyuser -c "ruby -v ; cd 187 ; ruby -v" as root, I'd expect the following:

ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-linux]
ruby 1.8.7 (2012-10-12 patchlevel 371) [x86_64-linux]

Instead, I get my system ruby, indicating that the auto-switch via PROMPT_COMMAND is not triggered.

ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]
ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]

When I execute su - rubyuser -c "chruby_auto ; ruby -v ; cd 187 ; chruby_auto ; ruby -v", I get the expected result. When I add chruby_auto to my /etc/profile.d/chruby.sh, at least the initial switch to 1.9.3 when switching the user is executed.

I'm not sure whether I'm maybe missing something here, but it would be great to have this work "out of the box" when auto mode is enabled. At least maybe auto.sh could call chruby_auto on load to ensure that a top level .ruby-version is picked up?

This would be very useful for making sure the correct ruby is picked up when running services.

@postmodern
Owner

Could you run su - rubyuser -c "./scripts/bug_report.sh" and post the diagnostic information.
https://github.com/postmodern/chruby/blob/master/scripts/bug_report.sh

@colszowka

It's just a pretty straight-forward vagrant box on Ubuntu 12.04.1. Here's the output of the bug_report script after running it via su

root@vagrant-ubuntu:~# su - rubyapp -c "./bug_report.sh"

## System

    Linux vagrant-ubuntu 3.2.0-32-generic #51-Ubuntu SMP Wed Sep 26 21:33:09 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux
    GNU bash, version 4.2.24(1)-release (x86_64-pc-linux-gnu) (/bin/bash)
    [exited] (/usr/bin/tmux)
    ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux] (/usr/bin/ruby)

## Environment

    CHRUBY_VERSION=0.3.4
    SHELL=/bin/bash
    PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
    PROMPT_COMMAND=chruby_auto
    RUBIES=(/opt/rubies/1.8.7-p371 /opt/rubies/1.9.3-p392 /opt/rubies/2.0.0-p0 /opt/rubies/rbx-2.0.0-rc1)

## .ruby-version

    1.9.3-p392

## Aliases

    alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
    alias egrep='egrep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias grep='grep --color=auto'
    alias l='ls -CF'
    alias la='ls -A'
    alias ll='ls -alF'
    alias ls='ls --color=auto'

When logged in as that user and then running bug report, there's some more environment variables related to the then auto-selected ruby 1.9.3, everything else is the same.

@postmodern
Owner

This is due to the fact that bash only executes PROMPT_COMMAND when in interactive mode. A workaround would be to override cd when not in interactive mode; but that would only switch Rubies after cd is invoked.

@colszowka

Yeah, I figured this must be due to bash not invoking that, I wonder though why not when I actually use a login shell. Probably it needs a proper terminal emulator to actually call that variable. I wonder if there is some other environment var that chruby_auto could be placed into which would make this work. Although the current way is definetely workable, I would love to have the absolutely braindead way I suggested without overwriting cd (which actually is a bit of a hack as well, since you need to actually cd somewhere for it to work, somewhat similar problem to prompt_command :), but not sure if it is even possible at all :)

@postmodern
Owner

This might be fixed in 0.3.5. I switched from using PROMPT_COMMAND (which only runs in interactive mode) to using trap DEBUG which runs in both interactive / non-interactive mode (but not apparently in functions).

@postmodern postmodern closed this
@postmodern
Owner

Also, you should be using the -l option with su, so that chruby gets loaded:

su - hal -l -c "chruby; echo 'changing directory ...'; cd /vault/1/code/ronin/ronin; chruby"
   jruby-1.7.3
   jruby-1.7.4
   rubinius-2.0.0-rc1
   ruby-1.8.7-p371
   ruby-1.9.3-p392
   ruby-2.0.0-p0
changing directory ...
   jruby-1.7.3
   jruby-1.7.4
   rubinius-2.0.0-rc1
   ruby-1.8.7-p371
 * ruby-1.9.3-p392
   ruby-2.0.0-p0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.