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

  • `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). https://guides.rubyonrails.org/autoloading_and_reloading_constants.html#config-autoload-paths – max Apr 16 '23 at 10:43
  • 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"? – Xavier Noria Apr 16 '23 at 15:44
  • @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_paths` – Ghanshyam Anand Apr 16 '23 at 17:07
  • Is the other branch running in zeitwerk or classic mode? – Xavier Noria Apr 16 '23 at 19:22
  • Also, when `Error::TestErrors` raises, is it referenced in an initializer? – Xavier Noria Apr 16 '23 at 19:52
  • @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 folder – Ghanshyam Anand Apr 17 '23 at 10:17
  • 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? – Xavier Noria Apr 17 '23 at 14:17
  • @XavierNoria It is used at multiple places in the app/controllers and app/model – Ghanshyam Anand Apr 18 '23 at 03:09
  • But that does not respond to what I asked :). Is `NameError` being raised from an initializer? I wonder if you are hitting https://guides.rubyonrails.org/autoloading_and_reloading_constants.html#autoloading-when-the-application-boots. – Xavier Noria Apr 18 '23 at 10:50
  • Mention of this in the upgrade guide here https://guides.rubyonrails.org/upgrading_ruby_on_rails.html#autoloading-during-initialization – Xavier Noria Apr 18 '23 at 11:06

1 Answers1

1

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.

Xavier Noria
  • 1,640
  • 1
  • 8
  • 10
  • Thank you so much ! Using your link I could find this and the first option worked for me. -> https://guides.rubyonrails.org/v7.0/autoloading_and_reloading_constants.html#autoloading-when-the-application-boots – Ula Jun 20 '23 at 21:46