rails / spring

Rails application preloader
MIT License
2.81k stars 341 forks source link

Spring binstubs do not respect the --environment option. #729

Open b-nik opened 1 month ago

b-nik commented 1 month ago

Up until recently, the binstubs generated by spring were using exception handling to decide whether to load Spring or not. This worked ok, and correctly didn't load Spring on production when rails was booting.

Recently ( here: https://github.com/rails/spring/commit/02649d97ff1dfdb1eacfae32cfb33380acfa59f9 ) a better approach was used, basically just checking the environment along with whether Spring is defined. However, since the --environment argument is checked after Rails boots, it's unfortunately not respected with this version.

Which means, commands like "bundle exec bin/rails console -e production" will produce a LoadError on production, since Spring is not in the bundler group for production, and since RAILS_ENV will be nil at the point the check is made.

I came across this issue by noticing my whenever created cronjobs not working, and that's exactly the reason why.

PS. To fix my case, I just added this not-super-fancy piece of code in my bin/spring file.

env_idx = ARGV.find_index{ |v| v == "-e" || v == "--environment" }
if env_idx
  env = ARGV[env_idx + 1]
  ENV["RAILS_ENV"] = env
end