https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112018-07-03T02:38:19ZRuby Issue Tracking SystemRuby master - Bug #14891: Pathname#join has different behaviour to File.joinhttps://redmine.ruby-lang.org/issues/14891?journal_id=727852018-07-03T02:38:19Zrobotdana (Dana Sherson)
<ul><li><strong>ruby -v</strong> set to <i>2.6.0-preview2, and before</i></li></ul> Ruby master - Bug #14891: Pathname#join has different behaviour to File.joinhttps://redmine.ruby-lang.org/issues/14891?journal_id=727862018-07-03T07:24:22Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>This behaviour surprised me too. Is it expected that the information from "/a"<br>
is lost? I have no idea but the documentation does not mention this; perhaps<br>
the above could be added as an example OR the behaviour changed (or both).</p>
<p>I have little to none experience with pathname these days as I seem to handle<br>
paths ... without pathname. :D</p>
<p>Documentation to Pathname .join is at:</p>
<p><a href="https://ruby-doc.org/stdlib-2.5.1/libdoc/pathname/rdoc/Pathname.html#method-i-join" class="external">https://ruby-doc.org/stdlib-2.5.1/libdoc/pathname/rdoc/Pathname.html#method-i-join</a></p> Ruby master - Bug #14891: Pathname#join has different behaviour to File.joinhttps://redmine.ruby-lang.org/issues/14891?journal_id=727872018-07-03T08:05:39ZHanmac (Hans Mackowiak)hanmac@gmx.de
<ul></ul><p>More examples:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Pathname</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="s1">'/a'</span><span class="p">).</span><span class="nf">join</span><span class="p">(</span><span class="s1">'c'</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">).</span><span class="nf">to_s</span> <span class="c1">#=> "/a/c/b"</span>
<span class="no">Pathname</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="s1">'/a'</span><span class="p">).</span><span class="nf">join</span><span class="p">(</span><span class="s1">'/c'</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">).</span><span class="nf">to_s</span> <span class="c1">#=> "/c/b"</span>
<span class="no">Pathname</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="s1">'/a'</span><span class="p">).</span><span class="nf">join</span><span class="p">(</span><span class="s1">'/c'</span><span class="p">,</span> <span class="s1">'/b'</span><span class="p">).</span><span class="nf">to_s</span> <span class="c1">#=> "/b"</span>
</code></pre>
<p>Why it does this?<br>
because <code>"/c"</code> means start of an absolute path there</p> Ruby master - Bug #14891: Pathname#join has different behaviour to File.joinhttps://redmine.ruby-lang.org/issues/14891?journal_id=729032018-07-09T12:05:08Zznz (Kazuhiro NISHIYAMA)
<ul></ul><p>I think it is expected behavior.</p>
<p><a href="https://github.com/ruby/ruby/blob/38e05ff3e194268fd2f38ac7c9530298e464f07b/test/pathname/test_pathname.rb#L211" class="external">https://github.com/ruby/ruby/blob/38e05ff3e194268fd2f38ac7c9530298e464f07b/test/pathname/test_pathname.rb#L211</a></p>
<pre><code class="ruby syntaxhl" data-language="ruby"> <span class="n">defassert</span><span class="p">(</span><span class="ss">:plus</span><span class="p">,</span> <span class="s1">'/b'</span><span class="p">,</span> <span class="s1">'a'</span><span class="p">,</span> <span class="s1">'/b'</span><span class="p">)</span>
</code></pre> Ruby master - Bug #14891: Pathname#join has different behaviour to File.joinhttps://redmine.ruby-lang.org/issues/14891?journal_id=729422018-07-13T04:58:02Zfunny_falcon (Yura Sokolov)funny.falcon@gmail.com
<ul></ul><p>I'd rather say that File.join is currently broken, and it should behave like Pathname.join. But probably I'm missing something.</p> Ruby master - Bug #14891: Pathname#join has different behaviour to File.joinhttps://redmine.ruby-lang.org/issues/14891?journal_id=842952020-02-18T07:58:10Zjnchito (Junichi Ito)
<ul></ul><p>I am wondering about the current behavior of Pathname#join, too. Are there any useful use cases for <code>Pathname.new('/a').join('/c', '/b').to_s #=> "/b"</code>? I think it should be <code>Pathname.new('/a').join('/c', '/b').to_s #=> "/a/c/b</code> like File#join. I'd like to know the basic idea behind this design.</p> Ruby master - Bug #14891: Pathname#join has different behaviour to File.joinhttps://redmine.ruby-lang.org/issues/14891?journal_id=842962020-02-18T10:09:44Zzverok (Victor Shepelev)zverok.offline@gmail.com
<ul></ul><blockquote>
<p>I am wondering about the current behavior of Pathname#join, too. Are there any useful use cases for <code>Pathname.new('/a').join('/c', '/b').to_s #=> "/b"</code></p>
</blockquote>
<p>I believe that <code>Pathname#join</code> acts kinda like <code>cd</code> in the shell: <code>cd x</code> is "go deeper to <code>x</code> level" while <code>cd /x</code> si "go to the root, then into <code>/x</code> folder".</p>
<p>I believe it can be useful and desired behavior when working with configs for some authoring systems and devops tools, where you take from config "do 'action' by path 'path'", and config can specify lot of actions relative to app folder (like <code>touch: "tmp/restart.txt"</code>), but eventually want to specify something to do in relative-from-root folder (like <code>read: "/etc/myapp/settings.ini"</code>). Without following the specification "<code>/</code> at the beginning means go to root" it becomes rather ugly.</p>
<p>In other words, this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="c1"># I am just trying to merge pathes explicitly, and receive "unexpected" result:</span>
<span class="no">Pathname</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="s1">'foo'</span><span class="p">).</span><span class="nf">merge</span><span class="p">(</span><span class="s1">'/bar'</span><span class="p">)</span>
</code></pre>
<p>— might seem "weird", while this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="vi">@app_path</span> <span class="o">=</span> <span class="no">Path</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="n">__dir__</span><span class="p">)</span>
<span class="c1"># ...</span>
<span class="vi">@app_path</span><span class="p">.</span><span class="nf">join</span><span class="p">(</span><span class="n">action</span><span class="p">.</span><span class="nf">target_path</span><span class="p">)</span> <span class="c1"># when target_path is "/foo/bar", it allows to act outside of base dir</span>
</code></pre>
<p>— is desirable (and is NOT achievable if <code>Pathname's</code> behavior will be changed)</p>
<p>I assume that those finding the behavior less logical think about paths about "just some abstract strings" and <code>Pathname#join</code> as a fancy way to write <code>Array#join(SYSTEM_PATH_DELIMITER)</code>. But <code>Pathname</code> tries to represent "filesystem path object" in a meaningful way, consistent with filesystem's intuitions.</p> Ruby master - Bug #14891: Pathname#join has different behaviour to File.joinhttps://redmine.ruby-lang.org/issues/14891?journal_id=842972020-02-18T13:39:16Zjnchito (Junichi Ito)
<ul></ul><p>zverok (Victor Shepelev) wrote in <a href="#note-7">#note-7</a>:</p>
<blockquote>
<blockquote>
<p>I am wondering about the current behavior of Pathname#join, too. Are there any useful use cases for <code>Pathname.new('/a').join('/c', '/b').to_s #=> "/b"</code></p>
</blockquote>
<p>I believe that <code>Pathname#join</code> acts kinda like <code>cd</code> in the shell: <code>cd x</code> is "go deeper to <code>x</code> level" while <code>cd /x</code> si "go to the root, then into <code>/x</code> folder".</p>
</blockquote>
<p>Thank you for your explanation. It is easy to understand the behavior if I think <code>Pathname.new('/a').join('/c', '/b')</code> means <code>cd /a</code> then <code>cd /c</code> then <code>cd /b</code>. (params can be both directories and files, though.)</p>
<blockquote>
<p>I assume that those finding the behavior less logical think about paths about "just some abstract strings" and <code>Pathname#join</code> as a fancy way to write <code>Array#join(SYSTEM_PATH_DELIMITER)</code>. But <code>Pathname</code> tries to represent "filesystem path object" in a meaningful way, consistent with filesystem's intuitions.</p>
</blockquote>
<p>That is me who thought "<code>Pathname#join</code> as a fancy way to write <code>Array#join(SYSTEM_PATH_DELIMITER)</code>". However, Rails developers like me often see <code>Rails.root.join('foo/bar.jpg')</code> as main use case of <code>Pathname#join</code>. So I guess many of them might misunderstand it just joins two strings in a readable way.</p>
<p>In addition, it is very confusing <code>File#join</code> has the same name and different behavior, and File and Pathname are also close idea. Many people might misunderstand they act in the same way. So probably <code>Pathname#join</code> should have been given an another good name. (<code>Pathname#merge</code> might not be a bad idea.)</p> Ruby master - Bug #14891: Pathname#join has different behaviour to File.joinhttps://redmine.ruby-lang.org/issues/14891?journal_id=951912021-12-07T05:26:14Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Closed</i></li></ul>