Forum: Ruby-core "I strongly discourage the use of autoload in any standard libraries" (Re: autoload will be dead)

Posted by rosenfeld (Rodrigo Rosenfeld Rosas) (Guest)
on 2013-02-12 14:40
(Received via mailing list)
Issue #5653 has been updated by rosenfeld (Rodrigo Rosenfeld Rosas).


+1 for removing autoload. It not only has troubles with thread safety, 
which I believe is fixable (although I confess I haven't thought much 
about it).

There is a more serious issue in my opinion that is unfixable with 
current specs.

For a while I've been experiencing a subtle bug in my Rails application 
that only happened on development mode (when autoload is enabled) and 
only for the first request. Recently I decided to understand what was 
happening and Xavier Noria showed me what was happening.

The problem is that I had some custom classes that relied on 
ActionView::Helpers::NumberHelper. Since all code in number_helper.rb 
was self-contained and it had no extra dependencies on ActionView, I 
just used it directly and my unit tests passed and my application always 
worked except for the first request when my custom class was used by the 
controller.

The problem is that once Rails is loaded it will declare an auto-load 
dependency on ActionView. Then when I "require 
'action_view/helpers/number_helper'" that file will be something like:

module ActionView
  module Helpers
    module NumberHelper

But since Helpers is set up to use autoload, once "module Helpers" is 
found, it will load "action_view/helpers.rb". And this file will then 
require "action_view/helpers/number_helper.rb" which causes a circular 
dependency problem.

Here is a complete Ruby-only code exemplifying the issue:

./test.rb:
autoload :A, 'a'
require 'a/b'

./lib/a.rb:
require 'a/b'

./lib/a/b.rb:
module A
  module B
  end
end

ruby -I lib test.rb

This kind of bug may be hard to track, so I agree with Matz that 
autoload should be dead.
----------------------------------------
Feature #5653: "I strongly discourage the use of autoload in any 
standard libraries" (Re: autoload will be dead)
https://bugs.ruby-lang.org/issues/5653#change-36172

Author: matz (Yukihiro Matsumoto)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: lib
Target version: 2.0.0


 Hi,

 Today, I talked with NaHi about enhancing const_missing to enable
 autoload-like feature with nested modules.  But autoload itself has
 fundamental flaw under multi-thread environment.  I should have remove
 autoload when I added threads to the language (threads came a few
 months after autoload).

 So I hereby declare the future deprecation of autoload.  Ruby will
 keep autoload for a while, since 2.0 should keep compatibility to 1.9.
 But you don't expect it will survive further future, e.g. 3.0.

 I strongly discourage the use of autoload in any standard libraries.

               matz.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.