https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112016-03-09T01:40:39ZRuby Issue Tracking SystemRuby master - Bug #12159: Thread::Backtrace::Location#path returns absolute path for files loaded by require_relativehttps://bugs.ruby-lang.org/issues/12159?journal_id=573722016-03-09T01:40:39Zusa (Usaku NAKAMURA)usa@garbagecollect.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>ko1 (Koichi Sasada)</i></li></ul><p>I guess that it's intentional.<br>
<code>absolute_path</code> guarantees to contain the absolute path, but <code>path</code> does not guarantee so.<br>
It may contain the absolute path or may not.</p>
<p>note: not only in <code>require_relative</code> but also in <code>require</code>.</p> Ruby master - Bug #12159: Thread::Backtrace::Location#path returns absolute path for files loaded by require_relativehttps://bugs.ruby-lang.org/issues/12159?journal_id=573732016-03-09T01:43:56Ztagomoris (Satoshi Tagomori)tagomoris@gmail.com
<ul></ul><p>Usaku NAKAMURA wrote:</p>
<blockquote>
<p>I guess that it's intentional.<br>
<code>absolute_path</code> guarantees to contain the absolute path, but <code>path</code> does not guarantee so.<br>
It may contain the absolute path or may not.</p>
<p>note: not only in <code>require_relative</code> but also in <code>require</code>.</p>
</blockquote>
<p>If so, I think it's better to write "Returns the file name or absolute path of this frame" in document.<br>
<a href="http://docs.ruby-lang.org/en/2.3.0/Thread/Backtrace/Location.html#method-i-path" class="external">http://docs.ruby-lang.org/en/2.3.0/Thread/Backtrace/Location.html#method-i-path</a></p> Ruby master - Bug #12159: Thread::Backtrace::Location#path returns absolute path for files loaded by require_relativehttps://bugs.ruby-lang.org/issues/12159?journal_id=574972016-03-16T10:17:36Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>Agreed - should be more clearly written to reflect the current behaviour.</p> Ruby master - Bug #12159: Thread::Backtrace::Location#path returns absolute path for files loaded by require_relativehttps://bugs.ruby-lang.org/issues/12159?journal_id=580022016-04-11T04:57:36Zko1 (Koichi Sasada)
<ul></ul><p>Actually, I'm not sure the policy of path representation.<br>
For example, we can normalize every path entities with absolute path.</p>
<p>I'll ask Matz at next dev meeting (next Wed 13:00-, JST).</p> Ruby master - Bug #12159: Thread::Backtrace::Location#path returns absolute path for files loaded by require_relativehttps://bugs.ruby-lang.org/issues/12159?journal_id=613282016-11-05T16:14:29Zko1 (Koichi Sasada)
<ul></ul><p>Sorry for late response.</p>
<p>I'll ask matz again.<br>
I and Nobu talked about this topic and we agree with:</p>
<p>(1) to be obsolete <code>absolute_path</code> method and alias with <code>path</code> method.<br>
(2) <code>path</code> method returns absolute path, even if main script (which is specified for ruby command).</p>
<p>Disadvantage is backtrace will be long for main script.</p> Ruby master - Bug #12159: Thread::Backtrace::Location#path returns absolute path for files loaded by require_relativehttps://bugs.ruby-lang.org/issues/12159?journal_id=613292016-11-05T16:16:54Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>Agreed.</p>
<p>Matz.</p> Ruby master - Bug #12159: Thread::Backtrace::Location#path returns absolute path for files loaded by require_relativehttps://bugs.ruby-lang.org/issues/12159?journal_id=651682017-05-30T06:10:14Zko1 (Koichi Sasada)
<ul></ul><p>After consideration, I changed my proposal.</p>
<ul>
<li>Rename <code>#absolute_path</code> to <code>#real_path</code> (or <code>#realpath</code>) and make <code>#aboluste_path</code> as alias of <code>#real_path</code>.</li>
<li>change <code>absolute_path</code> (<code>real_path</code>) on <code>eval</code> with given file name.</li>
</ul>
<a name="summary-of-current-behavior"></a>
<h1 >summary of current behavior<a href="#summary-of-current-behavior" class="wiki-anchor">¶</a></h1>
<p>On MRI, "path" is used several ways.</p>
<ul>
<li><code>__FILE__</code></li>
<li>
<code>caller(_locations)</code>, backtrace</li>
<li>
<code>requre_relative</code> (base directory)</li>
<li><code>$0</code></li>
</ul>
<p>And ISeq has <code>path</code> and <code>absolute_path</code>. I use this terminology.</p>
<ul>
<li>path: given path.</li>
<li>absolute_path: realpath(path) if path is exist. If not, it is nil.</li>
</ul>
<p>Above usages are implemented with <code>path</code> and <code>absolute_path</code>.</p>
<ul>
<li>
<code>__FILE__</code> # path</li>
<li>
<code>caller(_locations)</code>, backtrace # path</li>
<li>
<code>requre_relative</code> (base directory) # absolute_path</li>
<li>
<code>$0</code> # path</li>
</ul>
<p>Most of case, path and absolute_path is same. However, the following case they are not same.</p>
<ul>
<li>
<code>path</code> and <code>realpath(path)</code> is different because of symlink (<code>absolute_path</code> is realpath).</li>
<li>script name is given by command parameter (<code>ruby x.rb</code>) (<code>absolute_path</code> will be <code>/path/to/x.rb</code>).</li>
<li>
<code>eval()</code> without file name (<code>path</code> will be "(eval)" and <code>absolute_path</code> will be nil).</li>
</ul>
<p>Note that <code>eval(script, binding, "x.rb")</code> makes <code>path</code> and <code>absolute_path</code> return <code>"x.rb"</code> even if given file name is not realpath.</p>
<pre><code>eval('caller_locations(0, 1).each{|e| p [e.path, e.absolute_path]}')
eval('caller_locations(0, 1).each{|e| p [e.path, e.absolute_path]}', binding, 'x.rb')
["(eval)", nil]
["x.rb", "x.rb"]
</code></pre>
<a name="proposal"></a>
<h1 >proposal<a href="#proposal" class="wiki-anchor">¶</a></h1>
<p>Checking current behavior, <code>#absolute_path</code> is used as <code>realpath</code> (check the existing and resolve symlink). So I want to add <code>#realpath</code> or <code>#real_path</code>. I'm not sure which is better because there is <code>File#realpath</code> and <code>absolute_path</code> include <code>_</code>.</p>
<p>Also I want to check realpass for file name given at <code>eval()</code>. If file name is not existing, <code>#realpath</code> should be <code>nil</code>. In this case, <code>require_relative</code> should fail because MRI can't infer the base directory.</p>
<p>How about it?</p> Ruby master - Bug #12159: Thread::Backtrace::Location#path returns absolute path for files loaded by require_relativehttps://bugs.ruby-lang.org/issues/12159?journal_id=651742017-05-30T09:58:41ZEregon (Benoit Daloze)
<ul></ul><p>ko1 (Koichi Sasada) wrote:</p>
<blockquote>
<p>Disadvantage is backtrace will be long for main script.</p>
</blockquote>
<p>This only applies to the main script, and none of the other files so I think it's worth the gain in consistency.<br>
Backtraces in anything but small script include more than one file anyway.</p>
<p>#realpath sounds OK, although it seems to mix two different things at once.<br>
Is #realpath essentially (in terms of current methods):</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">realpath</span>
<span class="n">absolute_path</span> <span class="o">&&</span> <span class="no">File</span><span class="p">.</span><span class="nf">realpath</span><span class="p">(</span><span class="n">absolute_path</span><span class="p">)</span> <span class="k">rescue</span> <span class="kp">nil</span>
<span class="k">end</span>
</code></pre>
<p>If so I think it's not needed and the simpler proposal is better.</p> Ruby master - Bug #12159: Thread::Backtrace::Location#path returns absolute path for files loaded by require_relativehttps://bugs.ruby-lang.org/issues/12159?journal_id=843732020-02-25T04:30:55Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Closed</i></li></ul><p>I talked about this issue with nobu and ko1.</p>
<blockquote>
<p>Is it intentional? or a bug?</p>
</blockquote>
<p>As <a class="user active user-mention" href="https://bugs.ruby-lang.org/users/9">@usa (Usaku NAKAMURA)</a> said, it is intentional according to nobu and ko1. Please use <code>#absolute_path</code>.</p>
<blockquote>
<p>If so, I think it's better to write "Returns the file name or absolute path of this frame" in document.</p>
</blockquote>
<p>It may return relative path, too. IMO, "file name" includes relative and absolute paths, so I don't think we need to change anything.</p>
<p>ko1 said he withdraws his proposal. So closing this ticket.</p>