Fix __dir__ for eval filenames#9500
Conversation
JRuby expanded all Kernel#__dir__ source locations through File.expand_path, including filenames supplied to eval, instance_eval, and module_eval. CRuby treats explicit eval filenames differently: __FILE__ reports the filename as supplied and __dir__ returns File.dirname(filename) without expanding a relative path. For evals without an explicit filename, __dir__ returns nil.\n\nTrack whether an eval filename was synthesized by JRuby when the eval is created, carry that bit through Binding and IREvalScript, and have Kernel#__dir__ use the eval source metadata to choose the CRuby-compatible behavior. Normal loaded files keep the existing expansion path.\n\nThis also removes the old failing tags and adds coverage for instance_eval and methods defined inside eval, so the behavior survives beyond the immediate dynamic eval frame.
|
A note on the shape of this fix, since CRuby has a useful distinction here. CRuby stores source location as both This JRuby patch is intentionally narrower than copying CRuby's full path/realpath model. JRuby does not currently have the same iseq
The I think that keeps this as a targeted compatibility bug fix rather than a broader source-location refactor, while still matching CRuby's observable semantics for |
|
Ignoring for the moment that it didn't pass specs, I'll respond to the changes...
This is perhaps a good short-term fix but I actually want to get full and relative paths both propagated through the AST into the JRuby IR and JIT output. There's a number of reasons for this, but probably the biggest would be to make it easier for JVM debuggers to find loaded files based on the compiled code; currently we embed a full path, which most debuggers will ignore (they can't can the entire system for sources). The other compatiblity issues (and I'll review the changes separately. |
headius
left a comment
There was a problem hiding this comment.
This change is fine as a workaround for the lack of relative+full path information in the AST and IR, but I would want to push toward adding that information and eliminating this logic in the future.
The failing spec appears to be one added specifically for this PR, so it appears there's a bug there in the spec or the code.
(The stdlib failures are known-to-be-flaky tests)
Fix
Kernel#__dir__for code evaluated with explicit filenames so relative eval filenames stay relative, matching CRuby.Context
JRuby currently expands
__dir__for eval filenames through the normal file path handling. This differs from CRuby for code like:CRuby reports
__FILE__as"foo/bar/print.rb"and__dir__as"foo/bar". JRuby already reports__FILE__correctly, but__dir__was expanded to an absolute path. For evals without an explicit filename, CRuby returnsnilfor__dir__.Changes
BindingandIREvalScript, including methods defined inside eval.Kernel#__dir__to use eval source metadata:File.dirname(filename)without expansionnil__dir__spec tags.instance_evalwith an explicit filename and a method defined inside eval.Tophatting
Ran locally with JDK 25: