Skip to content

Commit 6c60916

Browse files
andrewn617gmcgibbon
andcommitted
Add embedded engine initializers to spring watcher
This will allow embedded engine initializers to reload the application when changed. Previously, the server would have to be reloaded manually. Co-Authored-By: Gannon McGibbon <[email protected]>
1 parent c5987d5 commit 6c60916

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/spring/application.rb

+5
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ def preload
124124

125125
if defined?(Rails) && Rails.application
126126
watcher.add Rails.application.paths["config/initializers"]
127+
Rails::Engine.descendants.each do |engine|
128+
if engine.root.to_s.start_with?(Rails.root.to_s)
129+
watcher.add engine.paths["config/initializers"].expanded
130+
end
131+
end
127132
watcher.add Rails.application.paths["config/database"]
128133
if secrets_path = Rails.application.paths["config/secrets"]
129134
watcher.add secrets_path

test/support/acceptance_test.rb

+25
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,31 @@ def exec_name
715715
test "rails db:system:change" do
716716
assert_success "bin/rails db:system:change --to=sqlite3"
717717
end
718+
719+
test "watches embedded engine initializers" do
720+
app.path("config/application.rb").write(<<~RUBY, mode: "a+")
721+
722+
$LOAD_PATH << Pathname.new(__dir__).join("../engine/lib").realpath.to_s
723+
require "my_engine"
724+
RUBY
725+
engine_lib = app.path("engine/lib/my_engine.rb")
726+
engine_lib.dirname.mkpath
727+
engine_lib.write(<<~RUBY)
728+
require "rails/engine"
729+
730+
class MyEngine < Rails::Engine
731+
end
732+
RUBY
733+
engine_initializer = app.path("engine/config/initializers/one.rb")
734+
engine_initializer.dirname.mkpath
735+
engine_initializer.write("")
736+
737+
assert_success app.spring_test_command
738+
739+
engine_initializer.write("raise 'omg'")
740+
741+
assert_failure app.spring_test_command, stderr: "omg (RuntimeError)"
742+
end
718743
end
719744
end
720745
end

0 commit comments

Comments
 (0)