✨
Public Notes
  • Personal running notes using Forem
  • Setting up a local test env without docker
  • Setting gitbook to use "main" branch by default
  • User Moderation
    • User Moderation is really just scoring
  • Flaky Specs
    • GithubReadmeTag
    • JS Testing
    • Setup not completed
    • userData is sometimes not loaded
    • Tracking down spec failures
    • Flaky Specs #1
  • Travis CI Segfaults
  • Bookmarks
  • User Setup in development
  • Testing in Docker locally
    • Reapproach quay containers
    • Webdriver and chrome headless
      • Using Selenium standalone
      • Chrome in docker
    • Setting up a local test env with docker
    • Traditional CI container setup
  • Peer Review
  • Rails runaway process in initialize!
    • Don't fix it
    • fix it?
  • Zeitwerk
  • Parallelize Cypress
  • Reading List
  • Sidekiq Memory Use
  • Buildkite configuration
  • Dumping heap
  • Tricking KnapsackPro
  • Debugging Ruby
  • Docker development revisited
  • gitpod development test
  • Embedding HTML in Markdown
Powered by GitBook
On this page

Was this helpful?

Zeitwerk

rails 6 autoloading behavior

Previousfix it?NextParallelize Cypress

Last updated 4 years ago

Was this helpful?

discusses this

context: Seeing (suddenly) errors loading Notifications::WelcomeNotificationWorker when trying to instantiate a new user in tests in docker.

[2] pry(#<RSpec::ExampleGroups::User::ProfileImage>)> create(:user)
NameError: uninitialized constant Notifications::WelcomeNotificationWorker
from /opt/apps/forem/app/models/notification.rb:79:in `send_welcome_notification'

Before I get much further - my suspicion is that the issue may be tied to two directories with classes in the Notifications module (app/services/notifications/ and app/workers/notifications/) and only the first getting searched.

This is only happening for me in docker (the same behavior does not happen in a local dev env).

I can confirm the Notifications module is loading correctly

[8] pry(#<RSpec::ExampleGroups::User::ProfileImage>)> show-method Notifications
                                                                               
From: /opt/apps/forem/app/services/notifications.rb:1
Module name: Notifications
Number of lines: 78

module Notifications
  def self.user_data(user)

However - I tried the dumbest thing that could possibly work, and copied the worker from workers/notifications/ to services/notifications/ and still could not autoload the class. Additionally, none of the code in either subdirectory belonging to this namespace was loaded:

[7] pry(#<RSpec::ExampleGroups::User::ProfileImage>)> constant = "Notifications::WelcomeNotificationWorker".constantize
NameError: uninitialized constant Notifications::WelcomeNotificationWorker                                             
from /opt/apps/forem/vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.3.1/lib/active_support/inflector/methods.rb:288:in `const_get'

[16] pry(#<RSpec::ExampleGroups::User::ProfileImage>)> Notifications.constants
=> []                                                 

Code is working when loaded (name is not wrong):

[2] pry(#<RSpec::ExampleGroups::User::ProfileImage>)> Notifications.constants
=> []                                                                        
[3] pry(#<RSpec::ExampleGroups::User::ProfileImage>)> load(Rails.root + "app/workers/notifications/welcome_notification_worker.rb")                                                     
=> true                                                                                                                            
[4] pry(#<RSpec::ExampleGroups::User::ProfileImage>)> Notifications.constants
=> [:WelcomeNotificationWorker]                       

https://guides.rubyonrails.org/v6.0.3/autoloading_and_reloading_constants.html