Skip to content

Commit eff7850

Browse files
authored
Merge pull request #652 from rails/rm-mechanism
Require applications to have reloading enabled in the managed environ…
2 parents 53d9e17 + 24cc80e commit eff7850

File tree

4 files changed

+31
-25
lines changed

4 files changed

+31
-25
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Next Release
22

3+
* Require applications to have reloading enabled in the managed environments.
4+
35
## 2.1.1
46

57
* Avoid -I rubylibdir with default-gem bundler

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ automatically add `./bin` to your `PATH` when you `cd` into your application.
6565
Simply create an `.envrc` file with the command `PATH_add bin` in your
6666
Rails directory.
6767

68+
### Enable reloading
69+
70+
Spring reloads application code, and therefore needs the application to have
71+
reloading enabled.
72+
73+
Please, make sure `config.cache_classes` is `false` in the environments that
74+
Spring manages. That setting is typically configured in
75+
`config/environments/*.rb`. In particular, make sure it is `false` for the
76+
`test` environment.
77+
6878
### Usage
6979

7080
For this walkthrough I've generated a new Rails application, and run
@@ -257,12 +267,10 @@ run through Spring, set the `DISABLE_SPRING` environment variable.
257267

258268
## Class reloading
259269

260-
Spring uses Rails' class reloading mechanism
261-
(`ActiveSupport::Dependencies`) to keep your code up to date between
262-
test runs. This is the same mechanism which allows you to see changes
263-
during development when you refresh the page. However, you may never
264-
have used this mechanism with your `test` environment before, and this
265-
can cause problems.
270+
Spring uses Rails' class reloading mechanism to keep your code up to date
271+
between test runs. This is the same mechanism which allows you to see changes
272+
during development when you refresh the page. However, you may never have used
273+
this mechanism with your `test` environment before, and this can cause problems.
266274

267275
It's important to realise that code reloading means that the constants
268276
in your application are *different objects* after files have changed:

lib/spring/application.rb

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -95,22 +95,17 @@ def preload
9595
raise "Spring only supports Rails >= 5.2.0"
9696
end
9797

98-
# config/environments/test.rb will have config.cache_classes = true. However
99-
# we want it to be false so that we can reload files. This is a hack to
100-
# override the effect of config.cache_classes = true. We can then actually
101-
# set config.cache_classes = false after loading the environment.
102-
Rails::Application.initializer :initialize_dependency_mechanism, group: :all do
103-
# Rails 7 dropped classic mode, and this setter does not exist anymore.
104-
if ActiveSupport::Dependencies.respond_to?(:mechanism=)
105-
ActiveSupport::Dependencies.mechanism = :load
98+
Rails::Application.initializer :ensure_reloading_is_enabled, group: :all do
99+
if Rails.application.config.cache_classes
100+
raise <<-MSG.strip_heredoc
101+
Spring reloads, and therefore needs the application to have reloading enabled.
102+
Please, set config.cache_classes to false in config/environments/#{Rails.env}.rb.
103+
MSG
106104
end
107105
end
108106

109107
require Spring.application_root_path.join("config", "environment")
110108

111-
@original_cache_classes = Rails.application.config.cache_classes
112-
Rails.application.config.cache_classes = false
113-
114109
disconnect_database
115110

116111
@preloaded = :success
@@ -195,14 +190,6 @@ def serve(client)
195190
# Load in the current env vars, except those which *were* changed when Spring started
196191
env.each { |k, v| ENV[k] ||= v }
197192

198-
# requiring is faster, so if config.cache_classes was true in
199-
# the environment's config file, then we can respect that from
200-
# here on as we no longer need constant reloading.
201-
if @original_cache_classes
202-
ActiveSupport::Dependencies.mechanism = :require
203-
Rails.application.config.cache_classes = true
204-
end
205-
206193
connect_database
207194
srand
208195

test/support/acceptance_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@ def without_gem(name)
121121
refute_output_includes "bin/rails runner ''", stderr: 'Running via Spring preloader in process'
122122
end
123123

124+
test "raises if config.cache_classes is true" do
125+
config_path = app.path("config/environments/development.rb")
126+
config = File.read(config_path)
127+
config.sub!(/config.cache_classes\s*=\s*false/, "config.cache_classes = true")
128+
File.write(config_path, config)
129+
130+
assert_failure "bin/rails runner 1", stderr: "Please, set config.cache_classes to false"
131+
end
132+
124133
test "test changes are picked up" do
125134
assert_speedup do
126135
assert_success app.spring_test_command, stdout: "0 failures"

0 commit comments

Comments
 (0)