Skip to content

Commit ea51de7

Browse files
george-bitclaude
andcommitted
Fix segfault when coverage runs with annotations disabled
The fix in v4.3.0 only avoided negative lineno when BOTH coverage was running AND annotations were enabled. This still caused segfaults when coverage was running but annotations were disabled (the common case in CI environments). The fix: always use lineno=0 when coverage is running, regardless of annotation settings. The annotation line stripping is still conditional on annotations being enabled (since there's nothing to strip otherwise). Before (v4.3.0): coverage + annotations → lineno=0 ✓ coverage + no annotations → lineno=-1 → SEGFAULT After: coverage + annotations → lineno=0, strip line ✓ coverage + no annotations → lineno=0 ✓ Added regression test for the annotations-disabled case. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 70c6a79 commit ea51de7

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

docs/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ nav_order: 6
1010

1111
## main
1212

13+
* Fix segfault when Ruby coverage is enabled but template annotations are disabled.
14+
15+
*George Holborn*
16+
1317
* Add `protocol` parameter to `with_request_url` test helper to enable testing with HTTPS protocol.
1418

1519
*Joel Hawksley*

lib/view_component/template.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ def initialize(component:, details:, path:)
3030
# annotation line from compiled source instead.
3131
lineno =
3232
if Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR > 0 && details.handler == :erb
33-
if coverage_running? && ActionView::Base.annotate_rendered_view_with_filenames
34-
@strip_annotation_line = true
33+
if coverage_running?
34+
# Can't use negative lineno with coverage (causes segfault on Linux).
35+
# Strip annotation line if enabled to preserve correct line numbers.
36+
@strip_annotation_line = ActionView::Base.annotate_rendered_view_with_filenames
3537
0
3638
else
3739
-1

test/sandbox/test/inline_template_test.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,25 @@ class InlineComponentDerivedFromComponentSupportingVariants < Level2Component
210210
end
211211
end
212212

213+
# Regression test for segfault when coverage is running but annotations are DISABLED.
214+
# This is the common case in CI environments.
215+
test "file-based templates compile without segfault when coverage is running and annotations disabled" do
216+
skip unless Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR > 0
217+
218+
without_template_annotations do
219+
with_coverage_running do
220+
# Force recompilation with coverage "enabled" but annotations disabled
221+
ViewComponent::CompileCache.cache.delete(ErbComponent)
222+
223+
# This would segfault in v4.3.0 because it only avoided -1 lineno
224+
# when annotations were enabled
225+
render_inline(ErbComponent.new(message: "Foo bar"))
226+
227+
assert_selector("div", text: "Foo bar")
228+
end
229+
end
230+
end
231+
213232
test "inline templates compile without segfault when coverage is running" do
214233
skip unless Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR > 0
215234

0 commit comments

Comments
 (0)