[#104307] Float truncate — Eustáquio Rangel <eustaquiorangel@...>
Hi!
4 messages
2021/06/16
[ruby-core:104429] [Ruby master Feature#16978] Ruby should not use realpath for __FILE__
From:
merch-redmine@...
Date:
2021-06-28 21:06:02 UTC
List:
ruby-core #104429
Issue #16978 has been updated by jeremyevans0 (Jeremy Evans).
vo.x (Vit Ondruch) wrote in #note-13:
> Dan0042 (Daniel DeLorme) wrote in #note-12:
> > I think this is a bug and should be fixed, but IMO the proper fix is to use realpath for `__FILE__`
>
> This is though one thinking about this again, but you are probably right. Maybe the biggest concern is that the behavior is inconsistent. If the `require` and `__FILE__` used `realpath`, I think the example in #16978-11 would work as you said.
It's not difficult to make `__FILE__` use `realpath`:
```diff
@@ -10307,6 +10307,14 @@ numparam_nested_p(struct parser_params *p)
return 0;
}
+VALUE rb_realpath_internal(VALUE basedir, VALUE path, int strict);
+
+static VALUE
+rb_realpath__FILE__(VALUE file)
+{
+ return rb_realpath_internal(Qnil, file, 1);
+}
+
static NODE*
gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
{
@@ -10326,8 +10334,19 @@ gettable(struct parser_params *p, ID id, const YYLTYPE *loc)
VALUE file = p->ruby_sourcefile_string;
if (NIL_P(file))
file = rb_str_new(0, 0);
- else
- file = rb_str_dup(file);
+ else if (compile_for_eval) {
+ file = rb_str_dup(file);
+ }
+ else {
+ int state;
+ VALUE realpath_file = rb_protect(rb_realpath__FILE__, file, &state);
+ if (state) {
+ file = rb_str_dup(file);
+ }
+ else {
+ file = realpath_file;
+ }
+ }
node = NEW_STR(file, loc);
RB_OBJ_WRITTEN(p->ast, Qnil, file);
}
```
Unfortunately, if `__FILE__` uses `realpath`, unless you also change the behavior of `$0` (which I'm fairly sure would not be acceptable), you break the common idiom for executing code if the file is executed instead of required:
```ruby
if __FILE__ == $0
do_something
end
```
----------------------------------------
Feature #16978: Ruby should not use realpath for __FILE__
https://bugs.ruby-lang.org/issues/16978#change-92682
* Author: vo.x (Vit Ondruch)
* Status: Open
* Priority: Normal
* Assignee: nobu (Nobuyoshi Nakada)
----------------------------------------
This is the simplest test case:
~~~
$ mkdir a
$ echo "puts __FILE__" > a/test.rb
$ ln -s a b
$ ruby -Ib -e "require 'test'"
/builddir/a/test.rb
~~~
This behavior is problematic, because Ruby should not know nothing about the `a` directory. It was not instructed to use it. I should always refer to the file using the original path and do not dig into the underlying details, otherwise depending on file system setup, one might be forced to used `File.realpath` everywhere trying to use `__FILE__`.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>