Skip to content

Commit

Permalink
Merge pull request #1707 from Shopify/vs/fix_engine_loading_older_rails
Browse files Browse the repository at this point in the history
Fix engine loading for Rails < 7.2
  • Loading branch information
vinistock authored Nov 6, 2023
2 parents 5c598c2 + e42529f commit 14acb51
Show file tree
Hide file tree
Showing 7 changed files with 24,028 additions and 15,244 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@ group :test do
end

gem "kramdown", "~> 2.4"

gem "prism", "< 0.16"
7 changes: 4 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ GEM
ast (~> 2.4.1)
racc
prettier_print (1.2.1)
prism (0.15.1)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
Expand Down Expand Up @@ -296,9 +297,9 @@ GEM
zeitwerk (~> 2.5)
rainbow (3.1.1)
rake (13.0.6)
rbi (0.1.1)
rbi (0.1.3)
prism (>= 0.15.1, < 0.17)
sorbet-runtime (>= 0.5.9204)
yarp (>= 0.11.0)
redis (5.0.6)
redis-client (>= 0.9.0)
redis-client (0.17.0)
Expand Down Expand Up @@ -384,7 +385,6 @@ GEM
yard-sorbet (0.8.1)
sorbet-runtime (>= 0.5)
yard (>= 0.9)
yarp (0.11.0)
zeitwerk (2.6.11)

PLATFORMS
Expand Down Expand Up @@ -412,6 +412,7 @@ DEPENDENCIES
minitest-reporters
net-smtp (= 0.4.0)
nokogiri
prism (< 0.16)
pry
pry-byebug
rails
Expand Down
30 changes: 13 additions & 17 deletions lib/tapioca/loaders/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ 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 @@ -112,7 +110,7 @@ def load_engines_in_zeitwerk_mode
autoloader = Zeitwerk::Loader.new

engines.each do |engine|
engine.config.all_eager_load_paths.each do |path|
eager_load_paths(engine).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 @@ -133,7 +131,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.all_eager_load_paths.each do |load_path|
eager_load_paths(engine).each do |load_path|
Dir.glob("#{load_path}/**/*.rb").sort.each do |file|
require_dependency file
end
Expand Down Expand Up @@ -181,19 +179,6 @@ 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 Expand Up @@ -239,6 +224,17 @@ def require_helper(file)

require(file)
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
sig { params(engine: T.class_of(Rails::Engine)).returns(T::Array[String]) }
def eager_load_paths(engine)
config = engine.config

(config.respond_to?(:all_eager_load_paths) && config.all_eager_load_paths) || config.eager_load_paths
end
end
end
end
10 changes: 9 additions & 1 deletion lib/tapioca/static/symbol_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ def engine_symbols(gem)

return Set.new unless gem_engine

paths = gem_engine.config.all_eager_load_paths.flat_map do |load_path|
# https://github.com/rails/rails/commit/ebfca905db14020589c22e6937382e6f8f687664
config = gem_engine.config
eager_load_paths = if config.respond_to?(:all_eager_load_paths)
config.all_eager_load_paths
else
config.eager_load_paths
end

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

Expand Down
Loading

0 comments on commit 14acb51

Please sign in to comment.