Zeitwerk“弃用警告:初始化自动加载常量”是由在 lib 文件夹中包含一个模块引起的

mar*_*v12 5 ruby-on-rails deprecation-warning ruby-on-rails-6

DEPRECATION WARNING: Initialization autoloaded the constants AuthHelper, SemanticFormHelper, ActionText::ContentHelper, and ActionText::TagHelper

自从升级到 Rails 6 以来,我一直收到有关新 Zeitwerk 自动加载器的警告。

通常这是由在config/initializers文件夹中加载常量引起的,但这里不是这种情况。

AuthHelper并被SemanticFormHelperlib文件夹中的这两个文件拉入:

module AuthorizationSystem
  include AuthHelper
  ...
end
Run Code Online (Sandbox Code Playgroud)
module SemanticFormBuilder
  include SemanticFormHelper
  ...
end
Run Code Online (Sandbox Code Playgroud)

初始化时,文件lib夹中的所有文件都会运行,这些文件中包含的任何内容都会触发DEPRECATION WARNING.

如果我删除这些include语句,警告就会消失,但是应用程序会在某些页面上中断,因为包含是必要的。

如何include在我的lib文件夹中的文件中包含语句而不引起警告?

ActionText::ContentHelper并且ActionText::TagHelper在我的应用程序中无处可寻,所以我想这些警告来自我正在使用的 gem。任何关于如何调试的想法也将不胜感激。

Mat*_*lič 4

要找到警告的来源,您可以使用以下代码,放置在application.rb, 之前Bundler.require

ActiveSupport.on_load(:action_controller_base) do
  bc = ActiveSupport::BacktraceCleaner.new
  bc.remove_silencers!
  bc.add_silencer { |line| line.start_with?(RbConfig::CONFIG["rubylibdir"]) }
  bc.add_silencer { |line| line =~ Regexp.union(
    *(
      %w{ bootsnap railties spring activesupport actionpack zeitwerk thor rack }.
      map{|g| /\A#{g} \([\w.]+\) /}
    ),
    /\Abin\/rails/
  )}
  trace = bc.clean(caller)
  puts "Cleaned backtrace:\n\t#{trace.join("\n\t")}\n"
  puts "Most probably the cause is: #{trace.first}"
  puts "If not - uncomment `raise` at #{__FILE__}:#{__LINE__+1}"
  # raise "i can haz full backtrace"
  exit(1)
end
Run Code Online (Sandbox Code Playgroud)

(来自GitHub