Monday, March 25, 2013

RVM v1.x's .rvmrc set -e Workaround

Note: the following was the case before, but please see the update at the bottom.

So, I learned a fun fact about RVM v1.x today which is that if you put "set -e" in your .rvmrc, it will exit out of the terminal session when you try to backout of the directory via "cd ..". This will be fixed in RVM v2.

If what you are trying to do is stop executing the .rvmrc unless RVM switched the ruby and gemset correctly, you can just check it, e.g.:

rvm use ruby-1.9.3@awesome --create

# we'll make sure that it switched before installing things
if [ "$(~/.rvm/bin/rvm-prompt i v g)" == "ruby-1.9.3@awesome" ] ; then
    # other things here
fi

Note: I'd make "ruby-1.9.3@awesome" a variable, but IDea/Rubymine currently expects a certain format, so that wouldn't work.

Update: 2013-12-05. This no longer appears to be the case even with RVM 1.x. Just ensure you have the latest version. See below...

$ rvm -v

rvm 1.23.3 (stable) by Wayne E. Seguin , Michal Papis  [https://rvm.io/]

$ bash -version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin11)
Copyright (C) 2007 Free Software Foundation, Inc.

$ cat my.sh
#!/bin/bash
source ~/.rvm/scripts/rvm
echo "so we loaded rvm"
set -e
cd ~/some/path/my_app/
rake db:migrate
cd ..
echo "ha!"
this_is_not_a_real_command
echo "this should not be shown"

$ ./my.sh
so we loaded rvm
You are using '.rvmrc', it requires trusting, it is slower and it is not compatible with other ruby managers,
you can switch to '.ruby-version' using 'rvm rvmrc to [.]ruby-version'
or ignore this warning with 'rvm rvmrc warning ignore /path/to/some/path/.rvmrc',
'.rvmrc' will continue to be the default project file in RVM 1 and RVM 2,
to ignore the warning for all files run 'rvm rvmrc warning ignore all.rvmrcs'.

Using /path/to/.rvm/gems/ruby-2.0.0-p247 with gemset my_app
...
ha!
./my.sh: line 8: this_is_not_a_real_command: command not found
$ 

And, to only load rvm if it is there:

if test -f ~/.rvm/scripts/rvm; then
  [ "$(type -t rvm)" = "function" ] || source ~/.rvm/scripts/rvm
fi

No comments: