0

In my rails 6 app running ruby 2.7, I am trying to eager load all the files using Zeitwerk. Here is a snippet of my application.rb file:

class Application < Rails::Application
config.load_defaults "6.0"
Zeitwerk::Loader.eager_load_all

# Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading
# the framework and any gems in your application.

# Middlewares
config.middleware.insert 0, Rack::UTF8Sanitizer
config.middleware.use Rack::Attack
config.autoload_paths += Dir["#{config.root}/lib/**/*"]
config.autoload_paths += Dir["#{config.root}/app/models/**/*"]
config.autoload_paths += Dir["#{config.root}/app/observers/*"]
config.autoload_paths += Dir["#{config.root}/app/middleware/*"]
config.autoload_paths += Dir["#{config.root}/app/validators/**/*"]
config.autoload_paths += Dir["#{config.root}/app/**/concerns/*"]
config.autoload_paths += Dir["#{config.root}/app/operations/**/*"]
config.autoload_paths += Dir["#{config.root}/app/scrubbers/**/*"]
config.autoload_paths += Dir["#{config.root}/app/workers/**/*"]
config.paths.add "#{config.root}/app/operations/", eager_load: true

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
config.time_zone = "UTC"

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')].reject { |f| File.fnmatch("#{Rails.root.to_s}/config/locales/**/x_*.yml", f) }
config.i18n.available_locales = [:"en", :"fr", :"fr_en"]
config.i18n.default_locale = :en

# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
config.assets.precompile += ["public.css", "public.js", "public_payments.js", "email.css", "pdf.css", "pronotif.css"]

config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins '*'
    resource '*', :headers => :any, :methods => [:get, :post, :options]
  end
end

end

However, when I run this I get this error:

NoMethodError: undefined method `eager_load_paths' for #<I18n::Config:0x00007fceade8c0f0>

Any solution for this will be helpful

  • 1
    Hold your horses. There is no reason you have to add all those directories to the autoload paths. Rails will already by default add every subdirectory of `/app` to the autoload paths. Adding `Dir["#{config.root}/lib/**/*"]` is also kind of an ant-pattern as it will add every subdirectory of `lib` as roots. I also don't get why you think you want to autoload everything in `application.rb` as that will just ruin code reloading in dev/test. This smells very much like an X&Y question - whats the actual problem that your trying to solve with all this? – max Mar 12 '20 at 13:25

1 Answers1

2

Problem

Aside from the fact that your application.rb is not in a good shape (as @max mentioned in the first comment to your question) the problem is known and already fixed for Rails.

Unfortunately (as time of writing, with Rails 6.0.2.2) the fix is currently not public.

Temporary Workaround

But until then you might copy the patched rake task to your app under a file named lib/tasks/local_zeitwerk.rake and change line 41 to:

namespace :local_zeitwerk do

and then execute the check task with

bin/rails local_zeitwerk:check
Sandro L
  • 1,140
  • 18
  • 32