MacOS – .rvmrc and mac osx lion “restore” issue

macosterminal

Just switched to lion a couple of days back, and juts noticed that when you quit Terminal while in a folder, when you open it, it should be back in that folder(along with your other tabs).

This is a good feature indeed, but the problem is, it doesn't "refresh" your paths. meaning even if you had a .rvmrc file in your app folder, it won't set the ruby and gemset again. you'll have to actually cd out and get back in again, which defeats the purpose of having an rvmrc.

Is this a known bug and are there an fixes/patches for it already?

EDIT
Following Chris Page's advice, I added cd "$PWD" in the last line of my .bashrc (which is loaded by my .bash_profile)

This in turn "loads" the rvmrc as expected :

Last login: Tue Aug  9 11:29:09 on ttys003
Using /Users/eumir/.rvm/gems/ruby-1.9.2-p180 with gemset mygemset

eumir$ gem list
/Users/eumir/.rvm/rubies/ruby-1.9.2-p180/bin/gem:12:in `require': no such file to load -- rubygems (LoadError)
from /Users/eumir/.rvm/rubies/ruby-1.9.2-p180/bin/gem:12

eumir$ ruby -v
ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin10.2.0]

As you can see, it did load the gemset(printed out by "Using /Users…") but trying to do gem list and showing the ruby version says that it still didn't load it. (the ruby version should be 1.9.2)

EDIT 2

I updated my rvm and now any of the fixes aren't working!

It prints out that it IS using the gemset in my rvm but when I do a gem env, i get this:

eumir$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 1.3.6
  - RUBY VERSION: 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0]
  - INSTALLATION DIRECTORY: /Users/eumir/.rvm/gems/ruby-1.8.7-p174@awetest
  - RUBY EXECUTABLE: /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
  - EXECUTABLE DIRECTORY: /Users/eumir/.rvm/gems/ruby-1.8.7-p174@awetest/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - universal-darwin-11
  - GEM PATHS:
     - /Users/eumir/.rvm/gems/ruby-1.8.7-p174@mygemset
     - /Users/eumir/.rvm/gems/ruby-1.8.7-p174@global
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
     - :sources => ["http://rubygems.org/", "http://gemcutter.org"]
     - "gem" => "--no-ri --no-rdoc"
  - REMOTE SOURCES:
     - http://rubygems.org/
     - http://gemcutter.org

doing cd ../ and then cd myfolder and then gem env results with this:

eumir$ gem env
RubyGems Environment:
  - RUBYGEMS VERSION: 1.7.2
  - RUBY VERSION: 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin10.4.0]
  - INSTALLATION DIRECTORY: /Users/eumir/.rvm/gems/ruby-1.8.7-p174@awetest
  - RUBY EXECUTABLE: /Users/eumir/.rvm/rubies/ruby-1.8.7-p174/bin/ruby
  - EXECUTABLE DIRECTORY: /Users/eumir/.rvm/gems/ruby-1.8.7-p174@awetest/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-darwin-10
  - GEM PATHS:
     - /Users/eumir/.rvm/gems/ruby-1.8.7-p174@mygemset
     - /Users/eumir/.rvm/gems/ruby-1.8.7-p174@global
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
     - :sources => ["http://rubygems.org/", "http://gemcutter.org"]
     - "gem" => "--no-ri --no-rdoc"
  - REMOTE SOURCES:
     - http://rubygems.org/
     - http://gemcutter.org

which is the correct environment. As you can see, the gemset was loaded correctly(along with the gems) but it is still using the system ruby instead of the rvmrc ruby.

Best Answer

Try upgrading your installation of RVM if you are running something older than 1.6.10 (check your active version with rvm --version).

In some shell:

rvm get latest

Then, either restart your other, existing shells, or run rvm reload in them.

Note: Another problem the OP faced was PATH manipulation after RVM was loaded into the shell. This created the “right gemset selected, but wrong installation of ruby” effect that disappeared after “cd out, cd in” (since RVM would have reasserted its position in the PATH at that point). When a shell is started in a directory that contains an .rvmrc it is vital that RVM be the last thing to prepend elements to the PATH (at least any PATH elements that might contain a ruby (etc.) so that the Ruby installation selected in the .rvmrc will be effective).


RVM arranges to source .rvmrc files by “hooking” your shell’s cd command (a cd shell function in bash, an entry in chpwd_functions for zsh). Normally this lets it catch any “normal” cd command that you type it or run via a sourced script after loading RVM.

I do not have Lion to test with, but I suspect the way it works is that Terminal forks, calls chdir(2), then execs your shell to establish your new shell’s initial working directory. This means that the directory is already established by the time RVM is loaded in your shell initialization file.

The history seems a bit muddy, but RVM has had some support for sourcing the .rvmrc file in the shell’s initial working directory when it is first loaded. It looks like maybe it was intended to be screen-specific, but in RVM commit cb649ba (Always load the .rvmrc if in an interactive shell (open new tab in same dir)., 2011-05-14) relaxed the requirement so that it should source .rvmrc files for any interactive shell when RVM is first loaded. This commit was first present in the 1.6.10 release of RVM. The code has changed a bit since then, but it still works in (more or less) the same way in the latest versions.