Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore a folder in Zeitwerk for Rails 6?

Simple question, but somehow the answer escapes me.

In moving to Rails 6 with Zeitwerk, I get:

Please, check the "Autoloading and Reloading Constants" guide for solutions.
 (called from <top (required)> at APP_ROOT/config/environment.rb:7)
rails aborted!
Zeitwerk::NameError: wrong constant name Enforce-calls-to-come-from-aws inferred by Module from directory

  APP_ROOT/app/junkyard/enforce-calls-to-come-from-aws

Possible ways to address this:

  * Tell Zeitwerk to ignore this particular directory.
  * Tell Zeitwerk to ignore one of its parent directories.
  * Rename the directory to comply with the naming conventions.

Which seems great: that's a junk folder and should never be loaded, so ignoring it makes perfect sense.

The Zeitwerk docs at https://github.com/fxn/zeitwerk say

tests = "#{__dir__}/**/*_test.rb"
loader.ignore(tests)
loader.setup

is how you ignore a folder. Fair enough. But how do I find loader? The Rails guide on Zeitwerk autoloading (https://guides.rubyonrails.org/autoloading_and_reloading_constants.html) doesn't mention how to ignore folders directly, but does mention the autoloader stashed at Rails.autoloaders.main, so I figured that

Rails.autoloaders.main.ignore("#{__dir__}/junkyard/**/*.rb")

or

Rails.autoloaders.main.ignore("#{__dir__}/app/junkyard/**/*.rb")

would be the way to go. No luck. I've tried putting this in application.rb and in initializers/zeitwerk.rb and neither worked.

Any ideas where and how to ignore a folder with Zeitwerk within Rails?

PS: yes, I know I should just remove this from app, and I will. But the question still vexes.

like image 874
Iain Bryson Avatar asked Sep 26 '19 23:09

Iain Bryson


2 Answers

I ran into this same issue, and it turns out it was complaining about a folder name.

Adding this to application.rb may work for you:

Rails.autoloaders.main.ignore(Rails.root.join('app/junkyard'))

like image 66
ajkerr Avatar answered Nov 01 '22 08:11

ajkerr


I added this to config/initializers/zeitwerk.rb:

Rails.autoloaders.each do |autoloader|
  autoloader.ignore(Rails.root.join('app/ui'))
...
like image 7
Piers C Avatar answered Nov 01 '22 07:11

Piers C