Skip to content

Fix coverage(eval) segfault when annotations are disabled#2553

Closed
joshsadam wants to merge 2 commits intoViewComponent:mainfrom
joshsadam:fix-coverage-eval-negative-lineno
Closed

Fix coverage(eval) segfault when annotations are disabled#2553
joshsadam wants to merge 2 commits intoViewComponent:mainfrom
joshsadam:fix-coverage-eval-negative-lineno

Conversation

@joshsadam
Copy link

What are you trying to accomplish?

Prevent a segfault when running with Ruby coverage of eval'd code enabled (for example SimpleCov.enable_coverage_for_eval) on Rails 8.1 ERB templates with ActionView::Base.annotate_rendered_view_with_filenames = false.

The current logic can still pass lineno = -1 to class_eval in this case, which triggers a Ruby crash in branch coverage instrumentation.

What approach did you choose and why?

Two targeted changes:

  1. In Template::File, when coverage_running? is true, always avoid negative line numbers by using lineno = 0.
  2. Keep annotation stripping conditional on ActionView::Base.annotate_rendered_view_with_filenames so stack trace behavior remains unchanged where annotations are enabled.

This preserves existing behavior outside of coverage mode while removing the crash path.

I also updated the regression helper to start coverage with eval: true:

Coverage.start(lines: true, branches: true, eval: true)

Without eval: true, the test does not exercise the crashing path.

Anything you want to highlight for special attention from reviewers?

Context:

Minimal reproducer (outside ViewComponent):

require "coverage"
Coverage.start(lines: true, branches: true, eval: true)
klass = Class.new
klass.class_eval("def x(v); v ? 1 : 2; end", "fake_template.erb", -1)

On Ruby 3.4 this segfaults with a backtrace through add_trace_branch_coverage.

@joshsadam joshsadam marked this pull request as draft February 12, 2026 16:16
@joshsadam
Copy link
Author

This is not working the way I thought. I will get back to you on this fix.

@joelhawksley
Copy link
Member

@joshsadam thanks for digging into it. I just landed #2552 to main, can you rebase this branch and see if that fix worked for you?

@joshsadam
Copy link
Author

That's great, thanks @joelhawksley I pinned your main branch in our gemfile, and running the CI tests now. I will let you know how it goes.

@joshsadam
Copy link
Author

@joelhawksley our tests pass on CI now after pinning your main version. Can you do a minor release with this update?

@joshsadam joshsadam closed this Feb 12, 2026
@joelhawksley
Copy link
Member

@joshsadam yes, I'll be getting a release out today. Thank you for your help validating!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants