Visitar URL original
Impossible to ``eval`` with ``require_relative`` · Issue #9606 · jruby/jruby · GitHub
Skip to content

Impossible to eval with require_relative #9606

Description

@Maumagnaguagno

JRuby versions 9.4.15.0, 10.1.1.0, and head (10.1.2.0-SNAPSHOT) behave differently to MRI, both locally and within GitHub Actions, when evaluating code with a require_relative call and a filename argument.
Maybe other versions also behave like this, but I have not tested them.

Consider the following minimal reproducible example:

File.write('file.rb', "puts 'success'")
eval("require_relative 'file'", TOPLEVEL_BINDING, 'otherfile.rb')

It is expected to print "success", but it only works if there is a file named as otherfile.rb present.
The file has no need to exist, as the filename provided is used for file paths and to improve backtrace messages.
Instead it prints the following error message:

Errno::ENOENT: No such file or directory - /home/mau/Desktop/test/otherfile.rb
          realpath at org/jruby/RubyFile.java:988
  require_relative at org/jruby/RubyKernel.java:1216
            <main> at otherfile.rb:1
              eval at org/jruby/RubyKernel.java:1281
            <main> at test.rb:2

Activity

  1. headius commented on Sep 10, 2026

    @headius
    Member

    This is probably because we are actually looking at the filesystem to find the relative path. I'm not sure whether that is better or worse than the CRuby behavior, to be honest.

    Can you explain why you want to do this?

  2. Maumagnaguagno commented on Sep 11, 2026

    @Maumagnaguagno
    Author

    My use case is that in one of my projects part of the code is generated at run-time based on user specified files, like a DSL, that may need require_relative to access custom methods.
    Those files used to be saved to disk and loaded later with require, but since those files may get big and are used only once I replaced with eval.
    The CRuby behavior makes it possible to avoid the filesystem, which is the goal when using eval, while executing the same code as it would if it was written to disk.
    Being able to add a custom filename helps to understand which eval generated an error without inspecting the source code through a more descriptive backtrace, which is my goal, while solving relative paths.

  3. headius commented on Sep 16, 2026

    @headius
    Member

    Got it, ok. I have a general idea how to fix this but it won't make it for 10.1.2.0. We'll get it patched for the next release.

  4. added this to the JRuby 10.1.3.0 milestone on Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions