-
-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrading zeitwerk to 2.6.0 causes a warning related to good_job #616
Comments
I don't understand the warning, as it seems to contradict the Zeitwerk documentation on third-party namespaces, which GoodJob already seems to follow. |
I came here to say the same thing - I think the documentation was not synchronised with this change. I had a quick play locally, using the |
Thanks everyone for flagging and discussing 🙌 This is really helpful for me. I see two different directions to go:
I am leaning towards the 2nd option. I will see if I can require the |
Let me explain. With the pass of time I've seen a few gems with extra directories under Typical example is a gem that provides Rails generators: lib/rails/generators/my_generator.rb
lib/my_gem
lib/my_gem.rb
Bottom line, that In this particular case, Zeitwerk::Loader.for_gem(warn_on_extra_files: false) The documentation has been updated in fxn/zeitwerk@9daa7b8. |
@bensheldon Just read your reply. Your 2nd option is totally cool too. |
@fxn thank you for hopping in this issue ❤️ Given that the Zeitwerk Readme uses the example of an ActiveJob adapter, I think I will instead follow your instructions 😄 https://github.com/fxn/zeitwerk#reopening-third-party-namespaces |
@fxn one more thought. I'd like to avoid forcing a Zeitwerk dependency upgrade. Does this seem like a reasonable version check? zeitwerk_options = Gem::Version.new(Zeitwerk::VERSION) >= Gem::Version.new("2.6.0") ? { warn_on_extra_files: false } : {}
Zeitwerk::Loader.for_gem(**zeitwerk_options).tap do |loader| |
This also seems to work as a workaround. I worry that implying to Zeitwerk that "I know what I'm doing" with Zeitwerk::Loader.for_gem.tap do |loader|
loader.inflector.inflect({
"cli" => "CLI",
})
loader.ignore("#{__dir__}/generators")
loader.ignore("#{__dir__}/active_job")
loader.push_dir("#{__dir__}/active_job/queue_adapters", namespace: ActiveJob::QueueAdapters)
loader.setup
end |
@bensheldon I like this one even as a replacement for the documentation, because it forces the user to EDIT: Actually no, when I read the snippet I didn't realize it was ignoring |
@bensheldon what do you think about Zeitwerk::Loader.for_gem.tap do |loader|
loader.inflector.inflect({
"cli" => "CLI",
})
loader.ignore("#{__dir__}/generators")
loader.push_dir("#{__dir__}/good_job/adapter", namespace: ActiveJob::QueueAdapters)
loader.setup
end ? |
@fxn hmmm. I don't really like mixing auto and manually nested constants in the same I think I prefer doing both |
On the other hand it feels weird to me to have active code below an ignored directory. |
Let me also say that the flag does not have to feel like hubris at all, eh? That is a pretty standard thing. You have a warning in tools for something that commonly is worth mentioning, but you may be in a situation where the warning is not warranted, and the tool gives you a way to disable. |
@fxn thank you for that 🙏 I deployed the version with the ignore+push_dir. I guess in my mental model of Zeitwerk I'm opting out of the default code-loading algorithm, and then declaring a special-purpose one. |
@bensheldon problem is, this configuration is inconsistent and I'll do something about it. Either raise if a root directory is a descendant of an ignored one, or ignore that root directory on traversal. In either case, your patch would not work. So, I see three options:
From them, (1) is the most simple and the recommended one after sleeping on it. But the other two are good too and aligned with the autoloading interface. |
There is a 4th option, which is to use a regular But, really, the flag is the idiomatic one. |
@fxn ok sounds like I should use the flag 😄 Thank you again for the help and discussion here 🙏 Just in case it got lost, my reluctance for the flag is less around turning it off (though I do like helpful warnings), so much as it is about the need for a hard version check to turn it off. e.g. zeitwerk_options = Gem::Version.new(Zeitwerk::VERSION) >= Gem::Version.new("2.6.0") ? { warn_on_extra_files: false } : {}
Zeitwerk::Loader.for_gem(**zeitwerk_options).tap do |loader|
#... As an argument, unless I'm mistaken (is there a standard way to introspect on args?), it's not possible to do feature checking and thus requires the messier (imo) version checking. Alternatively for example, If it was, for example, a class configuration it would make me less worried about introducing a future problem: Zeitwerk::Loader.warn_on_extra_files = false if Zeitwerk::Loader.respond_to?(:warn_on_extra_files=) |
Hey! Maybe
returns |
What do you think about an allow list instead? Something like loader.do_not_warn_about("#{__dir__}/active_job") possibly with a better name? Would that be a better interface for you? |
@fxn I think that's an improvement. I think it still leaves me with the future thought "Why?" and I'd probably end up writing it like this as a note to my future self: loader.do_not_warn_about("#{__dir__}/active_job") # already required constants above Would it be possible to make the flag assertive of the prevention? The following is some sloppy wording, but hopefully the suggestion makes sense: e.g. That would create some symmetry between what's happening outside of the loader configuration block, and within. (unless I'm utterly misunderstanding the source of the warning). require 'active_job'
require 'active_job/queue_adapters'
Zeitwerk::Loader.for_gem.tap do |loader|
loader.already_required_external_constants)_within("#{__dir__}/active_job")
loader.setup
end Or to try to move the configuration even closer together, would it be possible to do the require itself within the configuration block? e.g.something like loader.prerequire_constants("#{__dir__}/active_job") do
require 'active_job'
require 'active_job/queue_adapters'
end |
It's a tough one, that is why I went with the simplest solution. Most of the time anything extra should be ignored ( However, I cannot tell from the existence of the constant if the directory is legit to not warn ( I'll think about it, thanks for the discussion 😀. |
Hi @bensheldon
Today I upgraded
zeitwerk
to v.2.6.0
. Running my testsuite now producesThe zeitwerk changelog describes the warning. It probably makes more sense to you than to me ;)
The text was updated successfully, but these errors were encountered: