Search code examples
rubyrvmguard

How do I set correctly use RVM gemsets with the guard-shell plugin?


I am trying to run a Ruby script via guard-shell. This script requires the redcarpet gem. When the script runs, I get a "cannot load such file" error, indicating that the gemset I'm expecting will be used is in fact not being used.

Here are some relevant parameters:

  1. In the Guard app folder (where the Guardfile is):

    > ls -a
    .                .ruby-gemset   Gemfile     Guardfile
    ..      .ruby-version   Gemfile.lock    tmp1.txt
    > cat .ruby-*
    rails4
    2.1.2
    
  2. The gem seems to be there:

    > gem which redcarpet
    /Users/sameer/.rvm/gems/ruby-2.1.2@rails4/extensions/x86_64-darwin-13/2.1.0-static/redcarpet-3.1.2/redcarpet.bundle
    
  3. My Guardfile has watch(/(.*)/) {|m| `rvm list; rvm gemset list; gem which redcarpet` } which outputs:

    ERROR:  Can't find ruby library file or shared library redcarpet
    ERROR:  Can't find ruby library file or shared library redcarpet
    
    rvm rubies
    
    ruby-2.0.0-p353 [ x86_64 ]
    ruby-2.1.0-preview1 [ x86_64 ]
    ruby-2.1.1 [ x86_64 ]
    =* ruby-2.1.2 [ x86_64 ]
    
    # => - current
    # =* - current && default
    #  * - default
    
    gemsets for ruby-2.1.2 (found in /Users/sameer/.rvm/gems/ruby-2.1.2)
    (default)
    global
    => rails4
    

So what am I missing?


Solution

  • I get the same error you do, e.g.:

    ./my_prog.rb:1:in require': cannot load such file -- nokogiri (LoadError) from ./my_prog.rb:1:in'

    But if I put the gem that the ruby program requires in the Gemfile, then the ruby program executes without error. If I comment out the line in the Gemfile, then once again the error occurs. The gem is installed either way, but if it's not listed in the Gemfile, then guard-shell produces that error.

    My Guardfile:

    guard :shell do
      watch /.*/ do |m|
        puts 'hello'
        `ruby ./my_prog.rb` 
      end
    end
    

    My Gemfile:

    source 'https://rubygems.org'
    
    group :development do
      gem 'guard', '2.6.1'
      gem 'guard-shell', '0.6.1'
      gem 'nokogiri', '1.6.2.1'
    end
    

    my_prog.rb:

    require 'nokogiri'
    
    puts 'goodbye'
    

    Somewhat annoyingly, this is my output in the guard shell:

    hellouard(main)> 
    goodbye
    [1] guard(main)>