solidusio / solidus_support

Common runtime helpers for Solidus extensions.
BSD 3-Clause "New" or "Revised" License
9 stars 23 forks source link

Autoloading controllers in extension `lib` stopped working #79

Closed mamhoff closed 1 year ago

mamhoff commented 1 year ago

(originally reported here, but moving it here, as it affects not only solidus_auth_devise, but also solidus_braintree.

I'm experiencing the following error in a brand new Solidus Sandbox: Currently (Solidus main as of https://github.com/solidusio/solidus/commit/78d20c41b61c8fd3e708c3871030f4e6a5948628, solidus_auth_devise version 2.5.8) the sandbox can't find the backend sessions controller. This is the error when you try:

Started GET "/admin/" for 127.0.0.1 at 2023-06-12 15:37:29 +0200
  ActiveRecord::SchemaMigration Pluck (0.1ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by Spree::Admin::RootController#index as HTML
Redirected to http://localhost:3000/admin/login
Completed 302 Found in 340ms (ActiveRecord: 0.8ms | Allocations: 530212)

Started GET "/admin/login" for 127.0.0.1 at 2023-06-12 15:37:29 +0200

ActionController::RoutingError (uninitialized constant Spree::Admin::UserSessionsController

      Object.const_get(camel_cased_word)
            ^^^^^^^^^^
Did you mean?  UserPasswordsController

            raise MissingController.new(error.message, error.name)
            ^^^^^):
bin/rails zeitwerk:check

results in

Hold on, I am eager loading the application.

WARNING: The following directories will only be checked if you configure
them to be eager loaded:

  /home/anselm/.gem/ruby/3.1.4/gems/solidus_paypal_commerce_platform-1.0.1/app/decorators/models
  /home/anselm/.gem/ruby/3.1.4/gems/solidus_auth_devise-2.5.8/lib/decorators/backend/controllers
  /home/anselm/.gem/ruby/3.1.4/gems/solidus_auth_devise-2.5.8/lib/controllers/backend/spree

You may verify them manually, or add them to config.eager_load_paths
in config/application.rb and run zeitwerk:check again.

Otherwise, all is good!

Solidus Version:

Solidus main as of https://github.com/solidusio/solidus/commit/78d20c41b61c8fd3e708c3871030f4e6a5948628

To Reproduce

The simplest check for this is just running the rails console and typing the name of the controller we need. If all is good, the REPL should return the constant, if something's off, we get an undefined constant error:

=> Spree::Admin::UsersController
irb(main):007:0> Spree::Admin::UserSessionsController
(irb):7:in `<main>': uninitialized constant Spree::Admin::UserSessionsController (NameError)
Did you mean?  Spree::Admin::UsersController

This behavior occurs not just with solidus_auth_devise, but with any gem that has the backend controllers in lib. One example is solidus_braintree, with its SolidusBraintree::ConfigurationsController:

irb(main):002:0> SolidusBraintree
=> SolidusBraintree
irb(main):003:0> SolidusBraintree::ConfigurationsController
(irb):3:in `<main>': uninitialized constant SolidusBraintree::ConfigurationsController (NameError)

Current behavior

irb(main):002:0> SolidusBraintree::ConfigurationsController
(irb):3:in `<main>': uninitialized constant SolidusBraintree::ConfigurationsController (NameError)

Expected behavior

irb(main):002:0> SolidusBraintree::ConfigurationsController
=> SolidusBraintree::ConfigurationsController

Desktop (please complete the following information):

This stopped working with the following commit: https://github.com/solidusio/solidus_support/commit/17e871839e754bd5a247b99b21ca913f50700db9

DanielePalombo commented 1 year ago

Thank you for reporting this issue. I made a change to the autoload path of solidus_support last week. Probably this is the cause of the issue. Let me try to reproduce it locally.

elia commented 1 year ago

@DanielePalombo can you also try to add a regression test for both situations?

elia commented 1 year ago

I just got the same issue, moving from config.autoload_paths += controllers_path.glob('*') to config.autoload_paths += [controllers_path] seems to work and be consistent with https://edgeguides.rubyonrails.org/classic_to_zeitwerk_howto.html#globs-in-config-autoload-paths

I'll prepare a PR to fix this 🙋‍♂️

mamhoff commented 1 year ago

This seems to work in the situation described above (tested locally using bundle open).