Skip to content

Commit

Permalink
Merge pull request Shopify#1702 from Shopify/vs/fix_engine_loading_ra…
Browse files Browse the repository at this point in the history
…ils_72

Fix engine loading for Rails 7.2
  • Loading branch information
vinistock authored Oct 31, 2023
2 parents 7b8a37d + 700f431 commit 34fc676
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
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|
# 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

0 comments on commit 34fc676

Please sign in to comment.