Skip to content

Add support for oneshot_lines coverage mode - #1154

Merged
bf4 merged 1 commit into
simplecov-ruby:mainfrom
fatkodima:oneshot_lines-coverage-mode
Feb 17, 2026
Merged

bf4 merged 1 commit into
simplecov-ruby:mainfrom
fatkodima:oneshot_lines-coverage-mode

Conversation

@fatkodima

Copy link
Copy Markdown
Contributor

Closes #710.

https://bugs.ruby-lang.org/issues/15022

We use coverage on CI and I noticed that it significantly slows down the tests. After experimenting with oneshot_lines coverage mode, the runtime dropped a lot (it was 15 minutes before and now is 10 minutes).

Comment thread lib/simplecov.rb
def process_coverage_result
adapt_coverage_result
remove_useless_results
adapt_coverage_result

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this PR, Coverage.stub_line is used, which is a pretty costly method. So I swapped lines here to filter unneeded files first before doing the actual work.

@bf4

bf4 commented Feb 11, 2026

Copy link
Copy Markdown
Collaborator

Looking good to merge
but I haven't been an active maintainer for a while..
so sitting on it a bit

@bf4
bf4 merged commit 522dc7d into simplecov-ruby:main Feb 17, 2026
10 checks passed
@bf4

bf4 commented Feb 17, 2026

Copy link
Copy Markdown
Collaborator

Merged. Looks good to me.

@fatkodima
fatkodima deleted the oneshot_lines-coverage-mode branch February 17, 2026 18:06
@nflorentin

nflorentin commented Feb 25, 2026 •

Copy link
Copy Markdown

@fatkodima

I tried the feature but encountered this issue :

/Users/nicolas/.rvm/rubies/ruby-3.4.5/lib/ruby/3.4.0/coverage.rb:6:in 'RubyVM::InstructionSequence.compile_file': --> /Users/nicolas/www/rails_apps/ubikly/config/secrets.yml

unexpected ':', expecting end-of-input
unexpected ':', ignoring it
unexpected '&', ignoring it
unexpected local variable or method, expecting end-of-input
unexpected '<', ignoring it
unexpected constant, expecting end-of-input
unexpected '>'; expected an expression after the operator
unexpected '='; target cannot be written
unexpected string literal, expecting end-of-input
unexpected end-of-input; expected an expression after the operator
unexpected end-of-input, assuming it is closing the parent top level context

>  1  development: &default
 [content of my config file secrets.yml]
 
/Users/nicolas/www/rails_apps/ubikly/config/secrets.yml:1: syntax error, unexpected ':', expecting end-of-input (SyntaxError)
development: &default
           ^

	from /Users/nicolas/.rvm/rubies/ruby-3.4.5/lib/ruby/3.4.0/coverage.rb:6:in 'Coverage.line_stub'
	from /Users/nicolas/.rvm/gems/ruby-3.4.5/bundler/gems/simplecov-522dc7d3aee1/lib/simplecov/result_adapter.rb:35:in 'SimpleCov::ResultAdapter#adapt_oneshot_lines_if_needed'
	from /Users/nicolas/.rvm/gems/ruby-3.4.5/bundler/gems/simplecov-522dc7d3aee1/lib/simplecov/result_adapter.rb:25:in 'block in SimpleCov::ResultAdapter#adapt'
	from /Users/nicolas/.rvm/gems/ruby-3.4.5/bundler/gems/simplecov-522dc7d3aee1/lib/simplecov/result_adapter.rb:21:in 'Hash#each'
	from /Users/nicolas/.rvm/gems/ruby-3.4.5/bundler/gems/simplecov-522dc7d3aee1/lib/simplecov/result_adapter.rb:21:in 'Enumerable#each_with_object'
	from /Users/nicolas/.rvm/gems/ruby-3.4.5/bundler/gems/simplecov-522dc7d3aee1/lib/simplecov/result_adapter.rb:21:in 'SimpleCov::ResultAdapter#adapt'
	from /Users/nicolas/.rvm/gems/ruby-3.4.5/bundler/gems/simplecov-522dc7d3aee1/lib/simplecov/result_adapter.rb:15:in 'SimpleCov::ResultAdapter.call'
	from /Users/nicolas/.rvm/gems/ruby-3.4.5/bundler/gems/simplecov-522dc7d3aee1/lib/simplecov.rb:407:in 'SimpleCov.adapt_coverage_result'
	from /Users/nicolas/.rvm/gems/ruby-3.4.5/bundler/gems/simplecov-522dc7d3aee1/lib/simplecov.rb:397:in 'SimpleCov.process_coverage_result'
	from /Users/nicolas/.rvm/gems/ruby-3.4.5/bundler/gems/simplecov-522dc7d3aee1/lib/simplecov.rb:105:in 'SimpleCov.result'
	from /Users/nicolas/.rvm/gems/ruby-3.4.5/bundler/gems/simplecov-522dc7d3aee1/lib/simplecov/configuration.rb:197:in 'block in SimpleCov::Configuration#at_exit'
	from /Users/nicolas/.rvm/gems/ruby-3.4.5/bundler/gems/simplecov-522dc7d3aee1/lib/simplecov.rb:189:in 'SimpleCov.run_exit_tasks!'
	from /Users/nicolas/.rvm/gems/ruby-3.4.5/bundler/gems/simplecov-522dc7d3aee1/lib/simplecov.rb:179:in 'SimpleCov.at_exit_behavior'
	from /Users/nicolas/.rvm/gems/ruby-3.4.5/bundler/gems/simplecov-522dc7d3aee1/lib/minitest/simplecov_plugin.rb:11:in 'block in Minitest.plugin_simplecov_init'
	from /Users/nicolas/.rvm/gems/ruby-3.4.5/gems/minitest-5.27.0/lib/minitest.rb:81:in 'Array#reverse_each'
	from /Users/nicolas/.rvm/gems/ruby-3.4.5/gems/minitest-5.27.0/lib/minitest.rb:81:in 'block (2 levels) in Minitest.autorun'

