Gary Weaver
2014-Feb-18 21:14 UTC
Why "Circular dependency detected while autoloading constant" vs. NameError after remove_const?
Notice how a class that is not available is a NameError and a constant
unloaded from ActiveSupport::Dependencies or via remove_const called on the
module is a RuntimeError about a circular dependency...
The class I unloaded has no references to or from anywhere else.
Just to show how a missing constant behaves normally:
Loading development environment (Rails 4.0.2)
2.1.0 :001 > Nothing::Nothing.to_s
NameError: uninitialized constant Nothing
from (irb):1
from /path/to/gemset/railties-4.0.2/lib/rails/commands/console.rb:90:in
`start'
from /path/to/gemset/railties-4.0.2/lib/rails/commands/console.rb:9:in
`start'
from /path/to/gemset/railties-4.0.2/lib/rails/commands.rb:62:in `<top
(required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Now, how a missing constant behaves after being undefined by Rails 4.0.2:
2.1.0 :002 > Object.const_defined? 'MyModule::MyClass'
=> false
2.1.0 :003 > MyModule::MyClass.to_s
=> "MyModule::MyClass"
2.1.0 :004 > Object.const_defined? 'MyModule::MyClass'
=> true
2.1.0 :005 > ActiveSupport::Dependencies.remove_constant MyModule::MyClass
=> MyModule::MyClass
2.1.0 :006 > Object.const_defined? 'MyModule::MyClass'
=> false
2.1.0 :007 > MyModule::MyClass.to_s
RuntimeError: Circular dependency detected while autoloading constant
MyModule::MyClass
from
/path/to/gemset/activesupport-4.0.2/lib/active_support/dependencies.rb:461:in
`load_missing_constant'
from
/path/to/gemset/activesupport-4.0.2/lib/active_support/dependencies.rb:184:in
`const_missing'
from (irb):7
from /path/to/gemset/railties-4.0.2/lib/rails/commands/console.rb:90:in
`start'
from /path/to/gemset/railties-4.0.2/lib/rails/commands/console.rb:9:in
`start'
from /path/to/gemset/railties-4.0.2/lib/rails/commands.rb:62:in `<top
(required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
2.1.0 :008 > load
"#{Rails.root}/app/an_autoloaded_dir/my_module/my_class.rb"
=> true
2.1.0 :009 > MyModule::MyClass.to_s
=> "MyModule::MyClass"
2.1.0 :010 > MyModule.send(:remove_const, :MyClass)
=> MyModule::MyClass
2.1.0 :011 > Object.const_defined? 'MyModule::MyClass'
=> false
2.1.0 :012 > MyModule::MyClass.to_s
RuntimeError: Circular dependency detected while autoloading constant
MyModule::MyClass
from
/path/to/gemset/activesupport-4.0.2/lib/active_support/dependencies.rb:461:in
`load_missing_constant'
from
/path/to/gemset/activesupport-4.0.2/lib/active_support/dependencies.rb:184:in
`const_missing'
from (irb):23
from /path/to/gemset/railties-4.0.2/lib/rails/commands/console.rb:90:in
`start'
from /path/to/gemset/railties-4.0.2/lib/rails/commands/console.rb:9:in
`start'
from /path/to/gemset/railties-4.0.2/lib/rails/commands.rb:62:in `<top
(required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
2.1.0 :013 > load
"#{Rails.root}/app/workflow/my_module/my_class.rb"
=> true
2.1.0 :015 > MyModule::MyClass.to_s
NameError: uninitialized constant MyModule
from (irb):26
from /path/to/gemset/railties-4.0.2/lib/rails/commands/console.rb:90:in
`start'
from /path/to/gemset/railties-4.0.2/lib/rails/commands/console.rb:9:in
`start'
from /path/to/gemset/railties-4.0.2/lib/rails/commands.rb:62:in `<top
(required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
2.1.0 :016 > MyModule::MyClass.to_s
=> "MyModule::MyClass"
I'm curious what the circular dependency would be there, since there are no
other references to the class, afaik.
Thanks!
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to rubyonrails-core+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/groups/opt_out.