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