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

Fix engine loading for Rails 7.2 #1702

Merged
merged 2 commits into from
Oct 31, 2023
Merged
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
19 changes: 17 additions & 2 deletions lib/tapioca/loaders/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def load_rails_application(environment_load: false, eager_load: false, app_root:
def load_rails_engines
return if engines.empty?

normalize_eager_load_paths_configuration!

with_rails_application do
run_initializers

Expand Down Expand Up @@ -110,7 +112,7 @@ def load_engines_in_zeitwerk_mode
autoloader = Zeitwerk::Loader.new

engines.each do |engine|
engine.config.eager_load_paths.each do |path|
engine.config.all_eager_load_paths.each do |path|
vinistock marked this conversation as resolved.
Show resolved Hide resolved
# Zeitwerk only accepts existing directories in `push_dir`.
next unless File.directory?(path)
# We should not add directories that are already managed by a Zeitwerk loader.
Expand All @@ -131,7 +133,7 @@ def load_engines_in_classic_mode
# We can't use `Rails::Engine#eager_load!` directly because it will raise as soon as it encounters
# an error, which is not what we want. We want to try to load as much as we can.
engines.each do |engine|
engine.config.eager_load_paths.each do |load_path|
engine.config.all_eager_load_paths.each do |load_path|
Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
require_dependency file
end
Expand Down Expand Up @@ -179,6 +181,19 @@ def engines
.reject { |engine| gem_in_app_dir?(project_path, engine.config.root.to_path) }
end

# Rails 7.2 renamed `eager_load_paths` to `all_eager_load_paths`, which maintains the same original functionality.
# The `eager_load_paths` method still exists, but doesn't return all paths anymore and causes Tapioca to miss some
# engine paths. The following commit is the change:
# https://github.com/rails/rails/commit/ebfca905db14020589c22e6937382e6f8f687664
#
# Here we make sure that the new `all_eager_load_paths` is always defined for every Rails version below 7.2, so
# that we can use it everywhere
def normalize_eager_load_paths_configuration!
return if Rails::VERSION::MAJOR >= 7 && Rails::VERSION::MINOR >= 2

engines.each { |e| e.config.all_eager_load_paths = e.config.eager_load_paths }
end

sig { params(path: String).void }
def safe_require(path)
require path
Expand Down
2 changes: 1 addition & 1 deletion lib/tapioca/static/symbol_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def engine_symbols(gem)

return Set.new unless gem_engine

paths = gem_engine.config.eager_load_paths.flat_map do |load_path|
paths = gem_engine.config.all_eager_load_paths.flat_map do |load_path|
Pathname.glob("#{load_path}/**/*.rb")
end

Expand Down
Loading