Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined method `visibility' for nil:NilClass #2

Open
cpence opened this issue Sep 28, 2011 · 10 comments
Open

undefined method `visibility' for nil:NilClass #2

cpence opened this issue Sep 28, 2011 · 10 comments

Comments

@cpence
Copy link

cpence commented Sep 28, 2011

I'm having a strange error when running yardstick on my code base. It's been running fine for quite a while now, and has been really helpful in getting my documentation quality up, so I'd love to figure out what's gone wrong. Unfortunately, pecking through the code I can't seem to make heads or tails of what's gone wrong.

Running Ruby 1.9.2 (installed via RVM). The project is available here, and the current Git HEAD is precisely the set of code that's causing this error, if it would help to run things yourself. I can duplicate the error on two machines (with Ruby 1.9.2 and Yardstick 0.4.0), so I don't think it's a problem with my machine or Ruby setup.

Let me know if there's anything else I can do to help!

Backtrace follows:

[cpence@bertrand ~/Development/rletters/master]$ yardstick 'app/**/*.rb' 'lib/**/*.rb'
/Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/lib/yardstick/method.rb:94:in `visibility': undefined method `visibility' for nil:NilClass (NoMethodError)
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/lib/yardstick/method.rb:36:in `block in <module:Method>'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/lib/yardstick/measurement.rb:143:in `instance_eval'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/lib/yardstick/measurement.rb:143:in `block in measure'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/lib/yardstick/measurement.rb:142:in `catch'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/lib/yardstick/measurement.rb:142:in `measure'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/lib/yardstick/measurement.rb:36:in `initialize'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/lib/yardstick/rule.rb:31:in `new'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/lib/yardstick/rule.rb:31:in `measure'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/lib/yardstick/rule_set.rb:14:in `block in measure'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/lib/yardstick/ordered_set.rb:64:in `each'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/lib/yardstick/ordered_set.rb:64:in `each'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/lib/yardstick/rule_set.rb:14:in `map'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/lib/yardstick/rule_set.rb:14:in `measure'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/lib/yardstick/measurable.rb:74:in `measure'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/lib/yardstick/processor.rb:41:in `block in measurements'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/lib/yardstick/processor.rb:40:in `each'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/lib/yardstick/processor.rb:40:in `measurements'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/lib/yardstick/processor.rb:15:in `process_path'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/lib/yardstick.rb:23:in `measure'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/lib/yardstick/cli.rb:19:in `run'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/gems/yardstick-0.4.0/bin/yardstick:7:in `<top (required)>'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/bin/yardstick:21:in `load'
    from /Users/cpence/.rvm/gems/ruby-1.9.2-p290/bin/yardstick:21:in `<main>'
@dkubb
Copy link
Owner

dkubb commented Sep 28, 2011

Just from the stacktrace it looks like the error is happening within the Yardstick::Method module, which is mixed into YARD::CodeObjects::MethodObject. It uses the object.visibility from that MethodObject, and it looks like object is nil.

I'll try running the code against your repo later on. In the meantime, you could try running it against parts of the source code until you narrow down which file the problem is happening with. The most likely problem is that YARD has a bug in it's parser and is being tripped up by something specific in one place within the source.

@cpence
Copy link
Author

cpence commented Sep 29, 2011

Should have thought of that. It's the app/models/document.rb file. In particular, it seems to be the following snippet:

  # Faceted browsing information that was returned by the last search
  # 
  # For the purposes of faceted browsing, the Solr server (as configured by
  # default in RLetters) returns the number of items within the current search
  # with each author, journal, or publication decade.
  #
  # @api public
  # @return [Hash] facets returned by the last search, +nil+ if none.
  #   The hash contains the following keys:
  #     Document.facets[:author]["Particular Author"] = Integer
  #     Document.facets[:journal]["Particular Journal"] = Integer
  #     Document.facets[:year]["1980"] = Integer
  #   The +:year+ facet is handled specially: the value for "1980" is the
  #   facet count for the entire decade 1980-1989.
  # 
  # @example Get the number of documents in the last search published by W. Shatner
  #   shatner_docs = Document.facets[:author]["W. Shatner"]
  cattr_reader :facets

  # Number of documents returned by the last search
  #
  # Since the search results are almost always limited by the per-page count,
  # this variable returns the full number of documents that were returned by
  # the last search.
  #
  # @return [Integer] number of documents in the last search
  cattr_reader :num_results

