From fc20ca6b9c4073986b3114bee9e8b6ad488babbf Mon Sep 17 00:00:00 2001 From: Josh Adam Date: Thu, 12 Feb 2026 09:58:01 -0600 Subject: [PATCH 1/2] Fix coverage eval segfault when view annotations are disabled --- docs/CHANGELOG.md | 4 ++++ lib/view_component/template.rb | 4 ++-- test/sandbox/test/inline_template_test.rb | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index d143606be..207b02718 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -10,6 +10,10 @@ nav_order: 6 ## main +* Fix segfault when coverage of eval'd code is enabled with Rails 8.1 ERB templates and view annotations are disabled. + + *Josh Adam* + * 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..0aed78238 100644 --- a/lib/view_component/template.rb +++ b/lib/view_component/template.rb @@ -30,8 +30,8 @@ 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? + @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..0a8d1083f 100644 --- a/test/sandbox/test/inline_template_test.rb +++ b/test/sandbox/test/inline_template_test.rb @@ -234,7 +234,7 @@ class InlineComponentDerivedFromComponentSupportingVariants < Level2Component def with_coverage_running require "coverage" already_running = Coverage.running? - Coverage.start unless already_running + Coverage.start(lines: true, branches: true, eval: true) unless already_running yield ensure Coverage.result unless already_running From a3525252bfa06a7ef65bc87f413ec389df2eece4 Mon Sep 17 00:00:00 2001 From: Josh Adam Date: Thu, 12 Feb 2026 10:20:47 -0600 Subject: [PATCH 2/2] Guard negative eval lineno during coverage in compile_to_component --- lib/view_component/template.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/view_component/template.rb b/lib/view_component/template.rb index 0aed78238..f7c484dd6 100644 --- a/lib/view_component/template.rb +++ b/lib/view_component/template.rb @@ -30,8 +30,8 @@ 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? - @strip_annotation_line = ActionView::Base.annotate_rendered_view_with_filenames + if coverage_running? && ActionView::Base.annotate_rendered_view_with_filenames + @strip_annotation_line = true 0 else -1 @@ -137,8 +137,10 @@ def defined_on_self? def compile_to_component @component.silence_redefinition_of_method(call_method_name) + safe_lineno = coverage_running? && @lineno.to_i.negative? ? 1 : @lineno + # rubocop:disable Style/EvalWithLocation - @component.class_eval <<~RUBY, @path, @lineno + @component.class_eval <<~RUBY, @path, safe_lineno def #{call_method_name} #{compiled_source} end