rutie icon indicating copy to clipboard operation
rutie copied to clipboard

Can't find `RUBY_DESCRIPTION` when using a native gem from rust

Open pwoolcoc opened this issue 4 years ago • 1 comments
trafficstars

When trying to use ruby inside a rust application, I'm having some issues with requiring gems with C extensions, in this case Nokogiri. When I VM::require("nokogiri"), I get an error from the VM: #<NameError: uninitialized constant RUBY_DESCRIPTION>.

To reproduce:

# Gemfile

# ...

gem "nokogiri"
// src/main.rs

use rutie::{VM, Object, NilClass};

fn main() {
    VM::init();
    VM::init_loadpath();
    VM::require("rubygems"); 

    if let Err(e) = VM::protect(|| {
        VM::require("nokogiri");
        NilClass::new().into()
    }) {
        let output = VM::error_info().unwrap();
        eprintln!("{}", output);
    }
}

I run the program with:

$ RUBYLIB=$(bundle exec ruby -e 'puts $LOAD_PATH' | tr '\n' ':') DYLD_LIBRARY_PATH=$(bundle exec ruby -e "puts RbConfig::CONFIG['libdir']") cargo run

pwoolcoc avatar Apr 12 '21 14:04 pwoolcoc

@pwoolcoc You might need to initialize it first:

https://github.com/ruby/ruby/blob/d92f09a5eea009fa28cd046e9d0eb698e3d94c5c/version.c#L100

which provides

void
Init_ruby_description(void)
{
    VALUE description;

    if (MJIT_OPTS_ON) {
        description = MKSTR(description_with_jit);
    }
    else {
        description = MKSTR(description);
    }

    /*
     * The full ruby version string, like <tt>ruby -v</tt> prints
     */
    rb_define_global_const("RUBY_DESCRIPTION", /* MKSTR(description) */ description);
}

On many of these kinds of errors you can simply search the Ruby source code.

danielpclark avatar May 22 '21 15:05 danielpclark