【问题标题】:How to rubocop for rails without `bundle exec`?如何在没有`bundle exec`的情况下对rails进行rubocop?
【发布时间】:2020-11-05 18:09:33
【问题描述】:

我有一个在 ruby​​ 2.7.2 上运行的 rails 应用程序,在 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. 的情况下正常工作

【问题讨论】:

  • alias rubocop="bundle exec rubocop"?
  • 签出binstubs
  • 我试过bundle binstubs bundler --force,这是让它工作所必需的,我现在可以做bin/rubocop,但这仍然没有让我到达我想要的地方,即直接使用rubocop。

标签: ruby-on-rails ruby rubocop


【解决方案1】:

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

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

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

g:ale_ruby_rubocop_executable = 'bundle'

ALE 将此设置隐藏在 Ruby documentation 中,更多信息记录在 this GitHub issue 中。

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

【讨论】:

    【解决方案2】:

    bundle exec 是负责在您的 Gemfile(具有特定版本)中运行确定的 gem 的命令

    没有这个,您将运行系统的 rubocop(即另一个版本)并且您的项目将无法正常运行

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-25
      • 1970-01-01
      • 2011-04-12
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 2014-08-28
      相关资源
      最近更新 更多