NameError (uninitialized constant Error) in Rails 7
Asked Answered
H

1

1

I recently upgraded my rails application to rails 7 and after upgrading when I try to call the call from the lib folder I am getting this error NameError (uninitialized constant Error)

I have following file structure in the lib folder

lib
 -> a
 -> b
 -> c
 -> d
 -> error
    -> test_errors.rb

whenver I call Error::TestErrors I got this error, I guess this error is something related to the Zeitwerk

Can someone please help me with this

whenver I call Error::TestErrors I got this error ideally this should work and as it is working on the another branch

History answered 16/4, 2023 at 10:11 Comment(10)
lib has not autoloaded by default in Rails since Rails 3. You either should require this code explicitly, move the code to /app which is autoloaded or add lib to the autoloading paths (not recommended). guides.rubyonrails.org/…Tappet
Does your application have lib in the autoload paths? You can inspect ActiveSupport::Dependencies.autoload_paths in a console, for example. What is "the other branch"?Bison
@XavierNoria By another branch I mean, It is working on the master branch where the app is still on the rails 6 and yes lib folder is present in ActiveSupport::Dependencies.autoload_pathsHistory
Is the other branch running in zeitwerk or classic mode?Bison
Also, when Error::TestErrors raises, is it referenced in an initializer?Bison
@XavierNoria Yes another branch is running in classic mode. coming to your second question, No it is not referenced in the initializer it is declared inside the lib folderHistory
Question is not where is defined or declared, question is where is referenced, where is the constant being used and triggering NameError. Does that happen in an initializer?Bison
@XavierNoria It is used at multiple places in the app/controllers and app/modelHistory
But that does not respond to what I asked :). Is NameError being raised from an initializer? I wonder if you are hitting guides.rubyonrails.org/….Bison
Mention of this in the upgrade guide here guides.rubyonrails.org/…Bison
B
3

The constant Error::TestErrors was autoloaded in Rails 6.x. Therefore, lib seems to be in the autoload paths and lib/error/test_errors.rb seems to define the expected constant.

The exception that you get is NameError, therefore, this constant is not being autoloaded. If it was, you'd get Zeitwerk::NameError. This constant seems to be accessed prematurely, when autoloading is still not ready.

We do not have enough evidence, but it is very likely the problem is simply that this constant is being referenced while the application boots, perhaps in an initializer. This has been deprecated since Rails 6.0, and removed in Rails 7.0. This is a conceptual change in Rails, unrelated to Zeitwerk.

If that was right, please, have a look at this section of the upgrading guide to understand how to update the initializer.

Bison answered 20/4, 2023 at 8:37 Comment(1)
Thank you so much ! Using your link I could find this and the first option worked for me. -> guides.rubyonrails.org/v7.0/…Crossness

© 2022 - 2024 — McMap. All rights reserved.