Issue #10222 has been updated by Shyouhei Urabe.

Subject changed from require_relative and require should be compatible with each other to require_relative and require should be compatible with each other when symlinks are used

We looked at this issue in developer meeting today.

The ultimate reason why require and require_relative behaves differently is that while require_relative infers its argument's realpath every time, require doesn't.

This _was_ by design; because require is called many times, we wanted to completely avoid disk access for 2nd and later calls to require with identical arguments.

But I believe the reported behaviour is a bug to be fixed.  In order to do so a meeting attendee suggested to push both symlink-resolved and unresolved paths at once to $LOADED_FEATURES on the first call.


----------------------------------------
Bug #10222: require_relative and require should be compatible with each other when symlinks are used
https://bugs.ruby-lang.org/issues/10222#change-60844

* Author: Rodrigo Rosenfeld Rosas
* Status: Open
* Priority: Normal
* Assignee: 
* ruby -v: 2.3.1, 2.1.2p95
* Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN
----------------------------------------
Not sure if this should be considered a bug or a feature request since I don't know whether the current behavior is intended or not.

Recently I got a report for my gem rails-web-console related to require_relative causing trouble with symlinked dirs:

https://github.com/rosenfeld/active_record_migrations/issues/6

Dmitry was able to replicate the issue using vanilla Ruby:

~~~
mkdir a
ln -s a b
echo "require_relative 'b'" > a/a.rb
echo "p 'b loaded'" > a/b.rb
echo "$: << File.expand_path('../b', __FILE__); require 'a'; require 'b'" > c.rb
ruby c.rb
~~~

Notice how "b loaded" is printed twice but if you replace require_relative with require it's just loaded once.

Shouldn't Ruby always expand the loaded files before appending them to the $LOADED_FEATURES and avoid this kind of error? I don't think require_relative should behave differently than a regular require in such cases.

Any thoughts?



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