Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require_relative is broken after Dir.chdir #4700

Open
ghost opened this issue Jun 30, 2017 · 2 comments
Open

require_relative is broken after Dir.chdir #4700

ghost opened this issue Jun 30, 2017 · 2 comments

Comments

@ghost
Copy link

ghost commented Jun 30, 2017

$ ruby -v
jruby 9.1.7.0 (2.3.1) 2017-01-11 68056ae OpenJDK 64-Bit Server VM 24.131-b00 on 1.7.0_131-b00 +jit [linux-x86_64]

Files:

bin/tarona
lib/tarona.rb
game/game.rb

bin/tarona:

#!/usr/bin/env ruby
require_relative '../lib/tarona'
require_relative '../game/game'

lib/tarona:

PROJECT_ROOT = File.expand_path('..', File.dirname(__FILE__))
Dir.chdir PROJECT_ROOT

Run:

vadim@vadim:~/tarona$ bin/tarona
... Everything is fine ...

vadim@vadim:~/tarona$ cd bin
vadim@vadim:~/tarona/bin$ ./tarona
Errno::ENOENT: No such file or directory - /home/vadim/tarona/tarona
          realpath at org/jruby/RubyFile.java:865
  require_relative at uri:classloader:/jruby/kernel/kernel.rb:11
            <main> at ./tarona:6

vadim@vadim:~/tarona/bin$ cd ../..
vadim@vadim:~$ tarona/bin/tarona
Errno::ENOENT: No such file or directory - /home/vadim/tarona/tarona/bin/tarona
          realpath at org/jruby/RubyFile.java:865
  require_relative at uri:classloader:/jruby/kernel/kernel.rb:11
            <main> at tarona/bin/tarona:6

After Dir.chdir, as you can see, JRuby's relative_path is relative not to the file, but to the current working directory.
If bin/tarona is:

#!/usr/bin/env ruby
require_relative '../lib/tarona'
require 'game/game'

Everything is fine. Also, MRI does it as expected in both cases.
Quote from RubyDoc about require_relative:

Ruby tries to load ... relative to the requiring file’s path

@mprins
Copy link
Contributor

mprins commented Jun 30, 2017

you may want to check the current release, I think there were some changes and fixes in this area

@ghost
Copy link
Author

ghost commented Jul 1, 2017

9.1.12.0 has the bug too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant