DEV Community

Cover image for Rails 7.1: Changes to $LOAD_PATH
Hassan Ahmed
Hassan Ahmed

Posted on

Rails 7.1: Changes to $LOAD_PATH

Having worked in Ruby on Rails, you must know we don't need to manually require files that reside inside the app folder. The framework internally takes care of loading and reloading files in the app folder and apart from it, if we want to autoload any other folder e.g. lib, that can be achieved by pushing it to the collection config.autoload_paths (more details can be found in the guides). Historically, all autoload paths were pushed to $LOAD_PATH.

TLDR: $LOAD_PATH a.k.a $: is a collection of absolute paths that you can require (this video although a bit old helped me in understanding this concept better)

Starting Rails 7_1 autoloaded paths will no longer be added to $LOAD_PATH. Since autoloaded files e.g. app/models/user.rb are implicitly "required", it actually does makes sense to not bloat the $LOAD_PATH and it only contains list of paths that we need to require and are not handled by the autoloader.

If you are upgrading from a previous version of Rails this option was part of the new Rails 7_1 defaults under the flag

Rails.application.config.add_autoload_paths_to_load_path = false
Enter fullscreen mode Exit fullscreen mode

Uncommenting this default will also no longer move the autoloaded paths to $LOAD_PATH.

Upgrading from a previous version
In terms of impact, if you just upgraded from a previous version of Rails, I assume for the most part it should be safe as autoloaded files should not be required in the first place and test coverage should point out if this can be a breaking change. Moreover, I'd test this first on a non prod environment by playing around the application a bit and later when shipped to prod, keep an eye on any errors that pop up.

Happy coding!

References:

Top comments (0)