Remove the docstring on only one of these, and it still crashes. Remove both docstrings, and it works. Can you spot anything obviously crazy here?

@dkubb
Copy link
Owner

dkubb commented Sep 29, 2011

Hmm. Nothing obvious stands out for me. Sometimes when there are parsing errors (not just YARD, but in general I've noticed) the error could actually be above wherever the problem is manifesting. Like for example, maybe there's something that put the parser in an invalid state above this code.

What I might start by doing is a kind of binary search; removing all of the docs from the halfway mark down, see if the problem stays or goes away, and then keep dividing the doc up until you zero in on it.

@cpence
Copy link
Author

cpence commented Oct 2, 2011

Okay, I've pruned it down to a minimal example and posted it here.

It seems that YARD/Yardstick doesn't like cattr_reader. If I change the cattr_reader in both of these to attr_reader, everything parses just fine. Is there something I need to do to add support for Rails's cattr_reader to the YARD parser or to Yardstick?

@dkubb
Copy link
Owner

dkubb commented Oct 2, 2011

@cpence: it wouldn't be anything to do with Yardstick, but YARD itself. What you want to do is write a YARD handler that identifies the cattr_reader call and relates that to the method it defines. Its actually not that difficult, it's probably around 10-15 loc in the core, and then a bit of supporting mostly boilerplate code to package it up into a gem.

Here's two examples of YARD handlers, but you may be able to find more through github/google search:

https://github.com/postmodern/yard-dm-is-predefined
https://github.com/postmodern/yard-dm

By itself YARD has no knowledge of DSL methods like cattr_reader and the method definition side effect. The handler allows you treat those DSL methods as if they were inline method definitions, eg:

https://github.com/postmodern/yard-dm/blob/master/lib/yard-dm/belongs_to_handler.rb

1 similar comment
@dkubb
Copy link
Owner

dkubb commented Oct 2, 2011

@cpence: it wouldn't be anything to do with Yardstick, but YARD itself. What you want to do is write a YARD handler that identifies the cattr_reader call and relates that to the method it defines. Its actually not that difficult, it's probably around 10-15 loc in the core, and then a bit of supporting mostly boilerplate code to package it up into a gem.

Here's two examples of YARD handlers, but you may be able to find more through github/google search:

https://github.com/postmodern/yard-dm-is-predefined
https://github.com/postmodern/yard-dm

By itself YARD has no knowledge of DSL methods like cattr_reader and the method definition side effect. The handler allows you treat those DSL methods as if they were inline method definitions, eg:

https://github.com/postmodern/yard-dm/blob/master/lib/yard-dm/belongs_to_handler.rb

@dkubb
Copy link
Owner

dkubb commented Oct 2, 2011

I'm going to keep this ticket open though because it looks like I need to handle cases where YARD is parsing what looks to be like a method's documentation, but it doesn't know which method to associate the docs with.

@cpence
Copy link
Author

cpence commented Oct 2, 2011

Awesome, thanks for the help on this. And agreed that there should be some way for Yardstick to recover when it can -- or at least print a pretty error message.

@cpence
Copy link
Author

cpence commented Oct 2, 2011

A related point that does pertain only to Yardstick: now that I have a functioning DSL handler for cattr_*, how do I get Yardstick to load it? Is there a way (similar to YARD's -e option) to specify an extension to load, since it's not packaged as a gem?

@RichMorin
Copy link

I'm getting the same error from Yardstick (0.0.1), with no complaint from YARD (0.7.4).

Here's the code: http://pastie.org/3087569

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants