Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 7 autoloading issue #110

Merged
merged 2 commits into from
Nov 2, 2021
Merged

Conversation

woller
Copy link
Contributor

@woller woller commented Nov 1, 2021

When using both minitest-spec-rails and view_component in the same project, tests would blow up with the following stack trace:

Traceback (most recent call last):
        58: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/rake-13.0.6/lib/rake/rake_test_loader.rb:6:in `<main>'
        57: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/rake-13.0.6/lib/rake/rake_test_loader.rb:6:in `select'
        56: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/rake-13.0.6/lib/rake/rake_test_loader.rb:21:in `block in <main>'
        55: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/rake-13.0.6/lib/rake/rake_test_loader.rb:21:in `require'
        54: from /Users/woller/code/traels/open_source/minitest-spec-rails/test/cases/action_controller_test.rb:1:in `<top (required)>'
        53: from /Users/woller/code/traels/open_source/minitest-spec-rails/test/cases/action_controller_test.rb:1:in `require'
        52: from /Users/woller/code/traels/open_source/minitest-spec-rails/test/test_helper.rb:1:in `<top (required)>'
        51: from /Users/woller/code/traels/open_source/minitest-spec-rails/test/test_helper.rb:1:in `require'
        50: from /Users/woller/code/traels/open_source/minitest-spec-rails/test/dummy_app/init.rb:42:in `<top (required)>'
        49: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/railties-7.0.0.alpha2/lib/rails/railtie.rb:207:in `method_missing'
        48: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/railties-7.0.0.alpha2/lib/rails/railtie.rb:207:in `public_send'
        47: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/railties-7.0.0.alpha2/lib/rails/application.rb:369:in `initialize!'
        46: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/railties-7.0.0.alpha2/lib/rails/initializable.rb:60:in `run_initializers'
        45: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/2.7.0/tsort.rb:205:in `tsort_each'
        44: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/2.7.0/tsort.rb:226:in `tsort_each'
        43: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/2.7.0/tsort.rb:347:in `each_strongly_connected_component'
        42: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/2.7.0/tsort.rb:347:in `call'
        41: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/2.7.0/tsort.rb:347:in `each'
        40: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/2.7.0/tsort.rb:349:in `block in each_strongly_connected_component'
        39: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/2.7.0/tsort.rb:431:in `each_strongly_connected_component_from'
        38: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/2.7.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
        37: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/2.7.0/tsort.rb:228:in `block in tsort_each'
        36: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/railties-7.0.0.alpha2/lib/rails/initializable.rb:61:in `block in run_initializers'
        35: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/railties-7.0.0.alpha2/lib/rails/initializable.rb:32:in `run'
        34: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/railties-7.0.0.alpha2/lib/rails/initializable.rb:32:in `instance_exec'
        33: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/view_component-2.42.0/lib/view_component/engine.rb:21:in `block in <class:Engine>'
        32: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/zeitwerk-2.5.1/lib/zeitwerk/kernel.rb:35:in `require'
        31: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/zeitwerk-2.5.1/lib/zeitwerk/kernel.rb:35:in `require'
        30: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/view_component-2.42.0/lib/view_component/base.rb:14:in `<top (required)>'
        29: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/view_component-2.42.0/lib/view_component/base.rb:15:in `<module:ViewComponent>'
        28: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/zeitwerk-2.5.1/lib/zeitwerk/kernel.rb:35:in `require'
        27: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/zeitwerk-2.5.1/lib/zeitwerk/kernel.rb:35:in `require'
        26: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/actionview-7.0.0.alpha2/lib/action_view/base.rb:12:in `<top (required)>'
        25: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/actionview-7.0.0.alpha2/lib/action_view/base.rb:141:in `<module:ActionView>'
        24: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/actionview-7.0.0.alpha2/lib/action_view/base.rb:279:in `<class:Base>'
        23: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/activesupport-7.0.0.alpha2/lib/active_support/lazy_load_hooks.rb:51:in `run_load_hooks'
        22: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/activesupport-7.0.0.alpha2/lib/active_support/lazy_load_hooks.rb:51:in `each'
        21: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/activesupport-7.0.0.alpha2/lib/active_support/lazy_load_hooks.rb:52:in `block in run_load_hooks'
        20: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/activesupport-7.0.0.alpha2/lib/active_support/lazy_load_hooks.rb:66:in `execute_hook'
        19: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/activesupport-7.0.0.alpha2/lib/active_support/lazy_load_hooks.rb:61:in `with_execution_control'
        18: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/activesupport-7.0.0.alpha2/lib/active_support/lazy_load_hooks.rb:71:in `block in execute_hook'
        17: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/activesupport-7.0.0.alpha2/lib/active_support/lazy_load_hooks.rb:71:in `class_eval'
        16: from /Users/woller/code/traels/open_source/minitest-spec-rails/lib/minitest-spec-rails/railtie.rb:25:in `block (2 levels) in <class:Railtie>'
        15: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/zeitwerk-2.5.1/lib/zeitwerk/kernel.rb:35:in `require'
        14: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/zeitwerk-2.5.1/lib/zeitwerk/kernel.rb:35:in `require'
        13: from /Users/woller/code/traels/open_source/minitest-spec-rails/lib/minitest-spec-rails/init/action_view.rb:22:in `<top (required)>'
        12: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/zeitwerk-2.5.1/lib/zeitwerk/kernel.rb:35:in `require'
        11: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/zeitwerk-2.5.1/lib/zeitwerk/kernel.rb:35:in `require'
        10: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/actionview-7.0.0.alpha2/lib/action_view/test_case.rb:10:in `<top (required)>'
         9: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/actionview-7.0.0.alpha2/lib/action_view/test_case.rb:12:in `<module:ActionView>'
         8: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/actionview-7.0.0.alpha2/lib/action_view/test_case.rb:13:in `<class:TestCase>'
         7: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/actionpack-7.0.0.alpha2/lib/action_controller/railties/helpers.rb:19:in `inherited'
         6: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/actionpack-7.0.0.alpha2/lib/abstract_controller/helpers.rb:148:in `helper'
         5: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/actionpack-7.0.0.alpha2/lib/action_controller/metal/helpers.rb:104:in `modules_for_helpers'
         4: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/actionpack-7.0.0.alpha2/lib/abstract_controller/helpers.rb:170:in `modules_for_helpers'
         3: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/actionpack-7.0.0.alpha2/lib/abstract_controller/helpers.rb:170:in `map!'
         2: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/actionpack-7.0.0.alpha2/lib/abstract_controller/helpers.rb:177:in `block in modules_for_helpers'
         1: from /Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/activesupport-7.0.0.alpha2/lib/active_support/core_ext/string/inflections.rb:74:in `constantize'
/Users/woller/.rbenv/versions/2.7.3/lib/ruby/gems/2.7.0/gems/activesupport-7.0.0.alpha2/lib/active_support/inflector/methods.rb:280:in `constantize': uninitialized constant ApplicationHelper (NameError)

