I am currently migrating an old Rails application to Rails 6.
It seems that some files in the project are not consistent with the classes defined in it. I don't see this error when running the tests of the application, but after deploy I receive errors like:
Zeitwerk::NameError: expected file /app/my?_/app/lib/multi_io.rb to define constant MultiIo, but didn't
In previous SO questions I could find suggestions to:
- Rails 6: Zeitwerk::NameError doesn't load class from module set
config.autoloader = :classic
- How to ignore a folder in Zeitwerk for Rails 6? ignore a given path
- Autoloading and Zeitwerk Mode in Rails 6 tweak
config.autoload_path
I am not looking for a workaround so I corrected the file raising the exception.
I would like to find a programmatical way to detect all inconsistent path/class names without running the application in production mode.
So far, three options came to my mind:
- A rake task to validate the autoloading (which is not provided by the gem). Does it exists? Would you have a snippet to run from the rails console in development env instead?
- A rubocop cop or another static code analyser. There is any?
- Force the eager load in the test environment
What it the suggested approach to validate my code without requiring several iterations/deployments?
thank you very much in advance
zeitwerk:check
task? It detects autoloading issues, including things like issues with constant names (classic/underscore vs. zeitwerk/camelize). – Blond