Skip to content

Commit

Permalink
Prevent unnecessary application reloads in development
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
aeroastro committed Jan 11, 2024
1 parent c6ef898 commit d7e2575
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
8 changes: 8 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
* Prevent unnecessary application reloads in development.

Previously, some files outside autoload paths triggered unnecessary reloads.
With this fix, application reloads according to `Rails.autoloaders.main.dirs`,
thereby preventing unnecessary reloads.

*Takumasa Ochi*

* Use `oven-sh/setup-bun` in GitHub CI when generating an app with bun

*TangRufus*
Expand Down
4 changes: 2 additions & 2 deletions railties/lib/rails/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,8 @@ def routes_reloader # :nodoc:
def watchable_args # :nodoc:
files, dirs = config.watchable_files.dup, config.watchable_dirs.dup

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

[files, dirs]
Expand Down
20 changes: 18 additions & 2 deletions railties/test/application/watcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def app
@app ||= Rails.application
end

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

files, _ = Rails.application.watchable_args
assert_includes files, "#{rails_root}/app/README.md"
assert_not_includes files, "#{rails_root}/app/README.md"
end

test "watchable_args does include dirs in autoload path" do
add_to_config <<-RUBY
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
config.autoload_paths += %W(#{rails_root}/manually-specified-path)
RUBY
app_dir "app/automatically-specified-path"
app_dir "manually-specified-path"

require "#{rails_root}/config/environment"

_, dirs = Rails.application.watchable_args

assert_includes dirs, "#{rails_root}/app/automatically-specified-path"
assert_includes dirs, "#{rails_root}/manually-specified-path"
end
end
end

0 comments on commit d7e2575

Please sign in to comment.