[ruby-core:90869] [Ruby trunk Bug#15500] Behavior of require method in 2.5 is different from 2.4 and 2.6

From: muraken@...
Date: 2019-01-03 07:13:19 UTC
List: ruby-core #90869
Issue #15500 has been updated by mrkn (Kenta Murata).


I think the behavior of 2.5 can be thought of a bug because the behavior for .so file isn't consistent with one for .rb file.
You can see the inconsistency in the results below.

```
$ ruby -rbigdecimal/util -ve 'p $LOADED_FEATURES.grep(/bigdecimal/)'
ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-linux]
["/usr/local/lib/ruby/gems/2.5.0/gems/bigdecimal-1.4.2/lib/bigdecimal.so", "/usr/local/lib/ruby/gems/2.5.0/gems/bigdecimal-1.4.2/lib/bigdecimal.rb", "/usr/local/lib/ruby/gems/2.5.0/gems/bigdecimal-1.4.2/lib/bigdecimal/util.so", "/usr/local/lib/ruby/gems/2.5.0/gems/bigdecimal-1.4.2/lib/bigdecimal/util.rb"]
```

In this case, `bigdecimal/util` is required, and the gem-installed version of `bigdecimal/util.rb` is loaded.
You can see the gem-installed version of `bigdecimal.so` is also loaded.

The behavior is changed when `bigdecimal` is required before `bigdecimal/util`.

```
$ ruby -rbigdecimal -rbigdecimal/util -ve 'p $LOADED_FEATURES.grep(/bigdecimal/)'
ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-linux]
/usr/local/lib/ruby/gems/2.5.0/gems/bigdecimal-1.4.2/lib/bigdecimal.rb:7: warning: method redefined; discarding old new
Traceback (most recent call last):
	5: from /usr/local/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	4: from /usr/local/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:54:in `require'
	3: from /usr/local/lib/ruby/gems/2.5.0/gems/bigdecimal-1.4.2/lib/bigdecimal/util.rb:9:in `<top (required)>'
	2: from /usr/local/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:34:in `require'
	1: from /usr/local/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:130:in `rescue in require'
/usr/local/lib/ruby/site_ruby/2.5.0/rubygems/core_ext/kernel_require.rb:130:in `require': /usr/local/lib/ruby/gems/2.5.0/gems/bigdecimal-1.4.2/lib/bigdecimal/util.so: undefined symbol: rmpd_util_str_to_d - /usr/local/lib/ruby/gems/2.5.0/gems/bigdecimal-1.4.2/lib/bigdecimal/util.so (LoadError)
```

In this case, LoadError is occurred by the symbol resolution failure of bigdecimal/util.so.  This file is newly introduced since bigdecimal-1.4.0.
Although `bigdecimal.so` in bigdecimal-1.4.2 has `rmpd_util_str_to_d`, it isn't loaded because the default-gem version of `bigdecimal.so` is loaded by `-rbigdecimal` option.

The difference between the default-gem and gem-installed versions is whether `bigdecimal.rb exists.
Although I've not investigated the implementation of Ruby 2.5's `require` method yet, I consider that Ruby 2.5 won't search in the gem-installed version if there is .so file but there isn't .rb file.

----------------------------------------
Bug #15500: Behavior of require method in 2.5 is different from 2.4 and 2.6
https://bugs.ruby-lang.org/issues/15500#change-76056

* Author: mrkn (Kenta Murata)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
* ruby -v: ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin18]
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
On Ruby 2.5, `require 'bigdecimal'` does not load `bigdecimal.so` in gem-installed version of bigdecimal without specifying the version by `gem` method.
You can examine this phenomenon by the following code.

```ruby
require 'bigdecimal'
p $LOADED_FEATURES.grep(/bigdecimal/).first
```

After installing bigdecimal-1.4.2 by `gem install bigdecimal` command, this code (saved as `t.rb`) show the following results:

On Ruby 2.4.5

```
$ ruby-2.4.5 -v t.rb
ruby 2.4.5p335 (2018-10-18 revision 65137) [x86_64-darwin18]
"/Users/mrkn/.rbenv/versions/2.4.5/lib/ruby/gems/2.4.0/gems/bigdecimal-1.4.2/lib/bigdecimal.bundle"
```

On Ruby 2.5.3:

```
$ ruby-2.5.3 -v t.rb
ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin18]
"/Users/mrkn/.rbenv/versions/2.5.3/lib/ruby/2.5.0/x86_64-darwin18/bigdecimal.bundle"
```

On Ruby 2.6.0:

```
$ ruby-2.6.0 -v t.rb
ruby 2.6.0p0 (2018-12-25 revision 66547) [x86_64-darwin18]
"/Users/mrkn/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/gems/bigdecimal-1.4.2/lib/bigdecimal.bundle"
```

On Ruby 2.5.3, with `gem 'bigdecimal', '1.4.2'`, the correct file can be loaded.

```
$ ( echo "gem 'bigdecimal', '1.4.2'"; cat t.rb ) | ruby-2.5.3 -v -
ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin18]
"/Users/mrkn/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/bigdecimal-1.4.2/lib/bigdecimal.bundle"
```



-- 
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>

In This Thread

Prev Next