Feature #15230


Added by mame (Yusuke Endoh) about 2 months ago. Updated 7 days ago.

Target version:


I'd like a feature to know what will be loaded by require(feature) without actual loading.

$ ./local/bin/ruby -e 'p RubyVM.resolve_feature_path("set")'
[:r, "/home/mame/work/ruby/local/lib/ruby/2.6.0/set.rb"]

$ ./local/bin/ruby -e 'p RubyVM.resolve_feature_path("etc")'
[:s, "/home/mame/work/ruby/local/lib/ruby/2.6.0/x86_64-linux/"]

This feature is useful for a static analysis tool of Ruby programs. It might also be useful to check $LOAD_PATH configuration.

I don't think that RubyVM is the best place to have this method, but a good place to experiment the new feature. Kernel#resolve_feature_path looks too aggressive.

diff --git a/load.c b/load.c
index ddde2baf3b..dd609105ee 100644
--- a/load.c
+++ b/load.c
@@ -942,6 +942,26 @@ load_ext(VALUE path)
     return (VALUE)dln_load(RSTRING_PTR(path));

+rb_resolve_feature_path(VALUE klass, VALUE fname)
+    VALUE path;
+    int found;
+    char s[2];
+    fname = rb_get_path_check(fname, 0);
+    path = rb_str_encode_ospath(fname);
+    found = search_required(path, &path, 0);
+    if (!found) {
+        load_failed(fname);
+    }
+    s[0] = found;
+    s[1] = 0;
+    return rb_ary_new_from_args(2, ID2SYM(rb_intern2(s, 1)), path);
  * returns
  *  0: if already loaded (false)
diff --git a/vm.c b/vm.c
index fababaa2ec..2a72d16f47 100644
--- a/vm.c
+++ b/vm.c
@@ -2834,6 +2834,8 @@ static VALUE usage_analysis_operand_stop(VALUE self);
 static VALUE usage_analysis_register_stop(VALUE self);

+VALUE rb_resolve_feature_path(VALUE klass, VALUE fname);
@@ -3140,6 +3142,8 @@ Init_VM(void)

     /* vm_backtrace.c */
+    rb_define_singleton_method(rb_cRubyVM, "resolve_feature_path", rb_resolve_feature_path, 1);


Associated revisions

Revision 0cd28199
Added by mame (Yusuke Endoh) 7 days ago

load.c (RubyVM.resolve_feature_path): New method. [Feature #15230]

git-svn-id: svn+ssh:// b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 66237
Added by mame (Yusuke Endoh) 7 days ago

load.c (RubyVM.resolve_feature_path): New method. [Feature #15230]


#1 [ruby-core:89635] Updated by Eregon (Benoit Daloze) about 1 month ago

What's the leading one letter Symbol in the return value?
That seems fairly cryptic.

Do you need it? I would expect such a method to return a path, i.e., a String.

#2 [ruby-core:89639] Updated by mame (Yusuke Endoh) about 1 month ago

:r means .rb and :s means .so, I guess :-)

It is not absolutely necessary. But it would be somewhat useful for a static analysis tool of Ruby programs because such a tool typically needs to skip .so files.

#3 [ruby-core:89640] Updated by Eregon (Benoit Daloze) about 1 month ago

If it's reliable enough (I think it is) to detect native extensions by the file extension (.so, .dylib)
then I think that should be preferred as it would simplify the method's result.

#4 [ruby-core:89652] Updated by duerst (Martin Dürst) about 1 month ago

mame (Yusuke Endoh) wrote:

:r means .rb and :s means .so, I guess :-)

If this information is kept, please make it easier to understand. :rb/:so or :'.rb'/:'.so' at a minimum.

#5 Updated by mame (Yusuke Endoh) 7 days ago

  • Status changed from Assigned to Closed

Applied in changeset trunk|r66237.

load.c (RubyVM.resolve_feature_path): New method. [Feature #15230]

#6 [ruby-core:90337] Updated by znz (Kazuhiro NISHIYAMA) 7 days ago

It returns false as path when feature is already loaded.
Is it intentional?

% rbenv exec irb --simple-prompt -r irb/completion
>> RubyVM.resolve_feature_path('set')
=> [:rb, false]

Also available in: Atom PDF