tj / commander

The complete solution for Ruby command-line executables
http://visionmedia.github.com/commander
MIT License
1.09k stars 64 forks source link

Loading commander gem in a Rails 4 environment triggers: "DEPRECATION WARNING: RAILS_CACHE is deprecated!" #58

Closed mattbrictson closed 11 years ago

mattbrictson commented 11 years ago

If I do require "commander" inside a Rails 4 app, I get this message:

DEPRECATION WARNING: RAILS_CACHE is deprecated! Use ::Rails.cache instead.

This seems to be a side-effect of how activesupport implements its deprecation mechanism. Commander goes through all global constants and calls respond_to? on them, triggering the deprecation warning for the RAILS_CACHE constant.

I'm not sure if this is a commander or an activesupport issue.

Here's the full trace in IRB:

$ rails c -s
Loading development environment in sandbox (Rails 4.0.0)
Any modifications you make will be rolled back on exit
>> ActiveSupport::Deprecation.debug = true
=> true
>> require 'commander'
DEPRECATION WARNING: RAILS_CACHE is deprecated! Use ::Rails.cache instead. (called from irb_binding at (irb):3)
/Users/mbrictson/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/commander-4.1.4/lib/commander/user_interaction.rb:330:in `block in <module:AskForClass>'
  /Users/mbrictson/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/commander-4.1.4/lib/commander/user_interaction.rb:329:in `select'
  /Users/mbrictson/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/commander-4.1.4/lib/commander/user_interaction.rb:329:in `<module:AskForClass>'
  /Users/mbrictson/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/commander-4.1.4/lib/commander/user_interaction.rb:322:in `<module:UI>'
  /Users/mbrictson/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/commander-4.1.4/lib/commander/user_interaction.rb:13:in `<module:Commander>'
  /Users/mbrictson/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/commander-4.1.4/lib/commander/user_interaction.rb:4:in `<top (required)>'
  /Users/mbrictson/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
  /Users/mbrictson/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `block in require'
  /Users/mbrictson/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
  /Users/mbrictson/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
  /Users/mbrictson/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/commander-4.1.4/lib/commander.rb:27:in `<top (required)>'
  /Users/mbrictson/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
  /Users/mbrictson/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `block in require'
  /Users/mbrictson/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
  /Users/mbrictson/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
  (irb):3:in `irb_binding'
ggilder commented 11 years ago

Hmm. It doesn't seem like calling introspection methods like is_a? and respond_to? should trigger the deprecation warning, but maybe it's just applied to all methods.

mattbrictson commented 11 years ago

Yes, the deprecation warning seems to be triggered on all methods except inspect. For example, respond_to?:

>> ::RAILS_CACHE.respond_to?(:parse)
DEPRECATION WARNING: RAILS_CACHE is deprecated! Use ::Rails.cache instead. (called from irb_binding at (irb):1)
=> false

Here is the basic implementation from activesupport:

class DeprecationProxy #:nodoc:
  def self.new(*args, &block)
    object = args.first

    return object unless object
    super
  end

  instance_methods.each { |m| undef_method m unless m =~ /^__|^object_id$/ }

  # Don't give a deprecation warning on inspect since test/unit and error
  # logs rely on it for diagnostics.
  def inspect
    target.inspect
  end

  private
    def method_missing(called, *args, &block)
      warn caller, called, args
      target.__send__(called, *args, &block)
    end
end

Should commander work around this, or do you think I should raise this as an issue with activesupport?

ggilder commented 11 years ago

@mbrictson could you try this commit and confirm if it fixes the problem? It works for me. https://github.com/visionmedia/commander/commit/b475b6e6cf44400ed48808d42160833b850b5e22

mattbrictson commented 11 years ago

Yes, that fixes the deprecation warning for me. Thanks!

ggilder commented 11 years ago

Cool, I've just released 4.1.5 with this fix incorporated.