From f4935f84e7cb21b25f7f5b0e70c2fbdff30616ac Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Wed, 5 Oct 2022 11:17:29 +1100 Subject: [PATCH] Run without error if spring in uninstalled group We have spring in the development and test groups which means that it's present in the Gemfile but not installed on production. We are also not using the RAILS_ENV variable when logging in as developer and start a console with a parameter: ./bin/rails console -e production In this case, a LoadError was raised when trying to load spring. So since checking the RAILS_ENV is not a reliable detection of spring being installed, we added a rescue call. But looking into this issue further, I discovered that we can go through the list of actually requested gems and skip spring if it wasn't requested. This solves our specific case. A rescue statement may still be useful if other people have other reasons for the gem not being installed but I don't know if that's possible. --- CHANGELOG.md | 2 ++ lib/spring/client/binstub.rb | 2 +- test/support/acceptance_test.rb | 12 ++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c3dbadb..25a866cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -55,6 +55,8 @@ with. * Fix a small compatibility issue with Ruby 3.2 causing `Kernel#raise` to not accept a `cause`. +* Skip spring without error if spring is not in installed bundler groups. + ## 4.1.0 * Fix bug which makes commands to freeze when the Rails application is writing to STDERR. diff --git a/lib/spring/client/binstub.rb b/lib/spring/client/binstub.rb index 10e13999..b393b230 100644 --- a/lib/spring/client/binstub.rb +++ b/lib/spring/client/binstub.rb @@ -26,7 +26,7 @@ class Binstub < Command if !defined?(Spring) && [nil, "development", "test"].include?(ENV["RAILS_ENV"]) require "bundler" - Bundler.locked_gems.specs.find { |spec| spec.name == "spring" }&.tap do |spring| + Bundler.definition.requested_specs.find { |spec| spec.name == "spring" }&.tap do |spring| Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path gem "spring", spring.version require "spring/binstub" diff --git a/test/support/acceptance_test.rb b/test/support/acceptance_test.rb index d491996d..4c263fdf 100644 --- a/test/support/acceptance_test.rb +++ b/test/support/acceptance_test.rb @@ -76,9 +76,12 @@ def assert_speedup(ratio = DEFAULT_SPEEDUP) def without_gem(name) gem_home = app.gem_home.join('gems') + spec_home = app.gem_home.join('specifications') FileUtils.mv(gem_home.join(name), app.root) + FileUtils.mv(spec_home.join("#{name}.gemspec"), app.root) yield ensure + FileUtils.mv(app.root.join("#{name}.gemspec"), spec_home) FileUtils.mv(app.root.join(name), gem_home) end @@ -309,6 +312,15 @@ def exec_name end end + test "binstub when spring gem is in uninstalled group" do + without_gem "spring-#{Spring::VERSION}" do + File.write(app.gemfile, app.gemfile.read.gsub(/(gem 'spring.*)/, '\1, group: :debug')) + app.run! "bundle config set without debug" + app.run! "bundle install", timeout: 300 + assert_success "bin/rake -T", stdout: "rake db:migrate" + end + end + test "binstub when spring binary is missing" do begin File.rename(app.path("bin/spring"), app.path("bin/spring.bak"))