You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While upgrading an existing Rails app to v6.1 and enabling the zeitwerk autloader, I stumbled across this error, when reloading the application in development mode:
superclass mismatch for class FinderByTitle (TypeError)
echo'Refinery::Page; reload!; Refinery::Page'| rails c
# ># Running via Spring preloader in process 408055# Loading development environment (Rails 6.1.7.2)# Switch to inspect mode.# Refinery::Page; reload!; Refinery::Page# Reloading...# ~/.asdf/installs/ruby/3.2.0/lib/ruby/gems/3.2.0/bundler/gems/refinerycms-aee49a603860/pages/lib/refinery/pages/finder.rb:62:in `<module:Pages>': superclass mismatch for class FinderByTitle (TypeError)
When reloading, zeitwerk reloads the model, which in turn loads the finder again. But because the file is named finder.rb, only Refinery::Pages::Finder gets unloaded and reloaded again. All other classes stay loaded and would be patched by the reload. But because Refinery::Pages::Finder has been reloaded and initialized again, it got a new object_id, thus triggering the superclass mismatch error.
One solution, that fixed the error, was to move all sibling-classes of Refinery::Pages::Finder into it, making them children of Refinery::Pages::Finder. Because they are only called inside Refinery::Pages::Finder itself, this should not make problems.
I am not sure, if this would be the best or most elegant solution, if it would introduce side effects or if there are other parts of refinery, that suffer the same issues.
The text was updated successfully, but these errors were encountered:
While upgrading an existing Rails app to
v6.1
and enabling the zeitwerk autloader, I stumbled across this error, when reloading the application in development mode:This happens everywhere, code was reloaded.
I tried to reproduce the problem in a minimal setup by initializing a new rails 6.1 app, including
refinerycms
in the Gemfile:webit-de/refinerycms-zeitwerk-reloading-reproduction
The issue stems from the definition of
Refinery::Pages::Finder
and its sibling classes (Refinery::Pages::FinderByTitle
and so on). These are all defined beside each other inpages/lib/refinery/pages/finder.rb
which gets required inpages/app/models/refinery/page.rb
.When reloading, zeitwerk reloads the model, which in turn loads the finder again. But because the file is named
finder.rb
, onlyRefinery::Pages::Finder
gets unloaded and reloaded again. All other classes stay loaded and would be patched by the reload. But becauseRefinery::Pages::Finder
has been reloaded and initialized again, it got a newobject_id
, thus triggering thesuperclass mismatch
error.One solution, that fixed the error, was to move all sibling-classes of
Refinery::Pages::Finder
into it, making them children ofRefinery::Pages::Finder
. Because they are only called insideRefinery::Pages::Finder
itself, this should not make problems.I am not sure, if this would be the best or most elegant solution, if it would introduce side effects or if there are other parts of refinery, that suffer the same issues.
The text was updated successfully, but these errors were encountered: