It is undefined in the documentation how #absolute_path should operate when #path is invalid (in case of instance eval)
There are a few conditions where #path and #absolute_path can return nil, this forces extra protection code when parsing paths to check for nil. (for example getting filename)
Suggestions:
Instead of returning Qnil from location_path and location_absolute_path on invalid conditions, return the string "(unknown)" which is easier to parse and sticks out better in a big backtrace. There is precedent here with the string "(eval)"
If path is invalid have absolute_path return "(unknown)", define that in the documentation
(possible) add an additional method on caller_location called #filename so people stop parsing filename from #path and #absolute_path
Evaluate if it makes sense to have #path and #absolute_path in the API as both methods can return full paths so the semantic difference is subtle.
I'd like to have some extra clarification on this matter as well. Right now in Rubinius we return just the filename when calling Thread::Backtrace::Location#path as the documentation suggested this was the expected behaviour:
Returns the file name of this frame.
For example, using caller_locations.rb from Thread::Backtrace::Location
loc = c(0..1).first
loc.path #=> caller_locations.rb
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.
Nobu: I talked about this with Koichi today after noticing you added tests for
path/absolute_path after my talk at FOSDEM. Since these tests assert that path
returns the full path I'd like to know which one is correct.
If the last example is correct, is it perhaps an idea to add "basename" to
Thread::Location::Backtrace, which would return just the filename, and make
path an alias to absolute_path? So:
Benoit: correct, I just found this out by messing around more with this. This
behaviour is extremely confusing, especially since the tests both just compare
if path and absolute_path equal __FILE__.
How about to unify #path and #absolute_path?
Returning absolute path on both methods.
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).
I think we don't need to separate (1) and (2). (1) can be absolute path.
I guess the reason is that Matz thought backtrace should be simple for main script.
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.
Getting basename out of full path is easier than the other way around.
It's fine for the methods to do different things, the naming however is a bit
confusing. Using path doesn't clearly state when it's absolute and when it's
relative. Perhaps script_path would make more sense.
Merging the two is also fine, although it could potentially break code depending
on the current behaviour of path, thus it would have to be done with some
care.
I've improved the documentation for #path and #absolute path in fae135c5b39db173bf97dfa3c3a34eb8fb230276. The current behavior is not a bug, so switching this to a feature request.
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 #filename (suggestion 3) is worth it. It doesn't seem to be needed in most code, and File.basename can be used in the cases where it is needed (and people that need it can define the method themselves).
So in short #path is the same as __FILE__. #absolute_path is essentially File.realpath(#path) + it seems to work on CRuby even if the file was removed.
For eval(code, binding, file, line) code, #path and #absolute_path just return file and do not expand it, so it can be a relative path if file is relative.