awesome-print / awesome_print

Pretty print your Ruby objects with style -- in full color and with proper indentation
http://github.com/michaeldv/awesome_print
MIT License
4.08k stars 454 forks source link

NameError: undefined local variable or method `dotfile' for #<AwesomePrint::Inspector #316

Closed dawncold closed 7 years ago

dawncold commented 7 years ago

I'm using logstash@5.5.0 which used awesome_print as a dependency, when I run logstash, it raised an error:

NameError: undefined local variable or method `dotfile' for #<AwesomePrint::Inspector:0x665967db>
  merge_custom_defaults! at /usr/share/logstash/vendor/bundle/jruby/1.9/gems/awesome_print-1.8.0/lib/awesome_print/inspector.rb:163
              initialize at /usr/share/logstash/vendor/bundle/jruby/1.9/gems/awesome_print-1.8.0/lib/awesome_print/inspector.rb:50
                      ai at /usr/share/logstash/vendor/bundle/jruby/1.9/gems/awesome_print-1.8.0/lib/awesome_print/core_ext/kernel.rb:9
          encode_default at /usr/share/logstash/vendor/bundle/jruby/1.9/gems/logstash-codec-rubydebug-3.0.3/lib/logstash/codecs/rubydebug.rb:39
                    call at org/jruby/RubyMethod.java:120
                  encode at /usr/share/logstash/vendor/bundle/jruby/1.9/gems/logstash-codec-rubydebug-3.0.3/lib/logstash/codecs/rubydebug.rb:35
            multi_encode at /usr/share/logstash/logstash-core/lib/logstash/codecs/base.rb:50
                    each at org/jruby/RubyArray.java:1613
            multi_encode at /usr/share/logstash/logstash-core/lib/logstash/codecs/base.rb:50
           multi_receive at /usr/share/logstash/logstash-core/lib/logstash/outputs/base.rb:90
           multi_receive at /usr/share/logstash/logstash-core/lib/logstash/output_delegator_strategies/single.rb:15
             synchronize at org/jruby/ext/thread/Mutex.java:149
           multi_receive at /usr/share/logstash/logstash-core/lib/logstash/output_delegator_strategies/single.rb:14
           multi_receive at /usr/share/logstash/logstash-core/lib/logstash/output_delegator.rb:47
            output_batch at /usr/share/logstash/logstash-core/lib/logstash/pipeline.rb:420
                    each at org/jruby/RubyHash.java:1342
            output_batch at /usr/share/logstash/logstash-core/lib/logstash/pipeline.rb:419
             worker_loop at /usr/share/logstash/logstash-core/lib/logstash/pipeline.rb:365
           start_workers at /usr/share/logstash/logstash-core/lib/logstash/pipeline.rb:330

I have checked my inspector.rb Line 163, it is https://github.com/awesome-print/awesome_print/blob/master/lib/awesome_print/inspector.rb#L164

I'm not familiar with ruby, if there is no .aprc file or it is not readable, #{dotfile} could be undefined ?

imajes commented 7 years ago

Yeah, you had another issue, which isn't super clear -- but the real problem is that the rescue error is trying to print a local var it can't reach (as you suspected). We can fix up the error message, but if you change that line to the following, and re-run your app, it should give clearer indication to what's going on:

$stderr.puts "Issue loading custom defaults. #{e.message}\n#{e.backtrace}"

dawncold commented 7 years ago

Thx, I found the root problem, dotfile path is concatenated by ENV['HOME'] and .aprc, but in my environment, ENV['HOME'] is nil, so the File.join raised an exception.

iamrohitbanga commented 7 years ago

Is this fixed? There can be situations when ENV['HOME'] is not set. Is it possible to guard against this?

imajes commented 7 years ago

Yeah, I just pushed up a commit that resolves this. Thanks!

iamrohitbanga commented 7 years ago

awesome_thanks!

ranvijayj commented 6 years ago

Error still there in 1.8.0

KelseyDH commented 5 years ago

Heads up for others encountering this problem with awesome_print on Rails: if you see this error message, it's because your app's ENV['HOME'] has disappeared. The easiest fix here is to fix that. In my case awesome_print failed with this error because we had rspec specs using stub_const to stub ENV values in tests:

# rspec test ...

  before do
    stub_const("ENV", {
        "API_ENABLED" => true
    })
  end

This stub quietly erased our app's environment variables -- including ENV['HOME'] set by rails, which awesome_print needed to pry into tests. We fixed it by stubbing our environment variables in a way that didn't modify the environmental variables set by rails.

The current unreleased master branch of awesome_print past 1.8.0 has code that prevents this error from breaking pry.. but when my HOME env was still missing it just gave me nicely formatted code... without any colour. I'm staying on 1.8.0 for now.