call Kernel.require ruby method from autoload callback instead of callin... #281

wants to merge 1 commit into


None yet

4 participants


In jruby, the autoload mechanism doesn't actually call Kernel.require like the rubydocs claims it will. This change attempts to fix that.


autoload(module, filename) → nil click to toggle source

Registers filename to be loaded (using Kernel::require) the first time that module (which may be a String or a symbol) is accessed in the namespace of mod.

Note that this change uses the context when require happens rather than when the autoload method is called. I wasn't sure what the right thing to do is, or whether it really matters.

The purpose of this change is to allow the user to override Kernel.require even with autoloading. I assume it rubygems is the most well-known overrider of Kernel.require. It presumably ends up pulling in enough stuff into LOAD_PATH via direct requires that any autoloading it does still works, but I suspect it could be problematic if it tried to add paths for autoloaded files.

Thanks for your consideration,



This pull request passes (merged a311d53 into b37a0bd).

JRuby Team member

Ok, the change looks fine, but I want to make sure this is actually proper...i.e. that 1.9.3 does this too. I know there have been debates about MRI sometimes not actually dynamically dispatching to methods like require, and I think it should dynamically dispatch here, but doing so when MRI does not would be a significant behavioral difference.

JRuby Team member

Ok, my fears are confirmed...unfortunately.


system ~/projects/jruby $ cat blah.rb
puts 'there'

Before your patch:

system ~/projects/jruby $ jruby -e "class Foo; autoload :Bar, './blah.rb'; end; def Kernel.require(a); puts a; end; Foo::Bar"
NameError: uninitialized constant Foo::Bar
  const_missing at org/jruby/
         (root) at -e:1

After your patch:

system ~/projects/jruby $ jruby -e "class Foo; autoload :Bar, './blah.rb'; end; def Kernel.require(a); puts a; end; Foo::Bar"
NameError: uninitialized constant Foo::Bar
  const_missing at org/jruby/
         (root) at -e:1

Ruby 1.9.3:

system ~/projects/jruby $ ruby-1.9.3 -e "class Foo; autoload :Bar, './blah.rb'; end; def Kernel.require(a); puts a; end; Foo::Bar"
-e:1:in `<main>': uninitialized constant Foo::Bar (NameError)

So yeah, the bad news is that MRI does not exhibit this behavior, regardless of what the documentation claims, and therefore we can't accept this patch.

I would support you if you want to raise this as an issue with the MRI folks, since I agree it should be redispatching here to pick up require hooks like RubyGems installs. However, at this time I need to reject this pull request :(

@headius headius closed this Sep 4, 2012
JRuby Team member

We should solve that issue for 2.0. See, "Make autoload for stdlib gems work as long as autoload feature exists in 2.0."

Thanks @dontfidget, it would be nice to have test next time!

@eregon eregon added a commit that referenced this pull request Aug 19, 2016
@eregon eregon Squashed 'spec/ruby/' changes from 884bfb7..b6baa6d
b6baa6d Module#define_method accepts attr_accessor methods
818e0fe Fix sized enumerators of Numeric#step with Infinity
6488c84 Add a spec for a case then protected method called from public_send in own method
1be7799 Added Thread#name, #name=.
cb2cf57 Spec for module_function's singleton method with super.
e06f420 Clarify $~ spec by using different variables for different cases
1989a8d Additional Hash#to_proc specs from JRuby's suite.
ef90111 Add multi-threaded Etc.getgrgid spec.
7ecbd0b Use a more sensible command line for a RUBYOPT spec
b09bd70 Ensure to use -e for -e specs
9b31990 Add spec for creating instances of a cloned Time class
6dafa0b Add a spec for Hash#to_proc result passed as instance_exec block.
85ebe53 splatted hashes need to have symbol keys.  Adding spec
351ddff Port specs from Array#max in Array#min
630ed7f Import Array#max/min from JRuby+Truffle specs, themselves from Enumerable specs
bb51c64 Spec out Array#max a tiny bit
ea4ab25 Add missing fixture
07c74cb Use a fixture for common code in #spawn specs
a26029c Fix expectation with the newer output
a102ba3 Kernel#spawn should not take an Array as it means something quite different
869dc7c Make sure the path returned by Dir.pwd is absolute/expanded
641bc01 Use the system ruby when the environment is empty in #spawn
77132b3 Fail early if the Signalizer thread dies
0312337 Use /bin/env to print the environment when the passed environment is empty or almost
6987ef5 Make sure the thread is still alive while waiting for pid in Process#kill
3c57ec7 Keep at least PATH when unsetting all other environment variables
4f0609f Use a fixture rather than inline code which needs lots of escape characters
6cff59a Replace an occurrence of sleep in Signal#trap specs
94c1ac4 add spec. zero timeout returns immediately
8edcc46 Add note in Class#dup spec
73adaa2 [Truffle] Pass rb_str_new specs.
62520ae Notify when compilation failed and use system ruby
340e85c Merge pull request #286 from etehtsea/tcpserver-impr
903b5e6 Add basic TCPServer#listen spec
d15c2a2 Improve TCPServer#accept_nonblock specs
e7676c5 (un)pack sockaddr should land into JRuby-head soon
f579539 Guard failing expectation on Solaris
88700b1 Try to allow some names to be nil in Addrinfo#canonname spec
de38e1b Merge pull request #284 from etehtsea/tcp-server-sysaccept
ee34678 Add TCPServer#sysaccept spec
62b7184 Merge pull request #283 from etehtsea/imp-socket-option-spec
17b6275 Improve Socket::Option specs
cfc6222 Move inspect spec to other option specs
0bf9e67 Have a try at fixing Addrinfo.udp spec
77029fb Merge pull request #281 from etehtsea/add-socket-option-inspect
0b0c55c Cleanup Addrinfo specs
c3ee302 Add back socket Addrinfo specs
564b142 Fix the newly added-back library specs
f305537 Add library specs lost in the big merge back
e2d2069 Add Socket::Option#inspect spec
ebb208a Travis: allow ruby-head to fail since it's outdated
6a6153d Explicitly do not escape for testing -e
2704b5e Merge pull request #279 from etehtsea/connect-nonblock-exceptionless
0f33773 Add spec for exceptionless Socket#connect_nonblock
4f81c77 Follow CRuby r55728

git-subtree-dir: spec/ruby
git-subtree-split: b6baa6d10ca41f2322769ec9a6cc97635f9adc59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment