Skip to content

Commit 153eae8

Browse files
authored
Merge pull request rails#49032 from rails/gen-config.autoload_lib
Generate config.autoload_lib(...) for new apps
2 parents 20af938 + b16adcd commit 153eae8

File tree

6 files changed

+53
-14
lines changed

6 files changed

+53
-14
lines changed

actionmailer/lib/action_mailer/railtie.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ class Railtie < Rails::Railtie # :nodoc:
7171
initializer "action_mailer.set_autoload_paths", before: :set_autoload_paths do |app|
7272
options = app.config.action_mailer
7373
app.config.paths["test/mailers/previews"].concat(options.preview_paths)
74+
75+
# Preview paths configuration needs a pass.
76+
#
77+
# config.paths is cached as soon as it is accessed. Therefore, mutating
78+
# paths["test/mailers/previews"] does not guarantee config.autoload_paths
79+
# is going to include them.
80+
#
81+
# If config.paths was accessed before, config.autoload_paths is going to
82+
# have whatever paths["test/mailers/previews"] had when cached.
83+
app.config.autoload_paths.concat(options.preview_paths)
7484
end
7585

7686
initializer "action_mailer.compile_config_methods" do

railties/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
* `config/application.rb` now includes
2+
3+
```ruby
4+
config.autoload_lib(ignore: %w(assets tasks))
5+
```
6+
7+
In practice, this means that new 7.1 applications autoload from `lib` out of the box.
8+
9+
*Xavier Noria*
10+
111
* Add an option to start rails console in sandbox mode by default
212

313
`sandbox_by_default` option is added to start rails console in sandbox

railties/lib/rails/engine/configuration.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ def paths
5050
paths.add "app/mailers", eager_load: true
5151
paths.add "app/views"
5252

53+
# If you add more lib subdirectories here that should not be managed
54+
# by the main autoloader, please update the config.autoload_lib call
55+
# in the template that generates config/application.rb accordingly.
5356
paths.add "lib", load_path: true
5457
paths.add "lib/assets", glob: "*"
5558
paths.add "lib/tasks", glob: "**/*.rake"

railties/lib/rails/generators/rails/app/templates/config/application.rb.tt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ module <%= app_const_base %>
1515
config.load_defaults Rails::VERSION::STRING.to_f
1616
<%- end -%>
1717

18+
# Please, see https://guides.rubyonrails.org/autoloading_and_reloading_constants.html#config-autoload-lib-ignore.
19+
config.autoload_lib(ignore: %w(assets tasks))
20+
1821
# Configuration for the application, engines, and railties goes here.
1922
#
2023
# These settings can be overridden in specific environments using the files

railties/test/application/configuration_test.rb

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2119,8 +2119,10 @@ def index
21192119
end
21202120
end
21212121

2122-
test "autoload paths will exclude the configured javascript_path" do
2123-
add_to_config "config.javascript_path = 'webpack'"
2122+
test "autoload paths do not include custom config.javascript_paths" do
2123+
# The config.javascript_path assignment has to be in place before
2124+
# config.paths is accessed, since their compilation uses the value.
2125+
add_to_top_of_config "config.javascript_path = 'webpack'"
21242126
app_dir("app/webpack")
21252127

21262128
app "development"
@@ -2148,10 +2150,10 @@ def index
21482150
assert_empty Rails.configuration.paths.load_paths - $LOAD_PATH
21492151
end
21502152

2151-
test "autoload paths are not added to $LOAD_PATH by default" do
2153+
test "autoload paths are not added to $LOAD_PATH by default, except for lib" do
21522154
app "development"
21532155

2154-
assert_empty ActiveSupport::Dependencies.autoload_paths & $LOAD_PATH
2156+
assert_equal ["#{app_path}/lib"], ActiveSupport::Dependencies.autoload_paths & $LOAD_PATH
21552157

