Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fxn/271deed5a0772a5967c3aa00a8306746 to your computer and use it in GitHub Desktop.
Save fxn/271deed5a0772a5967c3aa00a8306746 to your computer and use it in GitHub Desktop.

The Zeitwerk integration in Rails 6 has several parts.

  1. First, we define a new configuration point Rails.configuration.autoloader here:

    https://github.com/rails/rails/blob/master/railties/lib/rails/application/configuration.rb

    and also set it to :zeitwerk in the 6.0 defaults.

  2. Then, we define Rails.autoloaders here

    https://github.com/rails/rails/blob/master/railties/lib/rails/autoloaders.rb

  3. When the application is finishing the boot process, Zeitwerk takes over AS::Dependencies depending on the configuration. It happens in the finisher let_zeitwerk_take_over:

    https://github.com/rails/rails/blob/master/railties/lib/rails/application/finisher.rb

  4. In the same file above, eager loading delegates to Zeitwerk if the Zeitwerk constant is defined, regardless of whether the application itself is using it (because there could be gem dependencies using it). See eager_load!.

  5. This is the main file, the one in which Zeitwerk is setup:

    https://github.com/rails/rails/blob/master/activesupport/lib/active_support/dependencies/zeitwerk_integration.rb

Everything has been thought in a way that leaves the classic autoloader untouched, to guarantee classic works as always in case you want to opt-out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment