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
Prefer require_relative
#8748
Comments
Hi @marcandre 👋🏻 I know this is a bit untraditional (& you might be aware of this already) but there's this cop Rationale at https://docs.rubocop.org/rubocop-packaging/cops_packaging.html#require-relative-hardcoding-lib-rationale Please let me know what you think? |
We've been converting our codebase to use require_relative for some time. It works around some pretty terrible dir walking behavior in rubygems that is particularly slow on Windows where disk IO performance in Ruby is less than ideal. It's been on my todo list for a while to write a rule to do this, but we cranked through it with this script. It also uses https://gist.github.com/tas50/65682084613887ac7e4a4900cfd1e495 |
What about ruby's |
Matz has not been persuaded yet to add a |
I didn't mean the |
Yes
Exclusively? Especially without a |
I don't see why not? https://medium.com/rubycademy/the-autoload-method-in-ruby-11fd079562ef writes it our cleanly with examples. |
...with bad examples using paths like Please note that the decision of |
I can definitely support the idea that |
FWIW, there's already a Packaging/RequireHardcodingLib cop which "almost" checks the same thing and autocorrects the offenses. Why I say almost is because it only checks for those calls which are mapped to the That said, I am sure it can be extended to a more generic cop with a few tweaks. I can put together a PR (or take a stab at it) if you'd like so? |
Yeah, that'd be great. |
FWIW, for downstream distributors, and for other use cases, require_relative from the test files to "[...]/../lib" is a problem because for example you can't run the tests against the code that is installed system-wide (this is an actual use case in Debian for integration testing of the distribution). This what @utkarsh2102 has been working on, starting with GSOC this year, and now continuing beyond the internship. In general, I think that require_relative is OK within lib/, but I'm a little dubious about the value of saying that everyone should do it. My personal taste is that I prefer requires to be more explicit, because that makes it more clear what exactly they are referring to. |
@terceiro what do you mean by 'more explicit'? Can you give an example? |
I'd like to see an example as well, as I have a hard time understanding what is being implied. |
Hi @coding-bunny,
Here's the rationale that might suffice: https://docs.rubocop.org/rubocop-packaging/cops_packaging.html#require-relative-hardcoding-lib-rationale. Please feel free to ask if something's not clear! |
That doesn't really explain it.
|
@coding-bunny the example with |
I'm totally fine with the For normal Ruby projects, |
Just to throw my 2 cents here. I think mostly everyone here agrees that I'd like to make the case that anything outside of the current gem "section" ( Also, since prettiness has been mentioned, I personally consider things like I think it'd be nice that a |
Hi @coding-bunny,
Eeeks! That's certainly not what's being implied here. The whole idea of providing that as an example was - But yes, I think it's better I just trim off the |
This is based on the comment and the discussion of #rubocop/rubocop/issues/8748#issuecomment-699478216 Signed-off-by: Utkarsh Gupta <utkarsh@debian.org>
This is based on the comment and the discussion of #rubocop/rubocop/issues/8748#issuecomment-699478216 Signed-off-by: Utkarsh Gupta <utkarsh@debian.org>
It's been a while since I looked into This may help to provide some historical context on decisions to use |
Allows `bin/github_changelog_generator` to be invoked from anywhere. This facilitates easier ad hoc testing locally. This commit leaves specs unchanged on purpose. See: rubocop/rubocop#8748
To load dependencies withing a gem/app,
require_relative
should always be preferred torequire
We should check for any instance of
(send nil? require `{:__dir__ :__FILE__})
and raise an offense.I can't event think of a single instance where
require
should be called with an interpolated string tbhThe text was updated successfully, but these errors were encountered: