Skip to content

require_relative through symlinks behaves differently in jruby than in MRI ruby #3092

@storrence

Description

@storrence

If you require a source file that contains require_relative through a symlink in a different diretory, MRI ruby interprets the require_relative relative to the directory containing the link target but jruby 9.0.0.0.rc1 interprets it relative to the directory containing the symlink itself.

The real application code that brought this to my attention is simulated by the following source files and symlink:

included_by_target.rb
link_target.rb
main.rb
subdir/link.rb -> ../link_target.rb
# link_target.rb
require_relative './included_by_target'
class LinkTarget
  include IncludedByTarget
end
# included_by_target.rb
module IncludedByTarget
  def do_something
    puts "  #{self.class.name}#do_something"
  end
end
# main.rb
require 'subdir/link'
LinkTarget.new.do_something

This code works in MRI ruby:

$ruby --version
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]

$ruby -I . main.rb
  LinkTarget#do_something

but fails under jruby 9.0.0.0.rc1. Note that the interpreter is looking for the included file in the directory containing the symlink rather than in the directory containing the linked file:

$ruby --version
jruby 9.0.0.0.rc1 (2.2.2) 2015-06-10 a0bf3b3 OpenJDK 64-Bit Server VM 25.45-b02 on 1.8.0_45-b14 +jit [linux-amd64]

$ruby -I . main.rb
LoadError: no such file to load -- /home/storrence/temp/ruby/jruby_require_relative/subdir/included_by_target
           require at org/jruby/RubyKernel.java:940
           require at /home/storrence/.rvm/rubies/jruby-9.0.0.0.rc1/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb:54
  require_relative at uri:classloader:/jruby/kernel/kernel.rb:24
             <top> at /home/storrence/temp/ruby/jruby_require_relative/subdir/link.rb:1
           require at org/jruby/RubyKernel.java:940
            (root) at /home/storrence/.rvm/rubies/jruby-9.0.0.0.rc1/lib/ruby/stdlib/rubygems/core_ext/kernel_require.rb:1
             <top> at main.rb:1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions