AtomLinter / linter-rubocop

Linter plugin for Ruby, using rubocop
MIT License
85 stars 53 forks source link

Run rubocop with different ruby version #21

Open carlosbaraza opened 9 years ago

carlosbaraza commented 9 years ago

Hi,

In my particular case, at work we are maintaining a big rails application developed in Ruby 1.8.7. However, we want to follow the recommendations from Rubocop (which does not support 1.8.7, but 1.9.3). We have some rules configured for our 1.8.7 version.

The problem is that in atom I was receiving this issue message (Obtained from the Linter debug option):

stderr /Users/baraza/.rvm/rubies/ree-1.8.7-2012.02/lib/ruby/site_ruby/1.8/rubygems.rb:900:in `report_activate_error': Could not find RubyGem rubocop (>= 0) (Gem::LoadError)
    from /Users/baraza/.rvm/rubies/ree-1.8.7-2012.02/lib/ruby/site_ruby/1.8/rubygems.rb:248:in `activate'
    from /Users/baraza/.rvm/rubies/ree-1.8.7-2012.02/lib/ruby/site_ruby/1.8/rubygems.rb:1276:in `gem'
    from /Users/baraza/.rvm/gems/ruby-1.9.3-p551/bin/rubocop:22
    from /Users/baraza/.rvm/gems/ree-1.8.7-2012.02/bin/ruby_executable_hooks:15

Then I decided to set the command (cmd) to execute directly on the linter-rubocop.coffee file. And also, not set up the rubocopExecutablePath config option.

cmd: '/Users/baraza/.rvm/rubies/ruby-1.9.3-p551/bin/ruby /Users/baraza/.rvm/gems/ruby-1.9.3-p551/bin/rubocop --format emacs'

However, this will bring another problem. The GEM_PATH environmental variable is not set:

stderr /Users/baraza/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:315:in `to_specs': Could not find 'rubocop' (>= 0) among 151 total gem(s) (Gem::LoadError)
Checked in 'GEM_PATH=/Users/baraza/.rvm/gems/ree-1.8.7-2012.02:/Users/baraza/.rvm/gems/ree-1.8.7-2012.02@global', execute `gem env` for more information
    from /Users/baraza/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/site_ruby/1.9.1/rubygems/dependency.rb:324:in `to_spec'
    from /Users/baraza/.rvm/rubies/ruby-1.9.3-p551/lib/ruby/site_ruby/1.9.1/rubygems/core_ext/kernel_gem.rb:64:in `gem'
    from /Users/baraza/.rvm/gems/ruby-1.9.3-p551/bin/rubocop:22:in `<main>'

Finally, I solved this issue in the init.coffee adding this line to the activate function.

  activate: ->
    console.log 'activate linter-rubocop'
    process.env['GEM_PATH'] = ['/Users/baraza/.rvm/gems/ruby-1.9.3-p551', process.env['GEM_PATH']].join(':')

Now, I would like to maybe create a fork and solve this issue in a more elegant way. Does anybody has a recommendation on how to proceed with this? I thought about creating an optional config variable for the GEM_PATH, Ruby and Rubocop.

Thank you all for this great plugin. Carlos.

thibaudgg commented 9 years ago

GEM_PATH is wrongly set for me too, would be a nice feature!

dcarral commented 9 years ago

Pointing from the executable path to a small script using rvm underneath did the trick for me ;)

I mean something like this (with your ruby version of choice)

bronson commented 8 years ago

FWIW I installed rubocop and reek on 1.9.3 like this:

gem install rubocop -v 0.41.2
gem install unparser -v 0.2.4
gem install reek -v 2.2.1

That doesn't help on 1.8.7 of course... Just figured I'd mention it here so I'll find it when I'm googling for this two years from now.

chrishough commented 7 years ago

Has anyone found a fix for this?

quolpr commented 6 years ago

@chrishough I have created new file .atom/rubocop with chmod +x:

#!/bin/bash
source ~/.rvm/scripts/rvm
rvm use --your_ruby_version--@--your_gemset(if you use)-- &>/dev/null
rubocop $@

Don't forget to replace your_ruby_version and your_gemset. If you don't use gemset, just use rvm use --your_ruby_version-- &>/dev/null

ngan commented 6 years ago

rvm-exec bundle exec rubocop worked for me

chrishough commented 6 years ago

We are using rbenv @quolpr @ngan, thoughts?

ngan commented 6 years ago

Looks like rbenv has their own exec: https://github.com/rbenv/rbenv/blob/master/libexec/rbenv-exec

chrishough commented 6 years ago

Thank you @ngan

vzamanillo commented 4 years ago

This will be implemented on #411