Skip to content

Commit d7e2575

Browse files
committed
Prevent unnecessary application reloads in development
Previously, the Rails application would reload due to changes in some files outside the autoload paths. For instance, editing `app/README.md` would trigger a reload, even though the reloaded classes and modules were identical to those loaded previously. This commit fixes this issue by ensuring the application reloads correctly according to `Rails.autoloaders.main.dirs`, thereby preventing unnecessary reloads. rails#37011 (comment)
1 parent c6ef898 commit d7e2575

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

railties/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
* Prevent unnecessary application reloads in development.
2+
3+
Previously, some files outside autoload paths triggered unnecessary reloads.
4+
With this fix, application reloads according to `Rails.autoloaders.main.dirs`,
5+
thereby preventing unnecessary reloads.
6+
7+
*Takumasa Ochi*
8+
19
* Use `oven-sh/setup-bun` in GitHub CI when generating an app with bun
210

311
*TangRufus*

railties/lib/rails/application.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,8 @@ def routes_reloader # :nodoc:
412412
def watchable_args # :nodoc:
413413
files, dirs = config.watchable_files.dup, config.watchable_dirs.dup
414414

415-
ActiveSupport::Dependencies.autoload_paths.each do |path|
416-
File.file?(path) ? files << path.to_s : dirs[path.to_s] = [:rb]
415+
Rails.autoloaders.main.dirs.each do |path|
416+
dirs[path.to_s] = [:rb]
417417
end
418418

419419
[files, dirs]

railties/test/application/watcher_test.rb

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def app
1313
@app ||= Rails.application
1414
end
1515

16-
test "watchable_args classifies files included in autoload path" do
16+
test "watchable_args does NOT include files in autoload path" do
1717
add_to_config <<-RUBY
1818
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
1919
RUBY
@@ -22,7 +22,23 @@ def app
2222
require "#{rails_root}/config/environment"
2323

2424
files, _ = Rails.application.watchable_args
25-
assert_includes files, "#{rails_root}/app/README.md"
25+
assert_not_includes files, "#{rails_root}/app/README.md"
26+
end
27+
28+
test "watchable_args does include dirs in autoload path" do
29+
add_to_config <<-RUBY
30+
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
31+
config.autoload_paths += %W(#{rails_root}/manually-specified-path)
32+
RUBY
33+
app_dir "app/automatically-specified-path"
34+
app_dir "manually-specified-path"
35+
36+
require "#{rails_root}/config/environment"
37+
38+
_, dirs = Rails.application.watchable_args
39+
40+
assert_includes dirs, "#{rails_root}/app/automatically-specified-path"
41+
assert_includes dirs, "#{rails_root}/manually-specified-path"
2642
end
2743
end
2844
end

0 commit comments

Comments
 (0)