ruby / readline

Readline Loader
Other
2 stars 5 forks source link

Fix readline-ext gem loading in non Unix-like environments #6

Closed hogelog closed 9 months ago

hogelog commented 9 months ago

I would like to suggest a fix because readline-ext loading is not working properly in non-Unix-like environments.

Why

The call require "foo.so" first calls the require overridden by RubyGems, which in turn calls ruby's native require. When ruby's native require loads "foo.so", it searches for the appropriate file for each environment, such as foo.so, foo.bundle, or foo.dll. However, RubyGems's require tries to load "foo.so" and does not look for files like foo.bundle, foo.dll.

So I have created this PR. With this change, the require "readline" call will load the native extension of the readline-ext gem.

Behavior of existing implementation.

Environment with .so extension (e.g. Linux)

Existing implementations and this PR implementation work fine.

The call "readline.so" and the actual file name match.

Environment with bundler

If you run via Bundler in a Gemfile like the following, ruby add the path of the gem to LOAD_PATH, so even ruby native require can find the readline.{bundle,dll,...} and load it successfully.

source "https://rubygems.org"

gem "readline-ext"

Environment where the extention extension is not .so (e.g. macOS) with Ruby <= 3.2

Until Ruby 3.2, ext/readline was a standard library, so when you do `require "readline.so"', RubyGems's require cannot find readline.so and native require can find the readline extension (e.g. readline.bundle) of ext/readline. On the other hand, the readline extension of readline-ext is not used.

Environment where the extention extension is not .so (e.g. macOS) with Ruby 3.3

In Ruby 3.3, since ext/readline is retired, it will not find the readline native extension provided by the readline-ext gem and will fall back to loading relines.

hogelog commented 9 months ago

I think this PR would work as a workaround.

However, I thought this behavior of RubyGems' require might be a bug in RubyGems, so I suggested RubyGems to fix it as well. https://github.com/rubygems/rubygems/pull/7241

If the fixes https://github.com/rubygems/rubygems/pull/7241 are merged, the problem may not be apparent in Ruby 3.3 without applying this PR workaround.

hsbt commented 9 months ago

Thanks. I have not enough time to review to https://github.com/rubygems/rubygems/pull/7241 for RubyGems 3.5.

I will merge this for Ruby 3.3.

hogelog commented 9 months ago

Thanks for the review and merge!