Issue #12159 has been updated by ko1 (Koichi Sasada).


After consideration, I changed my proposal.

* Rename `#absolute_path` to `#real_path` (or `#realpath`) and make `#aboluste_path` as alias of `#real_path`.
* change `absolute_path` (`real_path`) on `eval` with given file name.

# summary of current behavior

On MRI, "path" is used several ways.

* `__FILE__`
* `caller(_locations)`, backtrace
* `requre_relative` (base directory)
* `$0`

And ISeq has `path` and `absolute_path`. I use this terminology.

* path: given path.
* absolute_path: realpath(path) if path is exist. If not, it is nil.

Above usages are implemented with `path` and `absolute_path`.

```
* `__FILE__`                         # path
* `caller(_locations)`, backtrace    # path
* `requre_relative` (base directory) # absolute_path
* `$0`                               # path
```

Most of case, path and absolute_path is same. However, the following case they are not same.

* `path` and `realpath(path)` is different because of symlink (`absolute_path` is realpath).
* script name is given by command parameter (`ruby x.rb`) (`absolute_path` will be `/path/to/x.rb`).
* `eval()` without file name (`path` will be "(eval)" and `absolute_path` will be nil).

Note that `eval(script, binding, "x.rb")` makes `path` and `absolute_path` return `"x.rb"` even if given file name is not realpath.

```
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"]
```

# proposal

Checking current behavior, `#absolute_path` is used as `realpath` (check the existing and resolve symlink). So I want to add `#realpath` or `#real_path`. I'm not sure which is better because there is `File#realpath` and `absolute_path` include `_`.

Also I want to check realpass for file name given at `eval()`. If file name is not existing, `#realpath` should be `nil`. In this case, `require_relative` should fail because MRI can't infer the base directory.

How about it?


----------------------------------------
Bug #12159: Thread::Backtrace::Location#path returns absolute path for files loaded by require_relative
https://bugs.ruby-lang.org/issues/12159#change-65168

* Author: tagomoris (Satoshi TAGOMORI)
* Status: Assigned
* Priority: Normal
* Assignee: ko1 (Koichi Sasada)
* Target version: 
* ruby -v: ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin14]
* Backport: 2.1: UNKNOWN, 2.2: UNKNOWN, 2.3: UNKNOWN
----------------------------------------
I expected that Thread::Backtrace::Location#path always returns base filename, but returns absolute path for files loaded by require_relative.
Is it intentional? or a bug?

~~~
$ ruby -v
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin14]
$ cat > x.rb
def a
  caller_locations
end

p a.first.path
$ cat > y.rb
require_relative "x"
$ ruby x.rb 
"x.rb"
$ ruby y.rb
"/Users/tagomoris/x.rb"
~~~



-- 
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>