Skip to content

Segmentation fault with Ruby 3.4 + Rails 8.1 when coverage is enabled (negative lineno in class_eval) #2536

@george-bit

Description

@george-bit

Steps to reproduce

  1. Use Ruby 3.4.x, Rails 8.1.x, view_component 4.2.0
  2. Enable code coverage with SimpleCov (or any coverage tool)
  3. Run bin/rails test

The segfault occurs during component compilation when class_eval is called with a negative line number (-1).

Root cause: In v4.2.0, Template::File#initialize sets lineno = -1 for Rails 8.1+ with ERB handler:

# lib/view_component/template.rb lines 26-31
lineno =
  if Rails::VERSION::MAJOR >= 8 && Rails::VERSION::MINOR > 0 && details.handler == :erb
    - 1
  else
    0
  end

This is then passed to class_eval at line 119:

@component.class_eval <<~RUBY, @path, @lineno

When Ruby's coverage is enabled, negative line numbers in eval/class_eval trigger a segmentation fault. This is documented in https://bugs.ruby-lang.org/issues/19363.

Expected behavior

Tests run without crashing.

Actual behavior

Segmentation fault during eager loading/component compilation.

Backtrace:

  /vendor/bundle/ruby/3.4.0/gems/view_component-4.2.0/lib/view_component/template.rb:119: [BUG] Segmentation fault at 0x0000000000000024
  ruby 3.4.8 (2025-12-17 revision 995b59f666) +PRISM [x86_64-linux]

System configuration

Rails version: 8.1.2

Ruby version: 3.4.8

Gem version: 4.2.0


This works on 4.1.1 which always uses lineno: 0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions