From: "vo.x (Vit Ondruch)" Date: 2021-12-09T17:41:24+00:00 Subject: [ruby-core:106606] [Ruby master Feature#18401] Rework `require_relative` to add the "current path" on `$LOAD_PATH` Issue #18401 has been reported by vo.x (Vit Ondruch). ---------------------------------------- Feature #18401: Rework `require_relative` to add the "current path" on `$LOAD_PATH` https://bugs.ruby-lang.org/issues/18401 * Author: vo.x (Vit Ondruch) * Status: Open * Priority: Normal ---------------------------------------- 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 -- https://bugs.ruby-lang.org/ Unsubscribe: