简体   繁体   中英

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.

The bundle exec is the command responsible to run a determinated gem inside your Gemfile (that has a specific version)

Without this, you'll run your system's rubocop (that is another version) and your project will not run as well

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM