https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112011-06-26T18:27:43ZRuby Issue Tracking SystemRuby master - Bug #4487: require_relative fails in an eval'ed filehttps://bugs.ruby-lang.org/issues/4487?journal_id=184342011-06-26T18:27:43Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>mame (Yusuke Endoh)</i></li></ul> Ruby master - Bug #4487: require_relative fails in an eval'ed filehttps://bugs.ruby-lang.org/issues/4487?journal_id=188252011-07-05T02:53:06Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>ruby -v</strong> changed from <i>ruby 1.9.3dev (2011-03-04 trunk 31024) [i386-mingw32]</i> to <i>-</i></li></ul><p>Hello,</p>
<blockquote>
<p>Â $cat eval_me1.rb<br>
 eval(File.read('eval_me2.rb'), binding, File.expand_path('./eval_me2.rb'))<br>
 $cat eval_me2.rb<br>
 require_relative 'eval_me1.rb'<br>
 $ ruby eval_me1.rb<br>
 C:/dev/ruby/faster_require/spec/eval_me2.rb:1:in <code>require_relative': cannot infer basepath (LoadError)     from C:/dev/ruby/faster_require/spec/eval_me2.rb:1:in </code>'<br>
    from eval_me1.rb:1:in <code>eval'     from eval_me1.rb:1:in </code>'</p>
