Uninitialized Constant Error When Switching To Zeitwerk

Viewed 3481

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?

2 Answers

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!

Related