Search code examples
ruby-on-railsautoloadruby-on-rails-7zeitwerk

Rails 7.0 upgrade: Autoloading with Zeitwerk


Having trouble autoloading modules when upgrading from classic autoloader to now using zeitwerk.

Currently my modules are as follows:

# app/lib/configuration/test_configuration
module TestConfiguration
  ....code here
end

and in my application.rb i'm using:

config.paths.add 'app/lib', eager_load: true

Which results in the error:

NameError: uninitialized constant Configuration::TestConfiguration

I know that this module is technically namespaced based on the file structure so after changing it to

module Configuration::TestConfiguration
//
end

I still get an error:

uninitialized constant Configuration

I also think I need to change the line in my application to be

config.eager_load_paths

and remove any require that are still present in there i.e:

require "#{Rails.root.join('app', 'lib', 'configuration', 'other_config')}"

Thanks for all the help!


Solution

  • The issue you are finding is not really related to upgrading from classic to zeitwerk modes. It is about upgrading to Rails 7.

    Rails 6.0 and 6.1 issued warnings when loading reloadable code from initializers, and Rails 7 removes that possibility. This is a logical change unrelated to the autoloader mode.

    Please check this section of the upgrading guide.