I monkey patch Simplecov to make it worked:

  module SimpleCov
    class ResultAdapter
      private

      alias_method :adapt_oneshot_lines_if_needed_original, :adapt_oneshot_lines_if_needed

      def adapt_oneshot_lines_if_needed(file_name, cover_statistic)
        adapt_oneshot_lines_if_needed_original(file_name, cover_statistic)
      rescue SyntaxError
        return cover_statistic unless cover_statistic.key?(:oneshot_lines)

        oneshot_lines = cover_statistic.delete(:oneshot_lines)
        line_stub = []

        oneshot_lines.each do |covered_line|
          line_stub[covered_line - 1] = 1
        end

        cover_statistic[:lines] = line_stub
      end
    end

Also some lines are missing and were not with the default mode :
Capture d’écran 2026-02-25 à 15 12 32
Capture d’écran 2026-02-25 à 15 12 26

Also, it seems that this mode is incompatible with enable_coverage_for_eval.
When both are on, views have always 100% coverage no matters the tests.

@fatkodima

Copy link
Copy Markdown
Contributor Author

Why it produces a syntax error?

@nflorentin

Copy link
Copy Markdown

It seems that simplecov is evaluating .yml files in config folder like as it they were .rb files.

@fatkodima

Copy link
Copy Markdown
Contributor Author

Had it worked before with other modes?
If yes, can you create a sample app that reproduces the issue and give a link to it?

@nflorentin

Copy link
Copy Markdown

My suite is all green without any error when I removed the 2 lines :

enable_coverage :oneshot_line
primary_coverage :oneshot_line

I will try to find time to make a repro app, probably next week.

@nflorentin

Copy link
Copy Markdown

Curiously, with the monkey patch, it worked but it does not run faster.

My suite has 7168 runs, 42714 assertions and runs in about 55 seconds on mac m4 pro (12 cpu).
It runs in the same time with this mode.

@fatkodima

Copy link
Copy Markdown
Contributor Author

I noticed that the coverage for some projects takes a lot of time, but for some it is not. 55 seconds is already very fast.

Try to see if there is any difference between the modes in your app on mac m4. Disable the simplecov completely and add a code somewhere in the initializer and test the difference between

require "coverage"
Coverage.start(lines: true)
Coverage.result

vs

require "coverage"
Coverage.start(oneshot_lines: true)
Coverage.result

@nflorentin

Copy link
Copy Markdown

No difference nor bug with the snippets you gave.

I think I found the problem. The bugs occurs when combining enable_coverage_for_eval and :oneshot_line mode.

I use enable_coverage_for_eval to have coverage on my views.

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

Successfully merging this pull request may close these issues.

Feature request: Add Ruby 2.6 Oneshot ability

3 participants