Ruby on rails Spring gem在错误的环境中加载

Ruby on rails Spring gem在错误的环境中加载,ruby-on-rails,gemfile,spring-gem,Ruby On Rails,Gemfile,Spring Gem,为什么我的spring gem在错误(或全部)环境中加载 我的gem文件中有这个,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

为什么我的spring gem在错误(或全部)环境中加载

我的gem文件中有这个,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控制台测试时(对于
test
环境),spring进程启动,并且
Listen
模块加载到rails控制台中。我确保所有spring进程都已提前停止

为了进行合理性检查,我删除了上面的整个开发组并将其捆绑。Spring和listen gems不再像我预期的那样加载。

根据,看起来
rails控制台将加载Spring:

rails控制台、rails生成、rails运行程序

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

此外,这令人担忧:

不能在生产环境中安装Spring。到 防止安装,提供--而不进行开发 bundle install命令的测试参数


rails控制台
(开发)是有意义的,但不是
rails控制台测试
(或其他环境)。对我来说,这似乎是个问题,也是我现在不喜欢gem的原因。

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\u spring=1 rails c test

我在生产中遇到了这个误解

我是这样解决的:

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

bin/spring binstub --remove --all

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

rails c --environment=production
此外,为了避免在以后的情况下发生这种情况,请努力确保gem仅出现在gem文件中的
开发
测试
组中

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

bundle install --without development test
而不是通常的或普通的

bundle install
请注意:当您运行命令
rails c
rails console
时,您会看到以下输出:

通过过程26651中的弹簧预紧器运行 警告:Spring正在生产中运行。要解决此问题,请确保spring gem仅存在于Gemfile中的
开发
测试
组中,并确保在生产中始终使用
捆绑安装--而不使用开发测试

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

就这些


我希望这有帮助

是否有可能初始化了
RAILS\u ENV
变量?另外,您使用的bundler版本是什么?bundle版本1.15.4。否,未设置RAILS_ENV。尽管如此,我还是用同样的问题尝试了
RAILS\u ENV=testbundle execrails console
。从rails控制台,我还验证了
rails.env
Gem::Specification。我运行的
rails
是通过
bundle exec-rails
,而不是我不认为是通过binstubs运行的。无论如何,spring正在覆盖Gemfile环境组的规则,这很可怕。