Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
6 changes: 4 additions & 2 deletions lib/view_component/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions test/sandbox/test/inline_template_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading