Skip to content
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

The documentation on collapsing directories seems to be incorrect #131

Closed
piotaixr opened this issue Aug 18, 2020 · 1 comment
Closed

The documentation on collapsing directories seems to be incorrect #131

piotaixr opened this issue Aug 18, 2020 · 1 comment

Comments

@piotaixr
Copy link

I'm moving a small legacy gem to use zeitwerk for loading: https://gitlab.com/ekylibre/onoma

I have the lib/onoma/models folder that contains classes that are in the Onoma module and not Onoma::Models so, according to the documentation, I tried to use Zeitwerk::Loader#collapse like the documentation suggest it. Neither onoma/models nor */models worked.

Inside the gem and for running the tests, the working solution was to use loader.collapse('lib/onoma/models') (here).

But, when requiring this gem in a small Rails project recently moved to Rails 6 https://gitlab.com/ekylibre/open-nomenclature-org/ i'm greeted with this error: Zeitwerk::NameError: expected file /app/vendor/bundle/ruby/2.7.0/bundler/gems/onoma-001b654dd2a2/lib/onoma/models/document_category.rb to define constant Onoma::Models::DocumentCategory, but didn't

After fiddling for a time with the configuration the working solution was to collapse using: loader.collapse("#{__dir__}/onoma/models")

Can someone confirm this or point me in the right direction if i'm doing sth wrong?

@fxn
Copy link
Owner

fxn commented Aug 18, 2020

Yes, you need an absolute path.

In general, the README uses __dir__ in the majority of the examples, but in that particular section it uses relative names. Maybe that should be changed.

The collapse method does not know where it is called. All methods in Zeitwerk that receive paths, resolve relative ones against the current working directory at runtime. That works fine if you control the loader and the current working directory, but if your code is going to be moved (as it is the case in a gem), you need to configure an absolute path to not depend on the current working directory at runtime.

@fxn fxn closed this as completed in 8167a8d Aug 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants