From ea51de7f9b30fafe5a3772d88baa60de271f93d8 Mon Sep 17 00:00:00 2001 From: George Holborn Date: Thu, 12 Feb 2026 10:39:24 +0000 Subject: [PATCH] Fix segfault when coverage runs with annotations disabled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- docs/CHANGELOG.md | 4 ++++ lib/view_component/template.rb | 6 ++++-- test/sandbox/test/inline_template_test.rb | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index d143606be..4767b8efa 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -10,6 +10,10 @@ nav_order: 6 ## main +* Fix segfault when Ruby coverage is enabled but template annotations are disabled. + + *George Holborn* + * Add `protocol` parameter to `with_request_url` test helper to enable testing with HTTPS protocol. *Joel Hawksley* diff --git a/lib/view_component/template.rb b/lib/view_component/template.rb index afa1f8aec..ba15b1dc6 100644 --- a/lib/view_component/template.rb +++ b/lib/view_component/template.rb @@ -30,8 +30,10 @@ def initialize(component:, details:, path:) # annotation line from compiled source instead. lineno = if Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR > 0 && details.handler == :erb - if coverage_running? && ActionView::Base.annotate_rendered_view_with_filenames - @strip_annotation_line = true + if coverage_running? + # Can't use negative lineno with coverage (causes segfault on Linux). + # Strip annotation line if enabled to preserve correct line numbers. + @strip_annotation_line = ActionView::Base.annotate_rendered_view_with_filenames 0 else -1 diff --git a/test/sandbox/test/inline_template_test.rb b/test/sandbox/test/inline_template_test.rb index 60fa38adf..dcb5741ec 100644 --- a/test/sandbox/test/inline_template_test.rb +++ b/test/sandbox/test/inline_template_test.rb @@ -210,6 +210,25 @@ class InlineComponentDerivedFromComponentSupportingVariants < Level2Component end end + # Regression test for segfault when coverage is running but annotations are DISABLED. + # This is the common case in CI environments. + test "file-based templates compile without segfault when coverage is running and annotations disabled" do + skip unless Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR > 0 + + without_template_annotations do + with_coverage_running do + # Force recompilation with coverage "enabled" but annotations disabled + ViewComponent::CompileCache.cache.delete(ErbComponent) + + # This would segfault in v4.3.0 because it only avoided -1 lineno + # when annotations were enabled + render_inline(ErbComponent.new(message: "Foo bar")) + + assert_selector("div", text: "Foo bar") + end + end + end + test "inline templates compile without segfault when coverage is running" do skip unless Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR > 0