lib/tapioca/loaders/loader.rb in tapioca-0.11.9 vs lib/tapioca/loaders/loader.rb in tapioca-0.11.10

- old
+ new

@@ -78,10 +78,12 @@ sig { void } def load_rails_engines return if engines.empty? + normalize_eager_load_paths_configuration! + with_rails_application do run_initializers if zeitwerk_mode? load_engines_in_zeitwerk_mode @@ -108,11 +110,11 @@ # We use a fresh loader to load the engine directories, so that we don't interfere with # any of the existing loaders. 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. next if managed_dirs.member?(path) @@ -129,11 +131,11 @@ # https://github.com/rails/rails/blob/d9e188dbab81b412f73dfb7763318d52f360af49/railties/lib/rails/engine.rb#L489-L495 # # 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 rescue ScriptError, StandardError nil @@ -175,9 +177,22 @@ # We can use `Class#descendants` here, since we know Rails is loaded Rails::Engine .descendants .reject(&:abstract_railtie?) .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