Fix segfault when Ruby coverage is enabled with Rails 8.1 ERB templates #2541
+14
−6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fixes segmentation fault when running tests with code coverage enabled (e.g., SimpleCov) on Rails 8.1+ with Ruby 3.4+.
Problem
v4.2.0 introduced negative
linenovalues (-1) inclass_evalto fix incorrect line numbers in stack traces for Rails 8.1 ERB templates. However, this causes a segmentation fault when Ruby'sCoveragemodule is enabled.Root Cause
Rails 8.1 change: Rails added annotation comments to compiled ERB output (rails/rails#53731), which added an extra line and caused stack trace line numbers to be off by 1.
v4.2.0 fix: Used
lineno = -1inclass_evalto compensate for the extra line.Ruby bug: Negative line numbers in
eval/class_evalcause segmentation faults when Ruby's Coverage module is running (bugs.ruby-lang.org/issues/19363). This bug exists in Ruby 3.4 and has been known since 2022.Result: Rails 8.1 + Ruby 3.4 + view_component 4.2.0 + SimpleCov = crash.
Solution
Detect whether coverage is running via
Coverage.running?and adapt:Why not 0? Testing showed
lineno=0also causes the segfault.1is the minimum safe value.Trade-off
This is the best possible fix given the Ruby bug. Most users (development, production) get correct line numbers. Only CI/coverage users see slightly off line numbers—but their tests actually run instead of crashing.
Fixes #2540