0

I have a view that ajax-loads the same partial a number of times. Each one contains the result of a rather extensive calculation which takes a number of seconds. On my machine, about 3 times out of 20, instead of the partial I get the error message "Unable to autoload constant Answers::Node::Questions::KurzSinglePunch... expected .../app/models/answers/node/questions/kurz_single_punch.rb to define it" (I shortened the text and the path for legibility). The file is obviously there, since in 17 of 20 cases it is found, and the partial displays correctly in these cases.

In case this is relevant, I use Puma in single mode on a six-year-old Mac running OS 11.6.

This is the topmost part of the error message:

LoadError in KurzReportsController#company Unable to autoload constant Answers::Node::Questions::KurzSinglePunch, 
expected /Users/marion/Documents/Projekte/myproject/app/models/answers/node/questions/kurz_single_punch.rb to define it 
Extracted source (around line #511): 
#509 else 
#510 require_or_load(expanded, qualified_name) 
*511 raise LoadError, 
"Unable to autoload constant #{qualified_name}, expected #{file_path} to define it" unless from_mod.const_defined?(const_name, false) 
#512 return from_mod.const_get(const_name) 
#513 end 
#514 elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix) 
Extracted source (around line #195): 
#193 def const_missing(const_name) 
#194 from_mod = anonymous? ? guess_for_anonymous(const_name) : self 
*195 Dependencies.load_missing_constant(from_mod, const_name) 
#196 end 
#197 
#198 # 
We assume that the name of the module reflects the nesting 
Extracted source (around line #285): 
#283 constant.const_get(name) 
#284 else 
*285 candidate = constant.const_get(name) 
#286 next candidate if constant.const_defined?(name, false) 
#287 next candidate unless Object.const_defined?(name) 
#288 Rails.root: /Users/marion/Documents/Projekte/myproject 
Application Trace app/exporters/kurz_report_extractor.rb:63:
in `infos_for_question_id' app/exporters/kurz_report_extractor.rb:59:
in `block in build_survey_hash' app/exporters/kurz_report_extractor.rb:59:
in `map' app/exporters/kurz_report_extractor.rb:59:
in `build_survey_hash' app/exporters/kurz_report_extractor.rb:15:
in `initialize' app/controllers/kurz_reports_controller.rb:27:
in `new' app/controllers/kurz_reports_controller.rb:27:
in `block in company' app/controllers/kurz_reports_controller.rb:25:
in `each' app/controllers/kurz_reports_controller.rb:25:in `company'

After that there is a long section with a Framework trace.

How can it be that Rails is sometimes unable to find this file? And how can I fix this?

MDickten
  • 105
  • 1
  • 10
  • 1
    are you defining the variable in here? `myproject/app/models/answers/node/questions/kurz_single_punch.rb`, if that's the case you might have a typo, if not, you have a name collision, so you'll need to that file somewhere else. Now if you are having troubles loading a new path, that's another question, you'll have to eager_load or autoload the new folder, I have an answer for rails 6, but that should help to find the right answer https://stackoverflow.com/questions/58866709/how-can-i-extend-gem-class-in-rails-6-zeitwerk-without-breaking-code-reloading/61387804#61387804 – Alexis Jan 27 '23 at 17:24
  • @Alexis As I said, the code executes correctly in 17 of 20 cases. If I had an error of the kind you think, it could never execute correctly. – MDickten Jan 30 '23 at 08:29
  • the problem with versions of rails prior to 6 was that is actually possible, in dev mode eager load is disabled and is dynamic for hot-swapping deps with code changes, you probably need a require_dependency somewhere https://guides.rubyonrails.org/v5.2/autoloading_and_reloading_constants.html#require-dependency. If you want the long and detailed answer, last week, the creator of zeitwerk(the lib that fixed this in rails 6) gave a keynote on railsconf explaining all of this https://youtu.be/DzyGdOd_6-Y – Alexis Jan 30 '23 at 09:36
  • @Alexis Wow thanks. I'll look at it. Do you want to make this into an answer? – MDickten Jan 31 '23 at 11:31
  • nah don't worry, I'll need to actually see your code and find the problem to feel good giving an answer, just answer it yourself and give me a shoutout if it helped, good luck – Alexis Feb 05 '23 at 20:04
  • @Alexis I've watched most of the video, and maybe I've got an idea. When in dev mode, does the autoloader reload the file for each call, or at least check it's the same? Xavier Noria in this talk mentions how there is a list kept for all known constants, and one reason for getting the error message might be that it's simply not the correct error message but that, owing to too many things going on in parallel, autoload somehow fails. The point is: _If I insert a 10 msec wait between the AJAX calls the errors go away_. Maybe like this I enable autoload to complete successfully every time? – MDickten Feb 07 '23 at 10:34
  • it shouldn't, the autoloader use to track two things, one was changes on the code so the entire branch for that file would get reloaded and the other was when new requirements would get trigger around the code, let's say you call a namespaced model, you would get the leaf and all the way until that node, but if you have another similar namespaced somewhere else the collision would appear, and the same thing would happen the other way around. I was thinking that you might have different tests that were reloading the code, but that sounded like is was happening on a live environment. --continue – Alexis Feb 09 '23 at 22:26
  • try setting `config.eager_load = true, config.cache_classes = true` in development.rb that might help to pinpoint the error, now if I were you I would highly consider upgrading to rails 6.0.6.1, that version is really likely to had fixed your problem or to provide an straightforward solution, that kind of jump shouldn't be that difficult, I'm guessing that you are on 5.2.something, upgrades from majors like 5.2 to 6.0 are fully documented and 6.0.6.1 is just the last version released for 6.0 – Alexis Feb 09 '23 at 22:32
  • There aren't any tests running.There are no code changes happening anywhere. This is what's so funny. I load that page and pop, pop, pop go the AJAX calls and sometimes they fail. EDIT Yes, I'm on 5.2.2.4. – MDickten Feb 14 '23 at 08:48

0 Answers0