<p>I suppose was assuming that if eval included a filename, then require_relative would work from within it. Perhaps I am mistaken?</p>
</blockquote>
<p>I think your expectation is reasonable, though I personally dislike<br>
the eval's feature to fake filepath.</p>
<p>The following patch makes require_relative use the given file path.<br>
I'm afraid if I should include this patch in 1.9.3 because I can't<br>
estimate the impact of this patch. What do you think?</p>
<p>diff --git a/vm_eval.c b/vm_eval.c<br>
index 7df7f5f..3710401 100644<br>
--- a/vm_eval.c<br>
+++ b/vm_eval.c<br>
@@ -1007,7 +1007,7 @@ eval_string_with_cref(VALUE self, VALUE src,<br>
VALUE scope, NODE <em>cref, const char<br>
/</em> make eval iseq */<br>
th->parse_in_eval++;<br>
th->mild_compile_error++;</p>
<ul>
<li>iseqval</li>
</ul> Ruby master - Bug #4487: require_relative fails in an eval'ed filehttps://bugs.ruby-lang.org/issues/4487?journal_id=188282011-07-05T12:52:50Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Assignee</strong> changed from <i>mame (Yusuke Endoh)</i> to <i>matz (Yukihiro Matsumoto)</i></li></ul><p>Related to <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: [patch] Fix eval(s, b) backtrace; make eval(s, b) consistent with eval(s) (Closed)" href="https://bugs.ruby-lang.org/issues/4352">#4352</a>.</p>
<p>I need matz's judgment.</p>
<p>--<br>
Yusuke Endoh <a href="mailto:mame@tsg.ne.jp" class="email">mame@tsg.ne.jp</a></p> Ruby master - Bug #4487: require_relative fails in an eval'ed filehttps://bugs.ruby-lang.org/issues/4487?journal_id=278252012-07-05T17:07:56ZLTe (Piotr Niełacny)piotr.nielacny@gmail.com
<ul></ul><p>=begin<br>
If in irb we can execute</p>
<p>(({load("file.rb")}))</p>
<p>why we can't</p>
<p>(({require_relative("file")}))</p>
<p>Ruby just return exception (LoadError: cannot infer basepath). Unfortunately, this is a 'lie' because ruby can recognize basepath.<br>
=end</p> Ruby master - Bug #4487: require_relative fails in an eval'ed filehttps://bugs.ruby-lang.org/issues/4487?journal_id=278262012-07-05T17:26:23Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>=begin</p>
<p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/5573">@LTe (Piotr Niełacny)</a> sorry, I can't get it. load loads from $LOAD_PATH, while require_relative requires from relative path. They are different.</p>
<p>=end</p> Ruby master - Bug #4487: require_relative fails in an eval'ed filehttps://bugs.ruby-lang.org/issues/4487?journal_id=278492012-07-06T16:04:24ZLTe (Piotr Niełacny)piotr.nielacny@gmail.com
<ul></ul><p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/10">@shyouhei (Shyouhei Urabe)</a> yes I agree but load method tries to load file from relative path. When load method can't find file in relative path it loads from $LOAD_PATH. The same can be done by require_relative (recognize path).</p>
<p><a href="https://github.com/ruby/ruby/pull/139" class="external">https://github.com/ruby/ruby/pull/139</a></p> Ruby master - Bug #4487: require_relative fails in an eval'ed filehttps://bugs.ruby-lang.org/issues/4487?journal_id=278542012-07-06T17:52:39Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>=begin<br>
<a class="user active user-mention" href="https://bugs.ruby-lang.org/users/5573">@LTe (Piotr Niełacny)</a> I'd rather ask you "require_relative loads something relative from WHAT?"</p>
<p>Obviously it is not relative from your mind :)</p>
<p>Current require_relative loads relative from where the require_relative command is written. So when in IRB sessions, it fails to infer where it is beacuse the command is written in a non-file (console).</p>
<p>OTOH load loads from process PWD, which is possible in IRB.</p>
<p>So the point is, if you want require_relative to work on an IRB session, you have to define "from where require_relative should search relativeness".<br>
=end</p> Ruby master - Bug #4487: require_relative fails in an eval'ed filehttps://bugs.ruby-lang.org/issues/4487?journal_id=278562012-07-06T18:13:07ZEregon (Benoit Daloze)
<ul></ul><p>shyouhei (Shyouhei Urabe) wrote:</p>
<blockquote>
<p>Current require_relative loads relative from where the require_relative command is written. So when in IRB sessions, it fails to infer where it is beacuse the command is written in a non-file (console).</p>
<p>OTOH load loads from process PWD, which is possible in IRB.</p>
<p>So the point is, if you want require_relative to work on an IRB session, you have to define "from where require_relative should search relativeness".</p>
</blockquote>
<p>From the process current working directory I guess, especially since you almost always launch IRB from a terminal.</p>
<p>Personally I'm doing <code>require './myfile'</code> which is not the most elegant, but if you don't have completion in IRB, that's shorter to type.</p>
<p>Otherwise, there's always the option to do <code>irb -I.</code> and use plain <code>require</code>.</p> Ruby master - Bug #4487: require_relative fails in an eval'ed filehttps://bugs.ruby-lang.org/issues/4487?journal_id=278582012-07-06T18:59:28Znaruse (Yui NARUSE)naruse@airemix.jp
<ul></ul><p>Eregon (Benoit Daloze) wrote:</p>
<blockquote>
<p>shyouhei (Shyouhei Urabe) wrote:</p>
<blockquote>
<p>Current require_relative loads relative from where the require_relative command is written. So when in IRB sessions, it fails to infer where it is beacuse the command is written in a non-file (console).</p>
<p>OTOH load loads from process PWD, which is possible in IRB.</p>
<p>So the point is, if you want require_relative to work on an IRB session, you have to define "from where require_relative should search relativeness".</p>
</blockquote>
<p>From the process current working directory I guess, especially since you almost always launch IRB from a terminal.</p>
<p>Personally I'm doing <code>require './myfile'</code> which is not the most elegant, but if you don't have completion in IRB, that's shorter to type.</p>
<p>Otherwise, there's always the option to do <code>irb -I.</code> and use plain <code>require</code>.</p>
</blockquote>
<p>require_relative is introduced to avoid accidentally require a malicious file on the current working directory.<br>
So it can't be acceptable.<br>
Use require or load on such case.</p> Ruby master - Bug #4487: require_relative fails in an eval'ed filehttps://bugs.ruby-lang.org/issues/4487?journal_id=278592012-07-06T19:08:03Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>naruse (Yui NARUSE) wrote:</p>
<blockquote>
<p>require_relative is introduced to avoid accidentally require a malicious file on the current working directory.<br>
So it can't be acceptable.<br>
Use require or load on such case.</p>
</blockquote>
<p>I'm not pretty sure about this. Is there a chance for a (proposed behaviour of) require_relative to require a malicious file on the current directory?</p>
<p>Because you are on an IRB session and intentionally emitting require_relative (not require), I doubt the danger you say.</p> Ruby master - Bug #4487: require_relative fails in an eval'ed filehttps://bugs.ruby-lang.org/issues/4487?journal_id=278602012-07-06T19:21:03Znaruse (Yui NARUSE)naruse@airemix.jp
<ul></ul><p>shyouhei (Shyouhei Urabe) wrote:</p>
<blockquote>
<p>naruse (Yui NARUSE) wrote:</p>
<blockquote>
<p>require_relative is introduced to avoid accidentally require a malicious file on the current working directory.<br>
So it can't be acceptable.<br>
Use require or load on such case.</p>
</blockquote>
<p>I'm not pretty sure about this. Is there a chance for a (proposed behaviour of) require_relative to require a malicious file on the current directory?</p>
<p>Because you are on an IRB session and intentionally emitting require_relative (not require), I doubt the danger you say.</p>
</blockquote>
<ul>
<li>irb is not the only user of eval.</li>
<li>A user won't always use require_relative intentionally.</li>
<li>There is a suitable another way: require './myfile'</li>
</ul>
<p>With those reason, I don't think require_relative should be changed.</p> Ruby master - Bug #4487: require_relative fails in an eval'ed filehttps://bugs.ruby-lang.org/issues/4487?journal_id=278612012-07-06T20:06:19ZEregon (Benoit Daloze)
<ul></ul><p>naruse (Yui NARUSE) wrote:</p>
<blockquote>
<p>Eregon (Benoit Daloze) wrote:</p>
<blockquote>
<p>From the process current working directory I guess, especially since you almost always launch IRB from a terminal.</p>
</blockquote>
<p>require_relative is introduced to avoid accidentally require a malicious file on the current working directory.<br>
So it can't be acceptable.<br>
Use require or load on such case.</p>
</blockquote>
<p>I see, you're right.<br>
Indeed, with this in mind I think it's not worth changing, and the actual require_relative behavior is clearer (relative to "this file" directory, if there is no accurate "this file", just #raise).</p> Ruby master - Bug #4487: require_relative fails in an eval'ed filehttps://bugs.ruby-lang.org/issues/4487?journal_id=280352012-07-14T14:59:15Zko1 (Koichi Sasada)
<ul><li><strong>Assignee</strong> changed from <i>matz (Yukihiro Matsumoto)</i> to <i>mame (Yusuke Endoh)</i></li></ul><p>mame-san, please ask matz.</p> Ruby master - Bug #4487: require_relative fails in an eval'ed filehttps://bugs.ruby-lang.org/issues/4487?journal_id=357772013-02-02T13:06:57Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Subject</strong> changed from <i>require_relative fails in an eval'ed file</i> to <i>require_relative fails in an eval&#x27;ed file</i></li><li><strong>Assignee</strong> changed from <i>mame (Yusuke Endoh)</i> to <i>matz (Yukihiro Matsumoto)</i></li><li><strong>Target version</strong> set to <i>2.6</i></li></ul> Ruby master - Bug #4487: require_relative fails in an eval'ed filehttps://bugs.ruby-lang.org/issues/4487?journal_id=378392013-03-23T14:50:16ZConrad.Irwin (Conrad Irwin)conrad.irwin@gmail.com
<ul></ul><p>This bug also affects pry: <a href="https://github.com/pry/pry/issues/880" class="external">https://github.com/pry/pry/issues/880</a>. Our use-case is slightly different because we are doing TOPLEVEL_BINDING.eval("some code", "/absolute/path.rb").</p>
<p>I think that when an absolute path is set in eval, then require_relative should use it.</p> Ruby master - Bug #4487: require_relative fails in an eval'ed filehttps://bugs.ruby-lang.org/issues/4487?journal_id=538612015-08-18T14:28:24Zjulik (Julik Tarkhanov)me@julik.nl
<ul></ul><p>This is actually very pertinent for Rack as well, because currently config.ru does not support require_relative which is very counterintuitive.</p> Ruby master - Bug #4487: require_relative fails in an eval'ed filehttps://bugs.ruby-lang.org/issues/4487?journal_id=682242017-12-07T11:49:06Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Closed</i></li></ul><p>Now, it works. I'm unsure who changed the behavior... Anyway, closing.</p>