如何在没有 `bundle exec` 的情况下为 rails 进行 rubocop?

How to rubocop for rails without `bundle exec`?

我在 ruby 2.7.2 上有一个 rails 应用 运行ning,在 Gemfile 中有以下内容:

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

有运行bundle installbundle update。每当我在目录中 运行 rubocop 时,我都会得到以下信息:

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

如果我运行 bundle exec rubocop,一切正常。如果没有 bundle exec 部分,我怎样才能完成这项工作?我 运行 vim 中的一个 linter,它只调用没有 bundle exec 的可执行文件,所以我被卡住了。

有趣的是,我在 ruby 2.7.2 上有一个不同的应用程序,在 Gemfile 中具有相同的设置,并且它在没有前缀 bundle exec.

的情况下工作正常

bundle exec 是负责 运行 一个确定的 gem 在你的 Gemfile(具有特定版本)

中的命令

否则,您将 运行 您的系统的 rubocop(即另一个版本)并且您的项目也不会 运行

我相信 Rubocop 的行为符合预期,问题在于您的 linter 如何调用 Rubocop。

由于 Rubocop 是通过 Bundler 安装的,因此需要在其前面加上 bundle exec 才能 运行 本地版本。您需要告诉插件 运行 bundle exec rubocop 命令而不是 rubocop.

您没有提到您正在使用哪个 Vim linter,但是以选择流行的 ALE 选项为例,您可以使用以下设置来覆盖默认调用的命令:

g:ale_ruby_rubocop_executable = 'bundle'

ALE 将此设置隐藏在 Ruby documentation, and more information is documented in this GitHub issue.

如果您使用的是其他 linting 工具,我会深入研究其文档以查看是否存在类似的配置。否则,您可能需要自己在插件的源代码中进行更改。