https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112011-02-17T03:24:48ZRuby Issue Tracking SystemRuby master - Bug #4403: require command loads file twice when required symlink or file under directory which is symlink (symbolic link)https://redmine.ruby-lang.org/issues/4403?journal_id=158542011-02-17T03:24:48Zwyhaines (Kirk Haines)wyhaines@gmail.com
<ul></ul><p>=begin<br>
On Wed, Feb 16, 2011 at 7:48 AM, Robert Pankowecki<br>
<a href="mailto:redmine@ruby-lang.org" class="email">redmine@ruby-lang.org</a> wrote:</p>
<blockquote>
<p>Bug <a class="issue tracker-1 status-6 priority-4 priority-default closed" title="Bug: require command loads file twice when required symlink or file under directory which is symlink (... (Rejected)" href="https://redmine.ruby-lang.org/issues/4403">#4403</a>: require command loads file twice when required symlink or file under directory which is symlink (symbolic link)<br>
<a href="http://redmine.ruby-lang.org/issues/show/4403" class="external">http://redmine.ruby-lang.org/issues/show/4403</a></p>
<p>Author: Robert Pankowecki<br>
Status: Open, Priority: Normal<br>
ruby -v: ruby 1.9.2p136 (2010-12-25 revision 30365) [i686-linux]</p>
<p>Consider this file structure:</p>
<p>~/test/symbolic > tree<br>
.<br>
|-- a<br>
| <code>-- a.rb |-- b -> a |-- c.rb </code>-- d.rb -> c.rb</p>
<p>Current behavior:</p>
<p>ruby-1.9.2-p136 :001 > require './c'<br>
=> true<br>
ruby-1.9.2-p136 :002 > require './d'<br>
./d.rb:1: warning: already initialized constant CON2<br>
=> true<br>
ruby-1.9.2-p136 :003 > require './a/a'<br>
=> true<br>
ruby-1.9.2-p136 :004 > require './b/a'<br>
./b/a.rb:1: warning: already initialized constant CON<br>
=> true</p>
<p>Expected behavior:</p>
<p>ruby-1.9.2-p136 :001 > require './c'<br>
=> true<br>
ruby-1.9.2-p136 :002 > require './d'<br>
=> false<br>
ruby-1.9.2-p136 :003 > require './a/a'<br>
=> true<br>
ruby-1.9.2-p136 :004 > require './b/a'<br>
=> false</p>
<p>This bug can be reproduced at Ruby 1.8.7, too.</p>
</blockquote>
<p>The behavior that you are seeing is what is expected. Ruby is using<br>
the path of the required file to differentiate between files which<br>
have been required before, and those which have not been.</p>
<p>This has been well documented in the code and elsewhere for a long time.</p>
<p>Take a look in eval.c, searching for "rb_f_require", in a 1.8.x series<br>
source code, and in the 1.9.x code, look in load.c. The<br>
comment//documentation is identical except for the last sentence,<br>
which details a single important difference:</p>
<p>Ruby 1.8.6:</p>
<p>[This first chunk is identical]</p>
<ul>
<li>Ruby tries to load the library named <em>string</em>, returning</li>
<li>+true+ if successful. If the filename does not resolve to</li>
<li>an absolute path, it will be searched for in the directories listed</li>
<li>in <code>$:</code>. If the file has the extension ``.rb'', it is</li>
<li>loaded as a source file; if the extension is <code>.so'', </code>.o'', or</li>
<li>``.dll'', or whatever the default shared library extension is on</li>
<li>the current platform, Ruby loads the shared library as a Ruby</li>
<li>extension. Otherwise, Ruby tries adding <code>.rb'', </code>.so'', and so on</li>
<li>to the name. The name of the loaded feature is added to the array in</li>
<li>
<code>$"</code>. A feature will not be loaded if it's name already</li>
</ul>
<p>[This last sentence is what is different]</p>
<ul>
<li>appears in <code>$"</code>. However, the file name is not converted</li>
<li>to an absolute path, so that ``<code>require 'a';require</code>
</li>
<li>'./a''' will load <code>a.rb</code> twice.</li>
</ul>
<p>Ruby 1.9.3 trunk, as of today:</p>
<p>[Just the part that is different.]</p>
<ul>
<li>.... The file name is converted to an absolute</li>
<li>path, so ``<code>require 'a'; require './a'</code>'' will not load</li>
<li>
<code>a.rb</code> twice.</li>
</ul>
<p>In both cases, though, a link, which creates two different <em>absolute</em><br>
paths to the the same file, will allow it to get required twice. This<br>
is expected.</p>
<p>Kirk Haines</p>
<p>=end</p> Ruby master - Bug #4403: require command loads file twice when required symlink or file under directory which is symlink (symbolic link)https://redmine.ruby-lang.org/issues/4403?journal_id=184432011-06-26T18:39:24Znahi (Hiroshi Nakamura)nakahiro@gmail.com
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>nahi (Hiroshi Nakamura)</i></li><li><strong>Target version</strong> set to <i>1.9.3</i></li></ul> Ruby master - Bug #4403: require command loads file twice when required symlink or file under directory which is symlink (symbolic link)https://redmine.ruby-lang.org/issues/4403?journal_id=186042011-06-28T12:33:36Znahi (Hiroshi Nakamura)nakahiro@gmail.com
<ul><li><strong>Category</strong> set to <i>core</i></li><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Rejected</i></li></ul><p>As Kirk said. 1.9 expands a path for required feature for double load detection but it does not follow symlink. I can understand that some user think it as a bug but following symlink should be handled at OS layer, not at Ruby language layer I think. I don't expect 'require "foo"' adds '/path/to/bar.rb' to $LOADED_FEATURES.</p>
<p>I close this ticket as 'Not a Bug'. Please re-file a new issue for require+symlink as 'Feature' if you think it good to be added.</p>