I’m trying to load a module (defined in lib/) in one of my test cases.
The error:
Error:
VendorApis::FooVendorTest#test_fetching_data_works_properly:
Zeitwerk::NameError: expected file /home/blaine/project/lib/vendor_apis/foo_vendor.rb to define constant VendorApis::FooVendor, but didn't
app/models/inv_procure/vendor_api.rb:6:in `get_api_class_const'
app/models/inv_procure/vendor_api_mapping.rb:42:in `api_instance'
test/lib/vendor_apis/foo_vendor_test.rb:6:in `block in <class:FooVendorTest>'
This is how it’s being loaded:
def get_api_class_const
require "./lib/" + self.lib_path
return Object.const_get(self.class_path)
end
I think Zeitwerk (I don’t know much about this) is expecting a constant to be in the file, based on the file name, i.e. VendorApis::FooVendor. However, that is not the case. The file defines a class constant FooVendor::ApiConsumer. Is there a way around this?
By default, the lib directory is not in the autoload paths. Your application has to be adding it by hand.
The reason is that lib in the autoload paths is normally a bit tricky because the lib directory contains assorted stuff like Rake tasks, etc.
So, one question to ponder is: Does the application really need lib in the autoload paths?
If the answer is yes, do you need all of it or only part? Do you need autoload and reload? Because if you have something in the autoload paths, it is expected to follow certain conventions.
Once that understood, I will be able to help you in either case.
Just to express a general super frustration and borderline feeling insulted. By Zeitwerk. That is supposed to be advanced technology that helps? It should work for an out-of-the-box install instead of breaking everything !!
I have a similar issue, in a fresh install (!!), can’t get solutions anywhere.
Will try to reproduce the error and paste the messages here. Meanwhile, to answer your question: At the moment I see the issue as over-reliance on “convention over configuration” - by Zeitwerk. It raises an error if some Gem does not self-report its VERSION. I had actioncable install out-of-the-box for Rails 7.1.3.2 And that gem happened to violate the VERSION reporting convention. And Zeitwerk apparently was intolerant to that violation. I did a temporary hack to suppress the version requirement somehow, to get past it. But that does not fix it permanently. Perhaps not exact same issue. But related, no? Sorry - I was so frustrated at the moment perhaps I misplaced my rant.
EDIT: And I couldn’t find a way to loosen that strictness, with provided configuration.
Yep. Will try. The reason I keep saying “try” is it will 1) take a bit of time (20 mins?) to reproduce it and 2) it involves corporate proprietary code (wrapper/container stuff) that I need to skirt around. Please bear with me.