https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112014-12-04T19:22:56ZRuby Issue Tracking SystemRuby master - Feature #10561: Improve function of Thread::Backtrace::Location #path and #absolute_pathhttps://bugs.ruby-lang.org/issues/10561?journal_id=503042014-12-04T19:22:56Zyorickpeterse (Yorick Peterse)yorickpeterse@gmail.com
<ul></ul><p>I'd like to have some extra clarification on this matter as well. Right now in Rubinius we return <em>just</em> the filename when calling Thread::Backtrace::Location#path as the documentation suggested this was the expected behaviour:</p>
<blockquote>
<p>Returns the file name of this frame.</p>
<p>For example, using caller_locations.rb from Thread::Backtrace::Location</p>
<p>loc = c(0..1).first<br>
loc.path #=> caller_locations.rb</p>
</blockquote> Ruby master - Feature #10561: Improve function of Thread::Backtrace::Location #path and #absolute_pathhttps://bugs.ruby-lang.org/issues/10561?journal_id=503062014-12-04T22:49:34Zsam.saffron (Sam Saffron)sam.saffron@gmail.com
<ul></ul><p>I think the name #path should always refer to a #path of sorts using it as #filename is kind of odd. Which is a big reason I find the duality of #path / #absolute_path confusing.</p> Ruby master - Feature #10561: Improve function of Thread::Backtrace::Location #path and #absolute_pathhttps://bugs.ruby-lang.org/issues/10561?journal_id=513732015-02-03T22:27:26Zyorickpeterse (Yorick Peterse)yorickpeterse@gmail.com
<ul></ul><p>Nobu: I talked about this with Koichi today after noticing you added tests for<br>
path/absolute_path after my talk at FOSDEM. Since these tests assert that path<br>
returns the full path I'd like to know which one is correct.</p>
<p>Is it this:</p>
<pre><code>caller_locations[0].path # => "example.rb"
</code></pre>
<p>Or is this the correct way:</p>
<pre><code>caller_locations[0].path # => "/home/some-user/example.rb"
</code></pre>
<p>If the last example is correct, is it perhaps an idea to add "basename" to<br>
Thread::Location::Backtrace, which would return <em>just</em> the filename, and make<br>
path an alias to absolute_path? So:</p>
<pre><code>caller_locations[0].path # => "/home/some-user/example.rb"
caller_locations[0].absolute_path # => "/home/some-user/example.rb"
caller_locations[0].basename # => "example.rb"
</code></pre> Ruby master - Feature #10561: Improve function of Thread::Backtrace::Location #path and #absolute_pathhttps://bugs.ruby-lang.org/issues/10561?journal_id=513832015-02-04T05:42:50Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>The result of <code>#path</code> equals to <code>__FILE__</code> in the script file.<br>
It's expanded in <code>require</code>d libraries, but not in main scripts.</p> Ruby master - Feature #10561: Improve function of Thread::Backtrace::Location #path and #absolute_pathhttps://bugs.ruby-lang.org/issues/10561?journal_id=513942015-02-04T09:33:49Zyorickpeterse (Yorick Peterse)yorickpeterse@gmail.com
<ul></ul><p>Nobu: Thanks! I'll update the Rubinius implementation of this to match that behaviour.</p> Ruby master - Feature #10561: Improve function of Thread::Backtrace::Location #path and #absolute_pathhttps://bugs.ruby-lang.org/issues/10561?journal_id=513952015-02-04T09:37:19Zyorickpeterse (Yorick Peterse)yorickpeterse@gmail.com
<ul></ul><p>Also, I'll submit a patch to fix the documentation of <code>path</code> so that it states it's an alias of <code>absolute_path</code>.</p> Ruby master - Feature #10561: Improve function of Thread::Backtrace::Location #path and #absolute_pathhttps://bugs.ruby-lang.org/issues/10561?journal_id=514082015-02-05T10:19:22ZEregon (Benoit Daloze)
<ul></ul><p>Nobuyoshi Nakada wrote:</p>
<blockquote>
<p>The result of <code>#path</code> equals to <code>__FILE__</code> in the script file.<br>
It's expanded in <code>require</code>d libraries, but not in main scripts.</p>
</blockquote>
<p>There is only one main script (the file passed to the ruby executable) in a given execution, right?</p>
<p>Yorick: so #path is #absolute_path except for the main script, so it is similar to the formatting of usual exceptions backtraces:</p>
<pre><code>$ echo "raise" > tmp.rb
$ ruby tmp.rb
tmp.rb:1:in `<main>': unhandled exception
</code></pre>
<p>Here tmp.rb is main script.</p>
<pre><code>$ echo "require_relative 'tmp'" > tmp2.rb
$ ruby tmp2.rb
/home/eregon/code/tmp.rb:1:in `<top (required)>': unhandled exception
from tmp2.rb:1:in `require_relative'
from tmp2.rb:1:in `<main>'
</code></pre>
<p>Here tmp2.rb is the main script but tmp.rb becomes a "required library" so full path is shown.</p> Ruby master - Feature #10561: Improve function of Thread::Backtrace::Location #path and #absolute_pathhttps://bugs.ruby-lang.org/issues/10561?journal_id=514092015-02-05T10:23:58Zyorickpeterse (Yorick Peterse)yorickpeterse@gmail.com
<ul></ul><p>Benoit: correct, I just found this out by messing around more with this. This<br>
behaviour is extremely confusing, especially since the tests both just compare<br>
if <code>path</code> and <code>absolute_path</code> equal <code>__FILE__</code>.</p>
<p>Perhaps the Tempfile code in the tests (<a href="https://github.com/ruby/ruby/blob/69be3620302aab57c43014aca08629c9d7b7ce44/test/ruby/test_backtrace.rb#L170" class="external">https://github.com/ruby/ruby/blob/69be3620302aab57c43014aca08629c9d7b7ce44/test/ruby/test_backtrace.rb#L170</a>)<br>
checks for this, but it too is very confusing to understand. In case of Rubinius<br>
I'll see if we even have a way of detecting if something is called from the main<br>
script, and <em>again</em> adjust our specs and implementation.</p> Ruby master - Feature #10561: Improve function of Thread::Backtrace::Location #path and #absolute_pathhttps://bugs.ruby-lang.org/issues/10561?journal_id=516012015-02-23T09:48:12Zko1 (Koichi Sasada)
<ul></ul><p>[MAYBE IT SHOULD BE DIFFERENT TICKET]</p>
<p>How about to unify #path and #absolute_path?<br>
Returning absolute path on both methods.</p>
<p>When I made this API, there are three types path, (1) path of main script (partial path) and (2) paths of required files which is absolute path as described in this ticket and (3) string passed by "eval" methods (or "-e" command line option).</p>
<p>I think we don't need to separate (1) and (2). (1) can be absolute path.</p>
<p>What do you think?</p> Ruby master - Feature #10561: Improve function of Thread::Backtrace::Location #path and #absolute_pathhttps://bugs.ruby-lang.org/issues/10561?journal_id=516022015-02-23T09:49:42Zko1 (Koichi Sasada)
<ul></ul><blockquote>
<p>I think we don't need to separate (1) and (2). (1) can be absolute path.</p>
</blockquote>
<p>I guess the reason is that Matz thought backtrace should be simple for main script.</p> Ruby master - Feature #10561: Improve function of Thread::Backtrace::Location #path and #absolute_pathhttps://bugs.ruby-lang.org/issues/10561?journal_id=516042015-02-23T09:59:38Zcheba (Alexander Mankuta)alex@pointlessone.org
<ul></ul><p>Koichi Sasada wrote:</p>
<blockquote>
<p>I guess the reason is that Matz thought backtrace should be simple for main script.</p>
</blockquote>
<p>There's little benefit in having simple (i.e. basename only) path for main script (and only main script). I think consistent output is much more useful.</p>
<p>Getting basename out of full path is easier than the other way around.</p> Ruby master - Feature #10561: Improve function of Thread::Backtrace::Location #path and #absolute_pathhttps://bugs.ruby-lang.org/issues/10561?journal_id=516062015-02-23T10:11:21Zyorickpeterse (Yorick Peterse)yorickpeterse@gmail.com
<ul></ul><p>It's fine for the methods to do different things, the naming however is a bit<br>
confusing. Using <code>path</code> doesn't clearly state when it's absolute and when it's<br>
relative. Perhaps <code>script_path</code> would make more sense.</p>
<p>Merging the two is also fine, although it could potentially break code depending<br>
on the current behaviour of <code>path</code>, thus it would have to be done with some<br>
care.</p> Ruby master - Feature #10561: Improve function of Thread::Backtrace::Location #path and #absolute_pathhttps://bugs.ruby-lang.org/issues/10561?journal_id=876632020-09-23T23:03:16Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul><li><strong>Tracker</strong> changed from <i>Bug</i> to <i>Feature</i></li><li><strong>ruby -v</strong> deleted (<del><i>2.2.0</i></del>)</li><li><strong>Backport</strong> deleted (<del><i>2.0.0: UNKNOWN, 2.1: UNKNOWN</i></del>)</li></ul><p>I've improved the documentation for <code>#path</code> and <code>#absolute path</code> in <a class="changeset" title="Document difference between Thread::Backtrace::Location#{,absolute_}path They are usually the sa..." href="https://bugs.ruby-lang.org/projects/ruby-master/repository/git/revisions/fae135c5b39db173bf97dfa3c3a34eb8fb230276">fae135c5b39db173bf97dfa3c3a34eb8fb230276</a>. The current behavior is not a bug, so switching this to a feature request.</p>
<p>Personally, I'm OK with suggestions 1, 2, and 4 and willing to implement the changes if they are desired. I don't think adding <code>#filename</code> (suggestion 3) is worth it. It doesn't seem to be needed in most code, and <code>File.basename</code> can be used in the cases where it is needed (and people that need it can define the method themselves).</p> Ruby master - Feature #10561: Improve function of Thread::Backtrace::Location #path and #absolute_pathhttps://bugs.ruby-lang.org/issues/10561?journal_id=876682020-09-24T09:39:03ZEregon (Benoit Daloze)
<ul></ul><p>There is an extra difference that #absolute_path will also try to canonicalize the path and resolve symlinks, so then even if #path is absolute, #absolute_path can be different.<br>
See <a href="https://github.com/ruby/spec/blob/e829fb0bcb667013f3527142b9e29883f6904537/core/thread/backtrace/location/absolute_path_spec.rb#L39-L69" class="external">https://github.com/ruby/spec/blob/e829fb0bcb667013f3527142b9e29883f6904537/core/thread/backtrace/location/absolute_path_spec.rb#L39-L69</a></p>
<p>So in short #path is the same as <code>__FILE__</code>. #absolute_path is essentially File.realpath(#path) + it seems to work on CRuby even if the file was removed.<br>
For eval(code, binding, file, line) code, #path and #absolute_path just return <code>file</code> and do not expand it, so it can be a relative path if <code>file</code> is relative.</p>