Issue #15230 has been reported by mame (Yusuke Endoh).

----------------------------------------
Feature #15230: RubyVM.resolve_feature_path
https://bugs.ruby-lang.org/issues/15230

* Author: mame (Yusuke Endoh)
* Status: Assigned
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* 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/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
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
```



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>