Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calling a different rbenv ruby using backticks from a rake task running via rbenv errors #776

Closed
theirishpenguin opened this issue Sep 3, 2015 · 3 comments

Comments

@theirishpenguin
Copy link

Overview

Given we have ruby and 2.2.2 and jruby-9.0.0.0 both installed via rbenv
When we call a rake task using RBENV_VERSION=2.2.2
And that rake task calls a jruby script using backticks
Then we get the error...

Could not find rake-10.4.2 in any of the sources
Run `bundle install` to install missing gems.

Example app

Example rails app at https://github.com/theirishpenguin/rails_app_calling_jruby_script which features an example task that showcases this ( https://github.com/theirishpenguin/rails_app_calling_jruby_script/blob/master/lib/tasks/demo_task.rake)

Steps to reproduce using demo app

  • Install ruby 2.2.2 and 2.2.2 and jruby-9.0.0.0 using rbenv
  • git clone git@github.com:theirishpenguin/rails_app_calling_jruby_script.git
  • cd rails_app_calling_jruby_script
  • RBENV_VERSION=2.2.2 bundle install
  • RBENV_VERSION=2.2.2 bundle exec rake demo_task:demo
  • We get the error

Now try without rbenv when calling the rake task

... # Ensure you are still in the rails_app_calling_jruby_script directory

  • bundle install
  • bundle exec rake demo_task:demo
  • We get the success message "*** GOT HERE ***"

Also

  • I've tried a few variants on invoking the script (with rbenv init calls that mimic the .bashrc sourcing) but to no avail
  • I was thinking it might be possible to clear/replace some of the ENV variables temporarily before shelling out to the jruby script and then restore them afterwards but I don't understand this well enough to be sure it would work
@theirishpenguin
Copy link
Author

If, just prior to shelling out to jruby, you delete some env variables as follows...

ENV.delete("GEM_HOME")                                                      
ENV.delete("GEM_PATH")                                                      
ENV.delete 'BUNDLE_BIN_PATH'                                                
ENV.delete 'BUNDLE_GEMFILE'                                                 
ENV.delete 'RUBYOPT'

... and modify the RBENV_VERSION to point at jruby...

ENV['RBENV_VERSION'] = 'jruby-9.0.0.0'

... and modify ENV["RUBYLIB"] and ENV['PATH'] to be the same as your jruby environment then we can reach the success message.

Will look into how to replace this ENV variables temporarily in order to launch the jruby subscript.

@theirishpenguin
Copy link
Author

Finally, a working solution. Here is an example rake task where the environment will be jruby-based within the with_jruby_env block...

require 'active_record'                                                         

namespace :foo do                                                        
  desc 'Invokes bar based on the current RAILS_ENV.'          
  task :bar => :environment do                                        

    with_jruby_env('jruby-9.0.0.0') do                                          
      puts `jruby -S ./my_jruby_script.rb`
    end                                                                         

  end                                                                           

  # Thanks to the this article for all the env switching foo :-)                
  # https://www.fedux.org/articles/2015/07/22/using-env-in-mri-ruby-and-jruby.html
  def with_jruby_env(jruby_version, &block)                                     
    old_env = ENV.to_hash                                                       

    # Delete undesired variables                                                
    ENV.delete 'GEM_HOME'                                                      
    ENV.delete 'GEM_PATH'                                                      
    ENV.delete 'BUNDLE_BIN_PATH'                                                
    ENV.delete 'BUNDLE_GEMFILE'                                                 
    ENV.delete 'RUBYOPT'                                                        

    # Update incorrect variables                                                
    ENV['RBENV_VERSION'] = jruby_version                                        
    ENV["RUBYLIB"] = "#{ENV['HOME']}/.rbenv/rbenv.d/exec/gem-rehash:"              

    # Change path to use jruby                                                  
    ENV['PATH'] = "#{ENV['HOME']}/.rbenv/versions/#{ENV['RBENV_VERSION']}/bin/:" +
      ENV['PATH'].split(':').reject{|dir| dir.include? '.rbenv/versions'}.join(':')

    block.call if block_given?                                                  
  ensure                                                                        
    ENV.clear                                                                   
    ENV.update old_env                                                          
  end                                                                           

end

@mislav
Copy link
Member

mislav commented Sep 11, 2015

Thanks for sharing the solution. This is a known problem. Please see #121

@mislav mislav closed this as completed Sep 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants