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

superclass mismatch on Rails/Zeitwerk reload #3523

Closed
richard-kramer opened this issue Jan 31, 2023 · 1 comment
Closed

superclass mismatch on Rails/Zeitwerk reload #3523

richard-kramer opened this issue Jan 31, 2023 · 1 comment

Comments

@richard-kramer
Copy link
Contributor

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)

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

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)

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 in pages/lib/refinery/pages/finder.rb which gets required in pages/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, 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.

@richard-kramer
Copy link
Contributor Author

I created a pull request (#3524) with the changes to finder.rb, that might be a viable solution.

@parndt parndt closed this as completed in 5cbbd14 May 12, 2023
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

No branches or pull requests

1 participant