Rails 7.1.2升级后引擎中Zeitwerk忽略overrides文件夹失效及相关报错的解决方案咨询
Rails 7.1.2升级后引擎中Zeitwerk忽略overrides文件夹失效及相关报错的解决方案咨询
我最近把Rails升级到7.1.2版本后,我的引擎突然无法正常工作了,遇到了以下Zeitwerk错误:
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
我了解到这是因为Zeitwerk自动加载了引擎app/overrides目录下的文件导致的。在升级到这个版本之前,我一直在引擎的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
这段代码是完全按照Rails官方文档的示例来写的,官方文档里的示例是这样的:
# 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
现在升级到7.1.2之后就出问题了,想问问大家有没有解决办法?这是Rails的bug吗?还是我哪里写错了?另外,我之前用Rails 7.1版本的时候完全没问题。
更新:更多问题出现
升级到7.1.2之后,除了Zeitwerk的问题,还有更多报错。启动Rails服务器时的最新日志如下:
/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>'
现在完全不知道该怎么处理了,求助各位大佬!
备注:内容来源于stack exchange,提问作者jamesc
相关产品推荐
相关产品推荐

