Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warn if frameworks are loaded too early #46047

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Commits on Jan 12, 2023

  1. Warn if frameworks are loaded too early

    Prematurely loading frameworks in a Rails application may slow down boot
    time and could cause conflicts with load order and boot of the
    application.
    
    This change allows showing a warning in a load hook if a required
    framework or load hook hasn't been loaded yet:
    
    ```ruby
      initializer "active_record.warn_if_prematurely_loaded" do
        ActiveSupport.on_load(:active_record) do
          ActiveSupport.warn_if_prematurely_loaded(:active_record, before: :after_initialize)
        end
      end
    ```
    
    In this case if `:active_record` is loaded before `:after_initialize`
    has run we show a warning:
    
          Load hook :active_record was called before load hook :after_initialize.
          Prematurely loading frameworks may slow down your boot time and could
          cause conflicts with load order and boot of your application.
    
          Consider wrapping your code with an on_load hook:
    
              ActiveSupport.on_load(:active_record) do
                # your code
              end
          Called from:
          config/initializers/some_initializer.rb:1:in `<main>'
          ...
    
    In production `eager_load` gets called to load add frameworks and we
    should silence the warning. A `silence_prematurely_loading_warnings` has
    been added to silence all load hook order warnings. This
    `silence_prematurely_loading_warnings` is called before `eager_load`
    gets called.
    
    The hook has been implemented for ActiveRecord but other frameworks,
    like ActionView for example, could use it as well.
    
    Some tests include initializers that call ActiveRecord::Base. To not
    show the warning `silence_prematurely_loading_warnings` has been added
    to the initializers.
    p8 committed Jan 12, 2023
    Configuration menu
    Copy the full SHA
    84ce472 View commit details
    Browse the repository at this point in the history