ruby-on-rails Rails 6 Zeitwerk“DEPRECATION WARNING:初始化自动加载常量...”但我不知道在哪里?

fgw7neuy  于 2023-04-08  发布在  Ruby
关注(0)|答案(2)|浏览(111)

我发誓我读过文档,我认为我对原理有了基本的理解,但我一辈子都不能弄清楚我在哪里不恰当地加载了这些常数。
我正在升级一个最初是Rails5.2的应用程序
当我运行RSpec、服务器、本地控制台等时,我会收到这个警告。

DEPRECATION WARNING: Initialization autoloaded the constants ApplicationHelper, EventsHelper, FontAwesome::Rails::IconHelper, DeviseHelper, ErrorHandler, and ApplicationController.

Being able to do this is deprecated. Autoloading during initialization is going
to be an error condition in future versions of Rails.

以下是不正确自动加载的常量:

  • ApplicationHelper -标准Rails文件
  • EventsHelper - * EventsController的标准Rails文件 *
  • FontAwesome::Rails::IconHelper - * 来自gem,而不是我的应用程序目录 *
  • DeviseHelper - * 来自gem,而不是我的应用程序目录 *
  • ErrorHandler - * 位于'app/controllers/concerns/error_handler'*
  • ApplicationController - * 标准Rails文件 *

我已经查找了可能调用includerequire来获取这些常量的示例,但没有找到。
我已经运行(并通读)了bin/rails zeitwerk:check,没有任何明显的提示。我看到这些示例按预期加载:

...
Zeitwerk@rails.main: constant ApplicationHelper loaded from file /Users/ckragt/ruby/filterbuildscheduler/app/helpers/application_helper.rb
...
Zeitwerk@rails.main: constant EventsHelper loaded from file /Users/ckragt/ruby/filterbuildscheduler/app/helpers/events_helper.rb
...
Zeitwerk@rails.main: constant FontAwesome::Rails::IconHelper loaded from file /Users/ckragt/.rvm/gems/ruby-2.7.0/gems/font-awesome-rails-4.7.0.7/app/helpers/font_awesome/rails/icon_helper.rb
...
Zeitwerk@rails.main: constant DeviseHelper loaded from file /Users/ckragt/.rvm/gems/ruby-2.7.0/gems/devise-4.7.3/app/helpers/devise_helper.rb
...
Zeitwerk@rails.main: constant ErrorHandler loaded from file /Users/ckragt/ruby/filterbuildscheduler/app/controllers/concerns/error_handler.rb
...
Zeitwerk@rails.main: autoload set for ApplicationController, to be loaded from /Users/ckragt/ruby/filterbuildscheduler/app/controllers/application_controller.rb

我正在使用Spring和Binstub(都更新到最新版本)。
下面是我的application.rb文件的顶部:

# frozen_string_literal: true

require_relative 'boot'
require 'csv'

require 'rails'
# Pick the frameworks you want:
require 'active_model/railtie'
require 'active_job/railtie'
require 'active_record/railtie'
# require "active_storage/engine"
require 'action_controller/railtie'
require 'action_mailer/railtie'
# require 'action_mailbox/engine'
# require 'action_text/engine'
require 'action_view/railtie'
# require 'action_cable/engine'
require 'sprockets/railtie'
# require 'rails/test_unit/railtie'

require 'google/apis/gmail_v1'
require 'google/api_client/client_secrets'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module BuildPlanner
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 6.0
...

我还能去哪里看看

nr7wwzry

nr7wwzry1#

这似乎与rails_admin有关。

我是怎么知道的:

Someone else分享了我对警告消息的沮丧,并要求更多的帮助来跟踪这个问题。
One response建议将pp caller_locations放在正在自动加载的文件的顶部。这样做给了我一个可以使用的回溯。这时我注意到rails_admin在每个文件中都显示为路径。
我注意到在config/initializers/rails_admin.rb中已经有了一个巨大的require语句列表:

require 'money-rails/rails_admin'
require 'rails_admin/adapters/active_record'
require 'application_record'
require 'user'
require 'event'
require 'registration'
require 'location'
require 'technology'
require 'supplier'
require 'component'
require 'part'
require 'material'
require 'count'
require 'inventory'
require 'extrapolate_component_part'
require 'extrapolate_material_part'
require 'extrapolate_technology_component'
require 'extrapolate_technology_part'
require 'extrapolate_technology_material'

为了好玩,我把它们都注解掉了,然后运行了一个RSpec测试,我的警告消息突然变得超级长,包含了更多的示例:

DEPRECATION WARNING: Initialization autoloaded the constants ApplicationHelper, EventsHelper, FontAwesome::Rails::IconHelper, DeviseHelper, ErrorHandler, ApplicationController, ApplicationRecord, User, Event, Registration, Location, Technology, Component, Part, Material, Supplier, Count, Inventory, ExtrapolateComponentPart, ExtrapolateMaterialPart, ExtrapolateTechnologyComponent, ExtrapolateTechnologyPart, and ExtrapolateTechnologyMaterial.

所以我决定在我以前的例子中添加:

require 'application_helper'
require 'events_helper'
require 'devise_helper'
require 'error_handler'
require 'application_controller'

...我已经很长的列表,我的警告消息,所有的,但FontAwesome::Rails::IconHelper消失.我想我可以得到一个太,如果我能找出正确的文件路径.

ctehm74n

ctehm74n2#

有点晚了,但我发现这很有帮助。说你遇到了。

DEPRECATION WARNING: Initialization autoloaded the constants Thing

你所需要做的就是转到它被定义的地方,并导致编译器错误:

# some/thing.rb

foo # blow up the initialization
class Thing
  ...
end

您现在应该能够找到需要它的地方。

相关问题