Skip to content

Coverage reports wrong number of lines #1196

Closed

Description

@donv

Given the file my_model.rb:

class MyModel
  class_eval <<-EOT, __FILE__, __LINE__
    def a
      foo
    end

    def b
      bar
    end
  EOT
end

and the following code to measure coverage in coverage_example.rb:

file = File.expand_path('my_model.rb', File.dirname(__FILE__))
puts File.read(file).split("\n").size
require 'coverage'
Coverage.start
require file.chomp('.rb')
puts Coverage.result[file].size

Run with jruby 1.7.6 and debug enabled, the output is:

$ jruby --debug coverage_example.rb 
11
15
$ 

I would expect the numbers to be the same, possibly the last to be smaller than the first.

The discrepancy trigger very noisy warning messages when measuring coverage, for example using simplecov.

What could cause the difference?

Activity

  1. enebo commented on Mar 21, 2016

    @enebo
    Member

    Upon reviewing this one I can see a major issue. MRI does not store eval parses in coverage at all? So the reported issue is more bizarre than just being the wrong size. The eval happens to have the exact same name as the file so it gets its coverage data stored in the hash. The original file is left out. If I remove the FILE from the eval then JRuby has 2 entries with the original file containing the proper data and an eval entry with the data you reported. I think I just need to remove any evals from recording coverage info?

  2. enebo commented on Mar 21, 2016

    @enebo
    Member

    Ah. So here is a clue simplecov-ruby/simplecov#38. So coverage only tracks require. I just checked and this also includes load. So I think I can just check to see if parse is eval and if so then it means it must have came from load/require.

  3. enebo commented on Mar 21, 2016

    @enebo
    Member

    Yucky wrinkle in fixing this. We enable a global hook for all LINE EVENTS whereas MRI will instrument live events into the source only for iseqs (which is IRScopes for us). Since FILE is used as the eval's name it ends up sending it's coverage events info to the coverage array and we end up mixing the eval and regular file coverage data up.

    So the fix is to remove the global COVERAGE_HOOK and to replace it with instrumented code.

  4. bf4 commented on Mar 22, 2016

    @bf4

    @enebo Thanks for looking into this :)

    SimpleCov now has https://github.com/colszowka/simplecov/blob/cffb463890eae605a9b3f0b4437d1dbbe2271c42/lib/simplecov.rb#L5-L17

    if defined?(JRUBY_VERSION) && defined?(JRuby)
    
      # @see https://github.com/jruby/jruby/issues/1196
      # @see https://github.com/metricfu/metric_fu/pull/226
      # @see https://github.com/colszowka/simplecov/issues/420
      # @see https://github.com/colszowka/simplecov/issues/86
      # @see https://jira.codehaus.org/browse/JRUBY-6106
    
      unless JRuby.runtime.debug?
        warn 'Coverage may be inaccurate; set "cli.debug=true" ("-Xcli.debug=true") in your .jrubyrc or' \
          ' do JRUBY_OPTS="-d"'
      end

    and simplecov-ruby/simplecov#430

  5. enebo commented on Mar 22, 2016

    @enebo
    Member

    @bf4 I think once I address not generating events for eval code along with a local fix I made to not mutilate the coverage array locally then coverage will just be working correctly. My last comment shows it is not quite trivial but I think doable.

  6. added 2 commits that reference this issue on Mar 23, 2016
  7. added this to the JRuby 9.1.0.0 milestone on Mar 23, 2016
  8. enebo commented on Mar 23, 2016

    @enebo
    Member

    So this will only be fixed from JRuby 9k and not JRuby 1.7 because we have a completely different runtime and the effort to fix this on 1.7 will be more than it was for 9k. If there is enough pushback someone can open a new issue against 1.7.

  9. PragTob commented on Jan 25, 2017

    @PragTob

    @enebo do I understand your comment correctly to mean that JRuby 9k+ shouldn't the comment of warning? It was attached to the 9.1 milestone so should it rather not be applicable to JRuby 9.1+?

    As a reference I reran the examples of @donv :

    tobi@speedy ~/dev/coverage_sample $ asdf local ruby jruby-9.1.7.0
    tobi@speedy ~/dev/coverage_sample $ jruby runner.rb 
    11
    runner.rb:4: warning: tracing (e.g. set_trace_func) will not capture all events without --debug flag
    11
    tobi@speedy ~/dev/coverage_sample $ jruby --debug runner.rb 
    11
    11
    tobi@speedy ~/dev/coverage_sample $ asdf local ruby jruby-9.0.5.0
    tobi@speedy ~/dev/coverage_sample $ jruby runner.rb 
    11
    runner.rb:4: warning: tracing (e.g. set_trace_func) will not capture all events without --debug flag
    7
    tobi@speedy ~/dev/coverage_sample $ jruby --debug runner.rb 
    11
    7
    tobi@speedy ~/dev/coverage_sample $ 
    
    

    9.0.5.0 looks off but off in a different manner (albeit both with --debug and without)

    edit: also just discovered another failure in the simplecov test suite that happens only on 9.0.5.0 but not 9.1 - so I guess the fixes really were 9.1, thanks for taking the time Tom!

  10. added a commit that references this issue on Jan 25, 2017
  11. enebo commented on Jan 26, 2017

    @enebo
    Member

    @PragTob so do I need to comment on anything now? I did not quite get your original question and then I see the edit.

  12. PragTob commented on Jan 26, 2017

    @PragTob

    @enebo question was more to confirm that coverage is only expected to work fully in 9.1+ to which I found out the answer is most likely yes so that's the official support target now. Thanks! :)

  13. PragTob commented on Feb 4, 2017

    @PragTob

    @enebo I remembered my question! Right now simplecov still propmpts users to enable the debug mode, is this still necessary or should we be good without debug mode?

  14. enebo commented on Feb 9, 2017

    @enebo
    Member

    @PragTob sorry for the long delay in replying but I was travelling. We still need --debug because we do not emit trace instructions into our emitted IR instructions without it. In the future we can invalidate ALL methods once tracing is called to re-compile all methods to emit a version with these instrs but it is not high in the priority list...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions