How to tell Zeitwerk to ignore overrides folder in a rails engine?

179 Views Asked by At

In the latest Rails 7.1.2 my engines have stopped working with the following error

Zeitwerk::NameError:
  expected file /home/jamie/Development/rails/comtech/r7/ccs_cms/engines/admin_dashboard/app/overrides/add_admin_dashboard_link_to_core_header.rb to define constant AddAdminDashboardLinkToCoreHeader, but didn't

It is my understanding that this is due to zeitwerk autoloading files in the engines app/overrides folder. Prior to the latest Rails release I have had the following code in an initializer in my engine.rb

    class Engine < ::Rails::Engine
...
      initializer "let the main autoloader ignore this engine's overrides" do
        overrides = Engine.root.join("app/overrides")
        Rails.autoloaders.main.ignore(overrides)
      end

  config.to_prepare do
    overrides = Engine.root.join("app", "overrides")
    Dir.glob("#{overrides}/**/*.rb").sort.each do |override|
      load override
    end
    decorators = Engine.root.join("app", "decorators")
    Rails.autoloaders.main.ignore(decorators)
    Dir.glob(Engine.root.join("app", "decorators", "**", "*_decorator*.rb")) do |c|
      Rails.configuration.cache_classes ? require(c) : load(c)
    end
  end

This has been happily working up until now and it follows the Documentation which shows the following example on ignoring and loading folders and files specifically overrides

# config/application.rb module MyApp   class Application < Rails::Application
    # ...

    overrides = "#{Rails.root}/app/overrides"
    Rails.autoloaders.main.ignore(overrides)

    config.to_prepare do
      Dir.glob("#{overrides}/**/*_override.rb").sort.each do |override|
        load override
      end
    end   end end

What is the solution to this now please? Or have I hit a Rails bug? Or have I just got it wrong somehow?, up to and including Rails 7.1 this was not an issue

Update It seems I have more than this to deal with. Upgrading to Rails 7.1.2 from Rails 7.1.1 seems to have caused a lot more issues than originally realised. Latest log from starting the rails server shows

/home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/railties-7.1.2/lib/rails.rb:51:in `configuration': undefined method `config' for nil:NilClass (NoMethodError)

      application.config
                 ^^^^^^^
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/factory_bot_rails-6.4.0/lib/factory_bot_rails/railtie.rb:25:in `block in <class:Railtie>'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/activesupport-7.1.2/lib/active_support/lazy_load_hooks.rb:97:in `class_eval'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/activesupport-7.1.2/lib/active_support/lazy_load_hooks.rb:97:in `block in execute_hook'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/activesupport-7.1.2/lib/active_support/lazy_load_hooks.rb:87:in `with_execution_control'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/activesupport-7.1.2/lib/active_support/lazy_load_hooks.rb:92:in `execute_hook'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/activesupport-7.1.2/lib/active_support/lazy_load_hooks.rb:78:in `block in run_load_hooks'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/activesupport-7.1.2/lib/active_support/lazy_load_hooks.rb:77:in `each'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/activesupport-7.1.2/lib/active_support/lazy_load_hooks.rb:77:in `run_load_hooks'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/activerecord-7.1.2/lib/active_record/base.rb:338:in `<module:ActiveRecord>'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/activerecord-7.1.2/lib/active_record/base.rb:15:in `<top (required)>'
    from <internal:/home/jamie/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from <internal:/home/jamie/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/zeitwerk-2.6.12/lib/zeitwerk/kernel.rb:38:in `require'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/database_cleaner-active_record-2.1.0/lib/database_cleaner/active_record/truncation.rb:2:in `<top (required)>'
    from <internal:/home/jamie/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from <internal:/home/jamie/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/zeitwerk-2.6.12/lib/zeitwerk/kernel.rb:38:in `require'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/database_cleaner-active_record-2.1.0/lib/database_cleaner/active_record.rb:4:in `<top (required)>'
    from <internal:/home/jamie/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from <internal:/home/jamie/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/zeitwerk-2.6.12/lib/zeitwerk/kernel.rb:38:in `require'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/database_cleaner-active_record-2.1.0/lib/database_cleaner-active_record.rb:1:in `<top (required)>'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/zeitwerk-2.6.12/lib/zeitwerk/kernel.rb:38:in `require'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/zeitwerk-2.6.12/lib/zeitwerk/kernel.rb:38:in `require'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/bundler-2.4.19/lib/bundler/runtime.rb:60:in `block (2 levels) in require'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/bundler-2.4.19/lib/bundler/runtime.rb:55:in `each'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/bundler-2.4.19/lib/bundler/runtime.rb:55:in `block in require'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/bundler-2.4.19/lib/bundler/runtime.rb:44:in `each'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/bundler-2.4.19/lib/bundler/runtime.rb:44:in `require'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/bundler-2.4.19/lib/bundler.rb:187:in `require'
    from /home/jamie/Development/rails/comtech/r7/ccs_cms/engines/admin_dashboard/test/dummy/config/application.rb:7:in `<top (required)>'
    from <internal:/home/jamie/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from <internal:/home/jamie/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/zeitwerk-2.6.12/lib/zeitwerk/kernel.rb:38:in `require'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/railties-7.1.2/lib/rails/commands/server/server_command.rb:139:in `block in perform'
    from <internal:kernel>:90:in `tap'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/railties-7.1.2/lib/rails/commands/server/server_command.rb:136:in `perform'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/thor-1.3.0/lib/thor/command.rb:28:in `run'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/thor-1.3.0/lib/thor/invocation.rb:127:in `invoke_command'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/railties-7.1.2/lib/rails/command/base.rb:178:in `invoke_command'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/thor-1.3.0/lib/thor.rb:527:in `dispatch'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/railties-7.1.2/lib/rails/command/base.rb:73:in `perform'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/railties-7.1.2/lib/rails/command.rb:71:in `block in invoke'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/railties-7.1.2/lib/rails/command.rb:149:in `with_argv'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/railties-7.1.2/lib/rails/command.rb:69:in `invoke'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/railties-7.1.2/lib/rails/commands.rb:18:in `<top (required)>'
    from <internal:/home/jamie/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from <internal:/home/jamie/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/zeitwerk-2.6.12/lib/zeitwerk/kernel.rb:38:in `require'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/railties-7.1.2/lib/rails/engine/commands.rb:9:in `<top (required)>'
    from <internal:/home/jamie/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from <internal:/home/jamie/.rvm/rubies/ruby-3.2.2/lib/ruby/3.2.0/rubygems/core_ext/kernel_require.rb>:37:in `require'
    from /home/jamie/.rvm/gems/ruby-3.2.2@cms_admin_dashboard/gems/zeitwerk-2.6.12/lib/zeitwerk/kernel.rb:38:in `require'
    from bin/rails:14:in `<main>'
0

There are 0 best solutions below