4

I currently have the following files in the jobs directory:

# app/jobs/importer.rb
module Imporer
  def self.valid_importers
    # Do stuff
  end
end

# app/jobs/importer/custom_import_job.rb
class Importer::CustomImportJob < ApplicationJob
  def perform
    # Do stuff
  end
end

This works without issues using the classic code loader, but when switching to Zeitwerk, I get a NameError: uninitialized constant Importer::CustomImportJob error when running rails zeitwerk:check.

I tried moving custom_import_job.rb to the jobs directory, but still received the same error. Adding app/jobs/importer to config.autoload_paths didn't help either.

Is there something wrong with the file structure or am I missing something in the Zeitwerk settings?

Roman Alekseiev
  • 1,854
  • 16
  • 24

2 Answers2

5

After some digging around, I realized that I had the following in development.rb:

Dir[Rails.root.join('app/jobs/importer/*.rb')].each { |f| require f }

It seems this was the cause of the issue. Everything is working as expected now that it's removed!

  • What was the problem though? Why was that an issue? – BJ McDuck Jun 22 '21 at 20:32
  • 3
    My guess is that the manual `require` interfered with the way Zeitwerk auto-loads the files, this causing the issue I faced. It probably wouldn't have been a problem if the files weren't in a folder that gets auto-loaded. – Ahmad Abdel-Yaman Jun 24 '21 at 06:52
0

You could take a look if you app/jobs is in the path:

ActiveSupport::Dependencies.autoload_paths

extend by config.autoload_paths in config/application.rb if necessary.

More debugging information: https://guides.rubyonrails.org/autoloading_and_reloading_constants.html#troubleshooting.

  • Stop Spring. Restart the server. May also help.

Hope these helps.

Juanito Fatas
  • 9,419
  • 9
  • 46
  • 70