Knock: Unable to autoload constant Engine

Created on 24 Aug 2015  路  12Comments  路  Source: nsarno/knock

Getting the following error (I followed the Auth0 manual)

LoadError (Unable to autoload constant Engine, expected /Users/mikhaildubov/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/knock-1.3.0/lib/knock/engine.rb to define it):

Any idea why this is happening? Appreciate any pointers.

help wanted

All 12 comments

I need more informations in order to help you. At least a complete stack trace. And maybe the code related to knock you added to your project. Thanks.

Yep, so I eventually figured out it was driven by the jsonapi_resources gem. Somehow the two do not communicate correctly. I unfortunately don't have the stack trace anymore, but the problem was in this line: https://github.com/cerebris/jsonapi-resources/blob/master/lib/jsonapi/link_builder.rb#L19

I eventually reverted to the jwt gem and it's all working now. It's a shame because knock seemed a lot cleaner.

If you give me more details on how to reproduce this issue, I'll be able to figure out if it comes from knock or jsonapi-resources and fix it. Thanks for your feedback.

I think it comes from the jsonapi_resources. I have very typical API with jsonapi_resources and the recommended knock configuration, exactly like in both manuals. I'll try to create a toy app when I have time.

Sounds good. Maybe you can start from this jsonapi-resources example app.

Hi, apologies for the long wait but I was finally able to replicate the problem in a demo app. It seems that the problem is somehow related to the newer version of jsonapi_resources. Here is the app: https://github.com/duboff/peeps

To recreate, launch and try visiting http://localhost:3000/api/contacts

Thank you for the demo app!

I've been trying to understand what should be fixed. And it seems to me that neither knock or jsonapi-resources is doing anything wrong.

But it makes no sense to me why the following happens:

irb(main):005:0> Api::Engine
LoadError: Unable to autoload constant Engine, expected /Users/arno/workspace/peeps/vendor/bundle/ruby/2.2.0/gems/knock-1.3.0/lib/knock/engine.rb to define it
[...]

and

irb(main):006:0> Engine
LoadError: Unable to autoload constant Engine, expected /Users/arno/workspace/peeps/vendor/bundle/ruby/2.2.0/gems/knock-1.3.0/lib/knock/engine.rb to define it
[...]

I believe something like this should happen instead:

irb(main):007:0> Wrong
NameError: uninitialized constant Wrong
[...]

The path lib/knock/engine.rb should NOT be expected to define a constant Engine.
It should only be expected to define a Knock::Engine constant.

irb(main):008:0> Knock::Engine
=> Knock::Engine

I feel like this is a weird behavior from rails autoloading, but maybe I just don't understand well enough how this works. I'll keep digging but any help on this is welcome.

I think it might be related to this: https://github.com/cerebris/jsonapi-resources/pull/443 which was merged recently so maybe it will go away in the next version

Unfortunaltey no, this issue is unrelated.

I opened another issue here: cerebris/jsonapi-resources/issues/450, so I'm closing this one for the moment.

This issue exists purely within knock and can be reproduced without jsonapi-resources. The line config.eager_load_paths += Dir["#{config.root}/lib/**/"] in knock/engine.rb should be replaced with paths.add 'lib', eager_load: true.

@tindron how did you find this fix? i'm curious because just had the same problem and your solution worked. Thanks!

Was this page helpful?
0 / 5 - 0 ratings