Project

General

Profile

Feature #15230

RubyVM.resolve_feature_path

Added by mame (Yusuke Endoh) 10 months ago. Updated 3 months ago.

Status:
Closed
Priority:
Normal
Target version:
-
[ruby-core:89431]

Description

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/etc.so"]

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));
 }

+VALUE
+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);
 #endif

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

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

 void

Related issues

Related to Ruby master - Feature #15903: Move RubyVM.resolve_feature_path to Kernel.resolve_feature_pathClosedActions

Associated revisions

Revision 0cd28199
Added by mame (Yusuke Endoh) 9 months ago

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

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66237 b2dd03c8-39d4-4d8f-98ff-823fe69b080e

Revision 66237
Added by mame (Yusuke Endoh) 9 months ago

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

Revision 66237
Added by mame (Yusuke Endoh) 9 months ago

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

History

Updated by Eregon (Benoit Daloze) 10 months 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.

Updated by mame (Yusuke Endoh) 10 months 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.

Updated by Eregon (Benoit Daloze) 10 months 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.

Updated by duerst (Martin Dürst) 10 months 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) 9 months ago

  • Status changed from Assigned to Closed

Applied in changeset trunk|r66237.


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

Updated by znz (Kazuhiro NISHIYAMA) 9 months 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]

Updated by Eregon (Benoit Daloze) 3 months ago

mame (Yusuke Endoh) should the behavior that znz (Kazuhiro NISHIYAMA) reports be fixed?
resolve_feature_path sounds like it should always resolve a feature, no matter if the feature is loaded or not.

#8

Updated by Eregon (Benoit Daloze) 3 months ago

  • Related to Feature #15903: Move RubyVM.resolve_feature_path to Kernel.resolve_feature_path added

Updated by mame (Yusuke Endoh) 3 months ago

Eregon (Benoit Daloze) wrote:

mame (Yusuke Endoh) should the behavior that znz (Kazuhiro NISHIYAMA) reports be fixed?

I hear nobu (Nobuyoshi Nakada) has already fixed the issue. (Thanks!)

Also available in: Atom PDF