jaredonline / google-authenticator

Ruby gem to implement Google's MFA authenticator
MIT License
307 stars 84 forks source link

`RailsAdapter::LoadedTooLateError` occurs if Zeitwerk is enabled #75

Closed kg8m closed 1 year ago

kg8m commented 1 year ago

Hi. Thank you for developing this gem.

I have an issue where GoogleAuthenticatorRails::ActionController::RailsAdapter::LoadedTooLateError occurs when running RSpec tests on Rails with Zeitwerk. This issue is the same as mentioned in https://github.com/jaredonline/google-authenticator/pull/63#issuecomment-1159866679.

This issue is caused by defined?(::ApplicationController) always returning "constant" when Zeitwerk is enabled. Zeitwerk uses Ruby's built-in autoload, so this issue is originally caused by autoload.

I'll create a pull request to fix this issue soon. I hope you can check it out.


Note: You can reproduce the autoload and defined? behavior as follows:

  1. Put sample.rb:
    class Sample
    end
  2. Run ruby -e 'p defined?(Sample)' in your shell:
    • You will see nil because Sample isn't loaded
  3. Run ruby -e 'autoload :Sample, "./sample"; p defined?(Sample)' in your shell:
    • You will see "constant" even though Sample isn't loaded because Sample is an autoload constant
renodor commented 1 year ago

We are experiencing the same issue.

Rspec error when running specs:

Failure/Error: require "rspec/rails"

GoogleAuthenticatorRails::ActionController::RailsAdapter::LoadedTooLateError:
  GoogleAuthenticatorRails is trying to prepend a before_filter in ActionController::Base.  Because you've already defined ApplicationController, your controllers will not get this before_filter.  Please load GoogleAuthenticatorRails before defining ApplicationController.
jaredonline commented 1 year ago

Heya @kg8m and @renodor just pushed 3.1.0 of the gem with your changes; should be fixed!

kg8m commented 1 year ago

Thank you so much!