Rails 7.1 automatically loads lib! Now, what?

All the directories under app are automatically loaded by Rails in production. Further, since Rails 7.1, it’s quite easy to get Rails to automatically load all the files in the lib directory. This is enabled by having this obvious sounding line in config/application.rb

config.autoload_lib(ignore: %w[assets tasks])

If you are sure you do not want to do this, there are a couple of things you can do.

Remove it all: If you don’t want this at all, just comment it out although it’s probably better to find a way to work with it if that makes sense to you.

Tune the loading: On the other hand, you might have code there which you do not want to load when the Rails app loads but are happy to have everything else loaded automatically. Already, the default line added has an ignore: list which includes assets and tasks which don’t make sense to automatically load.

You can add more paths to this. In my case, I have some utility classes that are called from tasks and will eventually be removed to a separate gem. So, I added them in to this list. Whatever is not included will get automatically loaded.

config.autoload_lib(ignore: %w[assets tasks data_loader])

You can find multiple articles on this feature and this one is pretty good: https://www.shakacode.com/blog/rails-7-1-introduces-config-autoload_lib/

comments powered by Disqus