maintenance_tasks icon indicating copy to clipboard operation
maintenance_tasks copied to clipboard

Class loader Issue MaintenanceTasks::CsvCollectionBuilder

Open orbanbotond opened this issue 2 years ago • 1 comments

As I turn on the eager loader and start the rails console I receive this error:

be rails c             7s arm64 2.7.3p183
/Users/orbanbotond/.frum/versions/2.7.3/lib/ruby/gems/2.7.0/gems/maintenance_tasks-1.10.1/app/models/maintenance_tasks/batch_csv_collection_builder.rb:9:in `<module:MaintenanceTasks>': uninitialized constant MaintenanceTasks::CsvCollectionBuilder
Did you mean?  MaintenanceTasks::NoCollectionBuilder
               MaintenanceTasks::BatchCSVCollectionBuilder
               MaintenanceTasks::CSVCollectionBuilder (NameError)

This is my ruby version:

ruby -v                7s arm64 2.7.3p183
ruby 2.7.3p183 (2021-04-05 revision 6847ee089d) [arm64-darwin20]

orbanbotond avatar May 31 '22 08:05 orbanbotond

Yeah we've noticed in https://github.com/Shopify/maintenance_tasks/pull/620 and dropped support for it for v2.0.0. I think you could work around that issue, but I can't reproduce it myself. I've created a rails 6.1.6 app, installed active storage, installed the gem, set config.autoloader = :classic and in development set config.eager_load = true, but when I start the console it doesn't raise the issue.

I've also tried adding an inflection in config/initializers/inflections.rb:

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym "CSV"
end

Can you give me repro steps? From there we should be able to require the file and avoid the eager-loading issue.

etiennebarrie avatar May 31 '22 10:05 etiennebarrie

Here are some reproduction steps with rails 6.1.7, but under Zeitwerk:

rails new testapp --skip-webpack-install --skip-javascript

Flip config.eager_load to true in config/environments/development.rb.

Then edit config/initializers/inflections.rb to have:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.acronym 'CSV'
end

And finally add gem 'maintenance_tasks' to Gemfile.

When you try to launch a console, it'll crash.

This is due to the fact that Rails doesn't isolate inflections to the main application, thus requiring something like:

Rails.autoloaders.each do |autoloader|
  autoloader.inflector.inflect(
    'batch_csv_collection_builder' => 'BatchCsvCollectionBuilder',
    'csv_collection_builder' => 'CsvCollectionBuilder',
  )
end

drcapulet avatar Oct 26 '22 17:10 drcapulet

Cheers @drcapulet. For anyone curious about the zeitwerk inflections mentioned in https://github.com/Shopify/maintenance_tasks/issues/642#issuecomment-1292393956

  • 📖 Check out https://guides.rubyonrails.org/autoloading_and_reloading_constants.html#customizing-inflections
  • Add bin/rails zeitwerk:check to your CI build, this will catch any autoload errors that would otherwise show up in eager_load https://guides.rubyonrails.org/autoloading_and_reloading_constants.html#manual-testing

akabiru avatar Nov 04 '22 08:11 akabiru

This issue has been marked as stale because it has not been commented on in two months. Please reply in order to keep the issue open. Otherwise, it will close in 14 days. Thank you for contributing!

github-actions[bot] avatar Jan 26 '24 01:01 github-actions[bot]