Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zeitwerk "DEPRECATION WARNING: Initialization autoloaded the constants" caused by including a module in the lib folder

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

Since upgrading to Rails 6 I have been getting this warning relating to the new Zeitwerk autoloader.

Often this is caused by loading constants in the config/initializers folder, but that is not the case here.

AuthHelper and SemanticFormHelper are pulled in by these 2 files I have in my lib folder:

module AuthorizationSystem
  include AuthHelper
  ...
end
module SemanticFormBuilder
  include SemanticFormHelper
  ...
end

On initialization all the files in the lib folder are run, and anything included in those files trigger the DEPRECATION WARNING.

If I remove the include statements the warnings go away but then the app breaks on certain pages because the includes are necessary.

How can I have include statements in files in my lib folder without causing the warning?

ActionText::ContentHelper and ActionText::TagHelper are nowhere to be found in my app, so I imagine those warnings are coming from a gem I am using. Any ideas on how to debug that would also be greatly appreciated.

like image 607
markv12 Avatar asked Nov 06 '22 02:11

markv12


1 Answers

To locate the source of your warnings, you can use the following code, placed in application.rb, before 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

(from GitHub)

like image 180
Matic Jurglič Avatar answered Nov 12 '22 19:11

Matic Jurglič