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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify Ruby files order through magic comments #3748

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

palkan
Copy link
Contributor

@palkan palkan commented Jul 19, 2017

Disclaimer

I marked this PR as Experimental to emphasize that it's the main purpose is to discuss the problem (or this is not a problem at all?).

The problem

Currently, Ruby files are loaded and compiled in lexicographical order. Thus we have to use such tricks as using file prefixes just to control this (even in mruby core). And that's an example from my lib (which inspired me to write this).

So, there is no way (other then naming hacks) to specify dependencies (usually we need it when using modules with shared logic).

The Idea

I was thinking of how to tell the builder to compile Ruby files in the specified order and came up with the following idea: let's use magic comments to declare dependencies:

# require: utils/logging

class Base
  include Utils::Logging
  ...
end

Looks like Sprockets 馃槒

Pros: easy to use/read, backward-compatible.

Cons: that differs from Ruby.

Other ideas

Another option I see is to specify the build order hints somewhere in config:

# Load specified files first, then everything else
conf.compile_first = %w(utils/**/*.rb zzz.rb)

Pros: no language changes.
Cons: harder to maintain (maybe, that's OK for mRuby libs, 'cause they're usually not too big).

@bovi
Copy link
Member

bovi commented Jul 19, 2017

I agree that this is an annoying problem. I actually fall several times over this. Yet I don't like the magic comment. The load specific config file on the other side I like.

@christopheraue
Copy link
Contributor

Well, you can already use

conf.rbfiles = Dir["#{conf.dir}/mrblib/utils/**/*.rb"] + Dir["#{conf.dir}/mrblib/zzz.rb"]

in the gemspec. As long as your files are still placed in the mrblib/ folder this should make sure that the files are loaded in the given order. I had a similar problem with my gem where I did this.

@matz
Copy link
Member

matz commented Jul 21, 2017

I prefer magic comment. I care about maintainability.

@palkan
Copy link
Contributor Author

palkan commented Jul 25, 2017

Ok. If we are going with magic comments, how they should work?

Current implementation ideologically works similar to MRI magic comments (at least, encoding, as I see here): we read source code line by line, ignore blank lines, parse magic "require" comments, scan until we find a line that do not match the magic comment (see test examples included in PR).

Not sure what to add/remove/improve.

@palkan palkan changed the title [EXPERIMENTAL] Specify Ruby files order through magic comments Specify Ruby files order through magic comments Aug 8, 2017
@palkan palkan force-pushed the feature/require-hints branch 2 times, most recently from e41ee66 to 936babb Compare August 8, 2017 09:05
@palkan
Copy link
Contributor Author

palkan commented Aug 8, 2017

Update:

  • Rebased/refactored on the top of the recent master changes (moving build utils to lib/)
  • Added libtest task (to test build utils)

RickCarlino added a commit to FarmBot-Labs/csvm that referenced this pull request May 10, 2018
@palkan
Copy link
Contributor Author

palkan commented Sep 17, 2018

馃啓

Hey everyone!

Are we still interested in this feature? Maybe it's time to re-visit it?

@palkan palkan requested a review from matz as a code owner May 13, 2021 07:07
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

Successfully merging this pull request may close these issues.

None yet

4 participants