There is a long dating bug affecting my application for a while but I never bothered to investigate it because it only affected the first request to my application and only under development environment (where autoload is used by Rails).
This is what I get:
rails s
=> Booting WEBrick
=> Rails 3.2.11 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2013-02-08 11:50:42] INFO WEBrick 1.3.1
[2013-02-08 11:50:42] INFO ruby 1.9.3 (2013-01-15) [x86_64-linux]
[2013-02-08 11:50:42] INFO WEBrick::HTTPServer#start: pid=1537 port=3000
Started POST "/search/store_state" for 127.0.0.1 at 2013-02-08 11:51:04 -0200
Ruby on Rails itself does not use dependencies.rb to load its code. It
is a regular Ruby library that uses requires and Kernel#autoload with
some added sugar. AS::Dependencies only covers application constant
autoloading.
The thing goes like this: When an application boots in any environment
action_view.rb is loaded. When that file is executed an autoload for
:Helpers is configured under ActionView. In a default setup,
helpers.rb is not yet loaded. That is, if you run
rails runner 1
helpers.rb is not loaded (at least in 3-2-stable, not that we are
explaining any contract, only load order execution to follow what
happens in your exception).
But if you force the evaluation of the constant as in your example above:
module ActionView
module Helpers
...
end
end
that autoload is triggered because the interpreter checks whether
"Helpers" is a constant defined in the module object stored in
ActionView. Therefore, helpers.rb is interpreted and sets in turn an
autoload for NumberHelper below AV::Helpers.
So, module Helpers in that snippet *reopens* a module object defined
via the autoload, rather than creating the module object. The
execution follows and the same happens with the "NumberHelper"
constant down below. The interpreter checks whether it belongs to the
module object stored in AV::Helpers. Since it is unknown and there is
an autoload for it it gets triggered, and loads... well the very
number_helper.rb whose execution we were in the middle of (not sure
this sentence is valid English :).
I suspect there is a circularity here that is showing up that way.
Would need to dig deeper to fully explain how this ends up in an
exception, maybe I'll do it tonight, but in the meantime here's some
context in case it helps.
As for the autoload + include, I don’t really know, maybe it is a fancy way to avoid writing a file path. Seems unnecessary to me at first glance. Maybe someone else from the team has a better justification?
But shouldn't helpers.rb require 'active_support/rails' then instead
of just ‘active_support/benchmarkable’?
Otherwise it should be documented that we're not supposed to require
specific parts of some libraries included in Rails… That way I’d
know (although surprised) that I’m not supposed to require
‘action_view/helpers/number_helper’ but simply ‘action_view/helpers’
(or just ‘action_view’?)
Anyway, thanks for your input. It helped documenting the bug in my
own code and stop the exception on the first request for dev
environment. But here is how my file looks like currently on latest
released Rails:
require 'action_view/helpers' # required due to bug in Rails:
require ‘action_view/helpers/number_helper’
number_helper depends on non declared module below. This is fixed
on Rails master and 3.2-stable branch so this can be removed
once we upgrade to Rails 4 or when a new Rails 3.2 is released:
require ‘active_support/core_ext/hash/keys’
require ‘active_support/core_ext/hash/reverse_merge’
I’ve fixed the last issues recently in the 3.2-stable branch but I’d
still like to be able to remove the first require.
Should I send a pull request to helpers.rb to just replace autoload
by regular requires (or require_relative, maybe for master)?
Thanks,
Rodrigo.
I don’t understand what you mean. In my unit tests involving
ParseFormatUtils for instance Rails is never loaded and my
application doesn’t even use ActiveRecord.
But my tests weren't complaining anyway because they don't autoload
ActionView/Helpers.
Also, even if I explicitly required as/rails it wouldn't fix the
situation for my case.
This is what's happening in a simplified way:
./test.rb:
autoload :A, 'a'
require 'a/b'
./lib/a.rb:
module A
autoload :B, 'a/b'
include B
end
./lib/a/b.rb:
module A
module B
end
end
ruby -I lib test.rb
This is enough to create the circular dependency error. Maybe this
is one of the reasons why Matz wants to remove autoload from Ruby…
Sorry but I don't have any suggestions right now that would make
require ‘a/b’ work if ‘a’ is set to be autoloaded and ‘a’ requires
‘b’ which depends on ‘a’…
Unless we create some 'action_view/helpers/all.rb' file. Would that
I am only explaining why autoload and concern have no explicit requires in that file. That was a tangential question you did not directly related to the exception.
I know it is tangential. I was just explaining why I don’t agree
with the current approach because non Rails code could rely only on
‘action_view/helpers’, don’t you agree? How are they supposed to
know that they should first require ‘active_support/rails’ or any
other entry point?
I don't think it is fine to just assume that ActionView classes only
make sense in within the Rails context. This is an unsafe assumption
that I don’t agree with.
At least, now that I know that this behavior is intentional in Rails
I documented myself in my own class so that other developers could
also understand the reason I’m depending on full ActionView when I
really depend only on NumberHelper:
require 'bigdecimal'
# In Rails, if you need action_view/helpers/number_helper you have
to require ‘action_view’,
# then 'action_view/helpers and finally
‘action_view/helpers/number_helper’. Pretty stupid but we don’t
control
# Rails. Since ActionView will load all components, we don’t need to
require the other files, just ‘action_view’:
require ‘action_view’
#require ‘action_view/helpers/number_helper’
I don’t remember if this is documented somewhere but I don’t think we have a guide to “How to use Rails pieces outside Rails” to put this kind of information.