Skip to content

require_relative confused by chdir #7394

@vivanishin

Description

@vivanishin

Hi!

I'm new to Ruby, but I believe, I've stumbled across a bug in JRuby implementation. I prepared a minimal example, which should be self-explanatory, so let me start with that.

Reproducer

reproducer.sh:

#!/bin/sh

cat << EOF > main.rb
Dir.chdir("foo")
require_relative "other"
puts "main finished"
EOF

cat << EOF > other.rb
puts "other finished"
EOF

mkdir -p foo

echo "MRI>"   &&  ruby main.rb
echo "JRuby>" && jruby main.rb

cd /tmp && sh ./reproducer.sh

MRI>
other finished
main finished
JRuby>
Errno::ENOENT: No such file or directory - /tmp/foo/main.rb
          realpath at org/jruby/RubyFile.java:898
  require_relative at org/jruby/RubyKernel.java:1040
            <main> at main.rb:2

Compliance

JRuby's behavior looks non-compliant to me, as language specification requires treating the other.rb path as relative to the requiring file’s path, regardless of the current working directory
https://docs.ruby-lang.org/en/3.1/Kernel.html#method-i-require_relative

Environment Information

I believe, this should be widely reproducible, but here's my setup

$ jruby -v
jruby 9.3.8.0 (2.6.8) 2022-09-13 98d69c9461 OpenJDK 64-Bit Server VM 18.0.2+0 on 18.0.2+0 +jit [x86_64-linux]

Arch Llinux (kernel 5.10.142-1), also reproduced on Ubuntu 18.04, with several versions of jruby (and an older JVM).

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