Feature #18401
closedRework `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:
But require_relative "foo" does something different:
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:
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
Updated by nobu (Nobuyoshi Nakada) about 1 month ago
- Status changed from Open to Feedback
vo.x (Vit Ondruch) wrote:
My proposal is to change the
require_relativein following way:
Do you mean require_relative should load the file in the current working directory?
I don't think it is reasonable behavior.
require_relative binds the caller's location more tightly, and should not be affected by the runtime environment.
In essence, non of the
require_relative,require,__FILE__or__dir__would need to use real path. The scenario bellow would provide expected output:
Isn't this the same as the current behavior?