After some investigation I traced the problem to the way MiniTestSpecRails::Init::ActionViewBehavior is included on line 22 of lib/minitest-spec-rails/init/action_view.rb. Rails 7 drops support for the classic autoloader and now only supports zeitwerk. Wrapping the include statement in a to prepare block, solves the issue:

module MiniTestSpecRails
  module Init
    module ActionViewBehavior
      extend ActiveSupport::Concern

      included do
        class_attribute :_helper_class
        register_spec_type(/(Helper|View)( ?Test)?\z/, self)
        register_spec_type(self) { |desc| desc.is_a?(Class) && desc < self }
        extend Descriptions
      end

      module Descriptions
        def described_class
          determine_default_helper_class(name)
        end
      end
    end
  end
end

Rails.application.reloader.to_prepare do
  ActionView::TestCase.send :include, MiniTestSpecRails::Init::ActionViewBehavior
end

For more details, see: https://edgeguides.rubyonrails.org/autoloading_and_reloading_constants.html#use-case-1-during-boot-load-reloadable-code

The same pattern for including code is used in all the other parts of the railtie and should probably be patched as well, but I have not experienced any other issues with this gem and Rails 7

This issue seems similar to #109 and I suspect, that this might solve that issue as well.

@metaskills metaskills merged commit 8e3d79d into metaskills:master Nov 2, 2021
@metaskills
Copy link
Owner

Thanks! I just pushed v6.1.0 with this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants