fidothe / symlink-require

Illustrate a problem with `require_relative` and symlinked dirs in `$LOAD_PATH`

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Issue with require_relative and symlinked directories

Symlinked directories cause an issue with require_relative where libraries loaded from a symlinked path (e.g. in a typical Capistrano / Bundler deployment) can be double-loaded.

Consider the example here. target.rb uses require_relative to load dependent.rb. target.rb is the main target for require by the library's users.

When the example also requires dependent.rb the file is double-loaded and we see a warning about already-initialized constant.

The problem is that when require in use-symlink.rb (line 7) checks the set of loaded files it uses an absolute path containing the symlink (since that is what was added to $LOAD_PATH), but when require_relative in target.rb is called, it uses an absolute path containing target of the symlink, causing the double-load.

Is this discrepancy intended behaviour on Ruby's part?

Running the examples

$ ruby use-symlink.rb

will cause a double-load and display a warning

$ ruby use-realdir.rb

will not

About

Illustrate a problem with `require_relative` and symlinked dirs in `$LOAD_PATH`


Languages

Language:Ruby 100.0%