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

Zeitwerk #22488

Merged
merged 6 commits into from
Aug 23, 2023
Merged

Zeitwerk #22488

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ class Application < Rails::Application

config.autoload_paths += config.eager_load_paths

config.autoloader = :zeitwerk

# config.load_defaults 6.1
# Disable defaults as ActiveRecord::Base.belongs_to_required_by_default = true causes MiqRegion.seed to fail validation on belongs_to maintenance zone

# NOTE: If you are going to make changes to autoload_paths, please make
# sure they are all strings. Rails will push these paths into the
# $LOAD_PATH.
Expand Down Expand Up @@ -159,6 +164,15 @@ class Application < Rails::Application
Vmdb::Loggers.apply_config(::Settings.log)
end

initializer :eager_load_all_the_things, :after => :load_config_initializers do
jrafanie marked this conversation as resolved.
Show resolved Hide resolved
if ENV['DEBUG_MANAGEIQ_ZEITWERK'].present?
config.eager_load_paths += config.autoload_paths
Vmdb::Plugins.each do |plugin|
plugin.config.eager_load_paths += plugin.config.autoload_paths
end
end
end

config.after_initialize do
Vmdb::Initializer.init
ActiveRecord::Base.connection_pool.release_connection
Expand Down
3 changes: 3 additions & 0 deletions config/initializers/yaml_autoloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#
# Note, this is used to autoload constants serialized as yaml from one process and loaded in another such as through
# args in the MiqQueue. An alternative would be to eager load all of our autoload_paths in all processes.
#
# This is still needed in some areas for zeitwerk, such as YAML files for tests in the manageiq-providers-vmware
# that reference a constant: RbVmomi::VIM::TaskEvent
Psych::Visitors::ToRuby.prepend Module.new {
def resolve_class(klass_name)
(class_loader.class != Psych::ClassLoader::Restricted && klass_name && klass_name.safe_constantize) || super
Expand Down
19 changes: 19 additions & 0 deletions config/initializers/zeitwerk.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
if Rails.application.config.autoloader == :zeitwerk && Rails.autoloaders.main
if ENV['DEBUG_MANAGEIQ_ZEITWERK'].present?
Zeitwerk::Loader.default_logger = method(:puts)
Rails.autoloaders.main.logger = Logger.new($stdout)
end

# These specific directories are for code organization, not namespacing:
# TODO: these should be either renamed with good names, the intermediate directory removed
# and/or both.
Rails.autoloaders.main.collapse(Rails.root.join("lib/manageiq/reporting/charting"))
Rails.autoloaders.main.collapse(Rails.root.join("lib/ansible/runner/credential"))
Rails.autoloaders.main.collapse(Rails.root.join("lib/pdf_generator"))

# requires qpid, which is an optional dependency because LOL mac
if RUBY_PLATFORM.include?("darwin")
message_handler_path = Pathname.new(Vmdb::Plugins.paths[ManageIQ::Providers::Nuage::Engine]).join("app/models/manageiq/providers/nuage/network_manager/event_catcher/messaging_handler.rb")
Rails.autoloaders.main.ignore(message_handler_path)
end
end
1 change: 1 addition & 0 deletions spec/initializers/yaml_autoloader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
let(:missing_model) { model_directory.join("zzz_model.rb") }

before do
skip "This is currently not testable with zeitwerk!" if Rails.application.config.autoloader == :zeitwerk
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried many things to test this with zeitwerk but either can't modify the autoload paths OR, it's too late and the changes to these paths aren't used. Basically, we can't do proper setup/teardown to test this with zeitwerk. Cross repo fails in manageiq-providers-vmware if you remove the yaml autoloader, see above.

File.write(missing_model, "class ZzzModel\nend\n")
ActiveSupport::Dependencies.autoload_paths << model_directory
end
Expand Down
Loading