aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib/rails/tasks/zeitwerk.rake
diff options
context:
space:
mode:
Diffstat (limited to 'railties/lib/rails/tasks/zeitwerk.rake')
-rw-r--r--railties/lib/rails/tasks/zeitwerk.rake96
1 files changed, 42 insertions, 54 deletions
diff --git a/railties/lib/rails/tasks/zeitwerk.rake b/railties/lib/rails/tasks/zeitwerk.rake
index b7f5cd154b..5421af6e8b 100644
--- a/railties/lib/rails/tasks/zeitwerk.rake
+++ b/railties/lib/rails/tasks/zeitwerk.rake
@@ -1,78 +1,66 @@
# frozen_string_literal: true
-ensure_classic_mode = ->() do
- if Rails.autoloaders.zeitwerk_enabled?
- abort <<~EOS
- Please, enable temporarily :classic mode:
-
- # config/application.rb
- config.autoloader = :classic
-
- and try again. When all is good, you can delete that line.
- EOS
+ensure_zeitwerk_mode = ->() do
+ unless Rails.autoloaders.zeitwerk_enabled?
+ abort "Please, enable :zeitwerk mode in config/application.rb and try again."
end
end
eager_load = ->() do
- Rails.configuration.eager_load_namespaces.each(&:eager_load!)
+ puts "Hold on, I am eager loading the application."
+ Zeitwerk::Loader.eager_load_all
end
-mismatches = []
-check_directory = ->(directory, parent) do
- # test/mailers/previews might not exist.
- return unless File.exist?(directory)
-
- Dir.foreach(directory) do |entry|
- next if entry.start_with?(".")
- next if parent == Object && entry == "concerns"
+report_not_checked = ->(not_checked) do
+ puts
+ puts <<~EOS
+ WARNING: The files in these directories cannot be checked because they
+ are not eager loaded:
+ EOS
+ puts
- abspath = File.join(directory, entry)
+ not_checked.each { |dir| puts " #{dir}" }
+ puts
- if File.directory?(abspath) || abspath.end_with?(".rb")
- print "."
- cname = File.basename(abspath, ".rb").camelize.to_sym
- if parent.const_defined?(cname, false)
- if File.directory?(abspath)
- check_directory[abspath, parent.const_get(cname)]
- end
- else
- mismatches << [abspath, parent, cname]
- end
- end
- end
+ puts <<~EOS
+ You may verify them manually, or add them to config.eager_load_paths
+ in config/application.rb and run zeitwerk:check again.
+ EOS
+ puts
end
-report = ->() do
- puts
- if mismatches.empty?
- puts "All is good!"
- puts "Please, remember to delete `config.autoloader = :classic` from config/application.rb."
+report = ->(not_checked) do
+ if not_checked.any?
+ report_not_checked[not_checked]
+ puts "Otherwise, all is good!"
else
- mismatches.each do |abspath, parent, cname|
- relpath = abspath.sub(%r{\A#{Regexp.escape(Rails.root.to_path)}/}, "")
- cpath = parent == Object ? cname : "#{parent.name}::#{cname}"
- puts "expected #{relpath} to define #{cpath}"
- end
- puts
- puts <<~EOS
- Please revise the reported mismatches. You can normally fix them by adding
- acronyms to config/initializers/inflections.rb or renaming the constants.
- EOS
+ puts "All is good!"
end
end
namespace :zeitwerk do
desc "Checks project structure for Zeitwerk compatibility"
task check: :environment do
- ensure_classic_mode[]
- eager_load[]
+ ensure_zeitwerk_mode[]
- $stdout.sync = true
- ActiveSupport::Dependencies.autoload_paths.each do |autoload_path|
- check_directory[autoload_path, Object]
+ begin
+ eager_load[]
+ rescue NameError => e
+ if e.message =~ /expected file .*? to define constant \S+/
+ abort $&.sub(/#{Regexp.escape(Rails.root.to_s)}./, "")
+ else
+ raise
+ end
end
- puts
- report[]
+ eager_load_paths = Rails.configuration.eager_load_namespaces.map do |eln|
+ eln.config.eager_load_paths if eln.respond_to?(:config)
+ end.compact.flatten
+
+ not_checked = ActiveSupport::Dependencies.autoload_paths - eager_load_paths
+ not_checked.select! { |dir| Dir.exist?(dir) }
+ not_checked.reject! { |dir| Dir.empty?(dir) }
+
+ report[not_checked]
end
end