Skip to content

Commit 5ded019

Browse files
authored
Merge pull request #2552 from george-bit/fix-coverage-segfault-v2
Fix segfault when coverage runs with annotations disabled
2 parents 70c6a79 + ea51de7 commit 5ded019

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)