我正在尝试将我的应用程序从Rails 5.2升级到6.1,并使用Zeitwerk autoload,而且我在自动加载/lib类时遇到了一些问题。
我把这个包括在config/application.rb里
config.load_defaults 5.2
config.autoloader = :zeitwerk
config.enable_dependency_loading = true
config.autoload_paths += Dir[Rails.root.join('lib/**/*')]
config.eager_load_paths += Dir[Rails.root.join('lib/**/*')]而且,当我运行zeitwerk:check时,它会提醒我:
Hold on, I am eager loading the application.
expected file lib/facades/coconut/v2/foo.rb to define constant Foo声明如下:
# /lib/facades/coconut/v2/foo.rb
module Coconut
module V2
class Foo
# ...
end
end
end当我尝试调试它(将一个binding.pry放在某个测试文件中)时,Zeitwerk说它没有定义,直到我调用它时没有模块(名称空间):
[1] pry(#<#filename>)> Coconut::V2::Foo
NameError: uninitialized constant Coconut::V2::Foo
from (pry):1:in `setup`
[2] pry(#<#filename>)> Foo
Zeitwerk::NameError: expected file /my-app/lib/api/coconut/v2/foo.rb to define constant Foo, but didn't
from /usr/local/bundle/gems/zeitwerk-2.5.4/lib/zeitwerk/loader/callbacks.rb:25:in `on_file_autoloaded'
[3] pry(#<#filename>)> Coconut::V2::Foo
=> Coconut::V2::Foo这听起来很奇怪,因为在/lib中有另一个类遵循相同的结构,但是它运行得很好,例如
# /lib/api/services/youtube/client.rb
module Services
module Youtube
class Client
# ...
end
end
end在一些测试中使用binding.pry检查它:
pry(#<#filename>)> Services::Youtube::Client
=> Services::Youtube::Client有没有人有这个问题或者知道会发生什么?
系统:
发布于 2022-05-16 23:54:09
自动路径是根目录,而不是其内容。
您需要将通配符作为文档化的这里删除。
https://stackoverflow.com/questions/72266610
复制相似问题