Spring gem loaded in wrong environment
Asked Answered
M

3

5

Why does my spring gem load in the wrong (or all) environment(s)?

I have this in my Gemfile and spring gem is not listed anywhere else in the file:

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

When I ran bundle exec rails console test (for the test environment), spring processes started and the Listen module was loaded in the rails console. I made sure all spring processes were stopped beforehand.

To do a sanity check, I removed the whole development group above and bundled. Spring and listen gems were no longer loaded, as I expected.

Maryettamaryjane answered 21/2, 2018 at 22:8 Comment(2)
Is it possible that there is a RAILS_ENV variable initialized? Also what version of bundler are you using?Spraggins
bundle version 1.15.4. No, RAILS_ENV is not set. Although, I've tried RAILS_ENV=test bundle exec rails console with same issue. From rails console, I also verified Rails.env and Gem::Specification.all_names (list of loaded gems)Maryettamaryjane
S
5

I faced this misunderstanding in production.

Here's how I solved it:

You can also fix this issue permanently by upspringing (removing the spring gem from) your bin/ executables:

bin/spring binstub --remove --all

Or

spring binstub --remove --all

You can now run the command below to get into rails console in production

rails c --environment=production

Also, to avoid this from occurring in subsequent occasions endeavour to make sure that the spring gem is only present in development and test groups in your Gemfile.

Moreso, when you're in production make sure you always provide the --without development test argument to the bundle install command

bundle install --without development test

and not the usual or common

bundle install

Please note: As an indication, whenever you run the command rails c or rails console and you see the output below:

Running via Spring preloader in process 26651 WARNING: Spring is running in production. To fix this make sure the spring gem is only present in development and test groups in your Gemfile and make sure you always use bundle install --without development test in production

It's an indication that the spring gem is running in your production environment, and it should be stopped or removed entirely from your bin executables.

That's all.

I hope this helps

Spectatress answered 30/1, 2020 at 16:14 Comment(0)
O
1

Spring is generally used through binstubs - did you install the binstubs? If so this is the file your rails command is running through.

#!/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'

As you can see it will load spring anytime you use the rails command. There is no check for the environment. If you do not want to load spring you can use DISABLE_SPRING=1 rails c test.

Ochone answered 23/2, 2018 at 0:12 Comment(2)
I'm running rails through bundle exec rails, not through the binstubs I don't think. Regardless, it's scary that spring is overriding the rule of the Gemfile environment group.Maryettamaryjane
Adding that begin/rescue/end fixed it for meSplatter
M
0

According the spring gem github page, it looks like rails console will load up spring:

rails console, rails generate, rails runner

These execute the rails command you already know and love. If you run a different sub command (e.g. rails server) then spring will automatically pass it through to the underlying rails executable (without the speed-up).

Also, this is worrying:

You must not install Spring on your production environment. To prevent it from being installed, provide the --without development test argument to the bundle install command

rails console (development) would make sense, but not rails console test (or another environment). It seems buggy to me, and a reason now why I don't like the gem.

Maryettamaryjane answered 22/2, 2018 at 23:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.