Skip to content

Commit ba9d63a

Browse files
authored
Merge pull request #662 from rails/rm-new-format-binstub
Modernize spring binstubs and disable it in production
2 parents a81ef6e + 02649d9 commit ba9d63a

File tree

2 files changed

+81
-27
lines changed

2 files changed

+81
-27
lines changed

Diff for: lib/spring/client/binstub.rb

+21-26
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,36 @@ class Binstub < Command
1111
# client is not invoked for whatever reason, then the Kernel.exit won't
1212
# happen, and so we'll fall back to the lines after this block, which
1313
# should cause the "unsprung" version of the command to run.
14-
LOADER = <<CODE
15-
begin
16-
load File.expand_path('../spring', __FILE__)
17-
rescue LoadError => e
18-
raise unless e.message.include?('spring')
19-
end
20-
CODE
14+
LOADER = <<~CODE
15+
load File.expand_path("spring", __dir__)
16+
CODE
2117

2218
# The defined? check ensures these lines don't execute when we load the
2319
# binstub from the application process. Which means that in the application
2420
# process we'll execute the lines which come after the LOADER block, which
2521
# is what we want.
26-
SPRING = <<'CODE'
27-
#!/usr/bin/env ruby
28-
29-
# This file loads Spring without using Bundler, in order to be fast.
30-
# It gets overwritten when you run the `spring binstub` command.
31-
32-
unless defined?(Spring)
33-
require 'rubygems'
34-
require 'bundler'
35-
36-
lockfile = Bundler::LockfileParser.new(Bundler.default_lockfile.read)
37-
spring = lockfile.specs.detect { |spec| spec.name == 'spring' }
38-
if spring
39-
Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path
40-
gem 'spring', spring.version
41-
require 'spring/binstub'
42-
end
43-
end
44-
CODE
22+
SPRING = <<~CODE
23+
#!/usr/bin/env ruby
24+
25+
# This file loads Spring without using loading other gems in the Gemfile, in order to be fast.
26+
# It gets overwritten when you run the `spring binstub` command.
27+
28+
if !defined?(Spring) && [nil, "development", "test"].include?(ENV["RAILS_ENV"])
29+
require "bundler"
30+
31+
Bundler.locked_gems.specs.find { |spec| spec.name == "spring" }&.tap do |spring|
32+
Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path
33+
gem "spring", spring.version
34+
require "spring/binstub"
35+
end
36+
end
37+
CODE
4538

4639
OLD_BINSTUB = %{if !Process.respond_to?(:fork) || Gem::Specification.find_all_by_name("spring").empty?}
4740

4841
BINSTUB_VARIATIONS = Regexp.union [
42+
%{load File.expand_path("spring", __dir__)\n},
43+
%{begin\n load File.expand_path('../spring', __FILE__)\nrescue LoadError => e\n raise unless e.message.include?('spring')\nend\n},
4944
%{begin\n load File.expand_path('../spring', __FILE__)\nrescue LoadError\nend\n},
5045
%{begin\n spring_bin_path = File.expand_path('../spring', __FILE__)\n load spring_bin_path\nrescue LoadError => e\n raise unless e.message.end_with? spring_bin_path, 'spring/binstub'\nend\n},
5146
LOADER

Diff for: test/support/acceptance_test.rb

+60-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def exec_name
281281
test "binstub when spring binary is missing" do
282282
begin
283283
File.rename(app.path("bin/spring"), app.path("bin/spring.bak"))
284-
assert_success "bin/rake -T", stdout: "rake db:migrate"
284+
assert_failure "bin/rake -T", stderr: "`load': cannot load such file"
285285
ensure
286286
File.rename(app.path("bin/spring.bak"), app.path("bin/spring"))
287287
end
@@ -407,6 +407,65 @@ def exec_name
407407

408408
assert_success "bin/spring binstub rake", stdout: "bin/rake: upgraded"
409409
assert_equal expected, app.path("bin/rake").read
410+
411+
# newer variation which checks end of exception message using include
412+
File.write(app.path("bin/rake"), <<-RUBY.strip_heredoc)
413+
#!/usr/bin/env ruby
414+
begin
415+
load File.expand_path('../spring', __FILE__)
416+
rescue LoadError => e
417+
raise unless e.message.include?('spring')
418+
end
419+
require 'bundler/setup'
420+
load Gem.bin_path('rake', 'rake')
421+
RUBY
422+
423+
assert_success "bin/spring binstub rake", stdout: "bin/rake: upgraded"
424+
assert_equal expected, app.path("bin/rake").read
425+
end
426+
427+
test "binstub remove with new binstub variations which checks end of the exception message using include" do
428+
# newer variation which checks end of exception message using include
429+
File.write(app.path("bin/rake"), <<-RUBY.strip_heredoc)
430+
#!/usr/bin/env ruby
431+
begin
432+
load File.expand_path('../spring', __FILE__)
433+
rescue LoadError => e
434+
raise unless e.message.include?('spring')
435+
end
436+
require 'bundler/setup'
437+
load Gem.bin_path('rake', 'rake')
438+
RUBY
439+
440+
File.write(app.path("bin/rails"), <<-RUBY.strip_heredoc)
441+
#!/usr/bin/env ruby
442+
begin
443+
load File.expand_path('../spring', __FILE__)
444+
rescue LoadError => e
445+
raise unless e.message.include?('spring')
446+
end
447+
APP_PATH = File.expand_path('../../config/application', __FILE__)
448+
require_relative '../config/boot'
449+
require 'rails/commands'
450+
RUBY
451+
452+
assert_success "bin/spring binstub --remove rake", stdout: "bin/rake: Spring removed"
453+
assert_success "bin/spring binstub --remove rails", stdout: "bin/rails: Spring removed"
454+
455+
expected = <<-RUBY.strip_heredoc
456+
#!/usr/bin/env ruby
457+
require 'bundler/setup'
458+
load Gem.bin_path('rake', 'rake')
459+
RUBY
460+
assert_equal expected, app.path("bin/rake").read
461+
462+
expected = <<-RUBY.strip_heredoc
463+
#!/usr/bin/env ruby
464+
APP_PATH = File.expand_path('../../config/application', __FILE__)
465+
require_relative '../config/boot'
466+
require 'rails/commands'
467+
RUBY
468+
assert_equal expected, app.path("bin/rails").read
410469
end
411470

412471
test "binstub remove with new binstub variations" do

0 commit comments

Comments
 (0)