ruby-on-railsrubyrubocop

How to rubocop for rails without `bundle exec`?


I have a rails app running on ruby 2.7.2 with the following in Gemfile:

group :development, :test do
  gem 'rubocop'
  gem 'rubocop-minitest'
  gem 'rubocop-performance'
  gem 'rubocop-rails'
end

Have ran bundle install and bundle update. Anytime I run rubocop in the directory, I get the following:

Could not find 'activesupport' (>= 4.2.0) among 220 total gem(s)

If I run bundle exec rubocop, all works fine. How can I make this work without the bundle exec part? I run a linter in vim that just calls the executable without the bundle exec and so I'm stuck.

Interestingly enough, I have a different app on ruby 2.7.2 with the same setup in the Gemfile, and it works fine without prefixing bundle exec.


Solution

  • I believe that Rubocop is behaving as expected, and that the issue lies in how your linter is invoking Rubocop.

    Since Rubocop is installed via Bundler, it will need to be prepended with bundle exec in order to run the local version. You'll need to tell the plugin to run the bundle exec rubocop command rather than rubocop.

    You didn't mention which Vim linter you're using, but choosing the popular ALE option as an example, you can use the following setting to override the default invoked command:

    g:ale_ruby_rubocop_executable = 'bundle'
    

    ALE has this setting tucked in the Ruby documentation, and more information is documented in this GitHub issue.

    If you're using a different linting tool, I'd dive into its documentation to see if a similar config exists. Otherwise you may need to make the change yourself in the plugin's source.