Feature #18401
openRework `require_relative` to add the "current path" on `$LOAD_PATH`
Description
I think that since inception of require_relative
, the implementation is wrong and is going against the spirit of require
functionality. Let me explain.
If there is require "foo"
, it does something like:
r = $LOAD_PATH.select {|lp| File.exist? File.join(lp, "foo")}.first
load(r)
But require_relative "foo"
does something different:
r = File.join(File.realpath(__dir__), "foo")
load(r)
Please note that this is problematic if mixture of require
and require_relative
(not mentioning __FILE__
and __dir__
are used. The major difference is actually the File.realpath
here, because it expands symlinks and that is difference to require
and may allow double loading.
My proposal is to change the require_relative
in following way:
$LOAD_PATH.unshift __dir__
require "foo"
In essence, non of the require_relative
, require
, __FILE__
or __dir__
would need to use real path. The scenario bellow would provide expected output:
$ mkdir a
$ mkdir b
$ echo 'puts __dir__' > a/test.rb
$ cd b
$ ln -s ../a/test.rb test.rb
$ ruby test.rb
/home/vondruch/ruby/a
This would also resolve issues such as #16978, #10222 or #17885
No data to display