21562158
# Precondition, ensure we are testing something next.
21572159
assert_not_empty Rails.configuration.paths.load_paths
@@ -2170,6 +2172,23 @@ def index
21702172
assert_includes $LOAD_PATH, lib
21712173
end
21722174

2175+
test "config.autoload_lib(...) is generated by default" do
2176+
app_file "lib/x.rb", "X = true"
2177+
app_file "lib/m/x.rb", "M::X = true"
2178+
app_file "lib/assets/x.rb", "Assets::X = true"
2179+
app_file "lib/tasks/x.rb", "Tasks::X = true"
2180+
2181+
app "development"
2182+
2183+
assert_includes Rails.application.config.autoload_paths, "#{app_path}/lib"
2184+
assert_includes Rails.application.config.eager_load_paths, "#{app_path}/lib"
2185+
2186+
assert X
2187+
assert M::X
2188+
assert_raises(NameError) { Assets }
2189+
assert_raises(NameError) { Tasks }
2190+
end
2191+
21732192
test "autoload paths can be set in the config file of the environment" do
21742193
app_dir "custom_autoload_path"
21752194
app_dir "custom_autoload_once_path"
@@ -2197,6 +2216,7 @@ def index
21972216
app_file "lib/tasks/x.rb", "Tasks::X = true"
21982217
app_file "lib/generators/x.rb", "Generators::X = true"
21992218

2219+
remove_from_config "config\\.#{method_name}.*"
22002220
add_to_config "config.#{method_name}(ignore: %w(tasks generators))"
22012221

22022222
app "development"
@@ -2215,6 +2235,7 @@ def index
22152235
app_file "lib/x.rb", "X = true"
22162236
app_file "lib/tasks/x.rb", "Tasks::X = true"
22172237

2238+
remove_from_config "config\\.#{method_name}.*"
22182239
add_to_config "config.#{method_name}(ignore: [])"
22192240

22202241
app "development"
@@ -2232,6 +2253,7 @@ def index
22322253
app_file "lib/x.rb", "X = true"
22332254
app_file "lib/tasks/x.rb", "Tasks::X = true"
22342255

2256+
remove_from_config "config\\.#{method_name}.*"
22352257
add_to_config "config.#{method_name}(ignore: 'tasks')"
22362258

22372259
app "development"
@@ -2249,6 +2271,7 @@ def index
22492271
app_file "lib/x.rb", "X = true"
22502272
app_file "lib/tasks/x.rb", "Tasks::X = true"
22512273

2274+
remove_from_config "config\\.#{method_name}.*"
22522275
add_to_config "config.#{method_name}(ignore: nil)"
22532276

22542277
app "development"

railties/test/application/zeitwerk_integration_test.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,27 +234,17 @@ module ZeitwerkIntegrationTestExtras; end
234234
$zeitwerk_integration_test_user = false
235235
app_file "app/models/user.rb", "class User; end; $zeitwerk_integration_test_user = true"
236236

237-
$zeitwerk_integration_test_lib = false
238-
app_dir "lib"
239-
app_file "lib/webhook_hacks.rb", "WebhookHacks = 1; $zeitwerk_integration_test_lib = true"
240-
241237
$zeitwerk_integration_test_extras = false
242238
app_dir "extras"
243239
app_file "extras/websocket_hacks.rb", "WebsocketHacks = 1; $zeitwerk_integration_test_extras = true"
244-
245-
add_to_config "config.autoload_paths << '#{app_path}/lib'"
246240
add_to_config "config.autoload_once_paths << '#{app_path}/extras'"
247241

248242
boot("production")
249243

250244
assert $zeitwerk_integration_test_user
251-
assert_not $zeitwerk_integration_test_lib
252245
assert_not $zeitwerk_integration_test_extras
253246

254-
assert WebhookHacks
255247
assert WebsocketHacks
256-
257-
assert $zeitwerk_integration_test_lib
258248
assert $zeitwerk_integration_test_extras
259249
end
260250

0 commit comments

Comments
 (0)