How to ignore a folder in Zeitwerk for Rails 6?

让人想犯罪 __ 提交于 2020-02-03 08:36:05

问题


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.


回答1:


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'))



来源:https://stackoverflow.com/questions/58125879/how-to-ignore-a-folder-in-zeitwerk-for-rails-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!