【问题标题】:Spring gem loaded in wrong environmentSpring gem 在错误的环境中加载
【发布时间】:2018-08-01 15:34:30
【问题描述】:

为什么我的 spring gem 在错误(或所有)环境中加载?

我的 Gemfile 中有这个,而 spring gem 没有在文件中的其他任何地方列出:

group :development do
  gem 'listen', '~> 3.1.5'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

当我运行bundle exec rails console test(对于test 环境)时,spring 进程启动并且Listen 模块被加载到rails 控制台中。我确保事先停止了所有的 spring 进程。

为了进行完整性检查,我删除了上面的整个开发组并捆绑在一起。正如我所料,不再加载 Spring 和 Listen gem。

【问题讨论】:

  • 是否有可能初始化了RAILS_ENV 变量?另外你使用的是什么版本的捆绑器?
  • 捆绑版本 1.15.4。不,未设置 RAILS_ENV。虽然,我试过RAILS_ENV=test bundle exec rails console 有同样的问题。在 Rails 控制台中,我还验证了 Rails.envGem::Specification.all_names(加载的 gem 列表)

标签: ruby-on-rails gemfile spring-gem


【解决方案1】:

根据spring gem github page,看起来rails console 将加载弹簧:

rails 控制台,rails 生成,rails runner

这些执行您已经知道和喜爱的 rails 命令。如果你跑 一个不同的子命令(例如rails服务器)然后spring会 自动将其传递给底层的 rails 可执行文件 (没有加速)。

另外,这令人担忧:

您不得在生产环境中安装 Spring。到 防止它被安装,提供 --without development bundle install 命令的测试参数

rails console(开发)是有意义的,但不是rails console test(或其他环境)。对我来说,这似乎是错误的,这也是我不喜欢 gem 的一个原因。

【讨论】:

    【解决方案2】:

    Spring 通常通过 binstubs 使用 - 您是否安装了 binstubs?如果是这样,这就是您的rails 命令正在运行的文件。

    #!/usr/bin/env ruby
    begin
      load File.expand_path('../spring', __FILE__)
    rescue LoadError => e
      raise unless e.message.include?('spring')
    end
    APP_PATH = File.expand_path('../config/application', __dir__)
    require_relative '../config/boot'
    require 'rails/commands'
    

    如您所见,它会在您使用rails 命令的任何时候加载spring。没有检查环境。如果不想加载spring,可以使用DISABLE_SPRING=1 rails c test

    【讨论】:

    • 我正在运行 railsbundle exec rails,而不是通过我不认为的 binstubs。不管怎样,spring 压倒了 Gemfile 环境组的规则,这很可怕。
    【解决方案3】:

    我在生产中遇到了这种误解。

    我是这样解决的:

    您还可以永久通过弹出(从中删除 spring gem)bin/ 可执行文件来解决此问题:

    bin/spring binstub --remove --all
    

    或者

    spring binstub --remove --all
    

    您现在可以运行以下命令进入生产环境中的 Rails 控制台

    rails c --environment=production
    

    此外,为避免这种情况在后续场合发生,请努力确保 spring gem 仅出现在 Gemfile 中的 developmenttest 组中。

    此外,当您在生产中时,请确保始终为 bundle install 命令提供 --without development test 参数

    bundle install --without development test
    

    而不是通常或常见的

    bundle install
    

    请注意:作为指示,每当您运行命令 rails crails console 时,您都会看到以下输出:

    在进程 26651 中通过 Spring 预加载器运行 警告:Spring 正在生产中运行。要解决此问题,请确保 spring gem 仅存在于您的 Gemfile 中的 developmenttest 组中,并确保您始终在生产中使用 bundle install --without development test

    这表明 spring gem 正在您的生产环境中运行,应该停止或从您的 bin 可执行文件中完全删除它。

    就是这样。

    我希望这会有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-15
      • 1970-01-01
      • 2011-05-09
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 2016-06-08
      相关资源
      最近更新 更多