Fix line numbers and empty output for ERB templates Rails doesn't annotate - #2716
Merged
joelhawksley merged 1 commit intoSep 14, 2026
Conversation
Rails 8.1 added a newline to compiled ERB output (rails/rails#53731), and ViewComponent compensates with a lineno of -1 so backtraces point at the right line. That newline lives inside the `<!-- BEGIN ... -->` annotation, which Rails only prepends when `annotate_rendered_view_with_filenames` is enabled *and* the template's format is HTML. Compensating for every ERB template shifted backtraces by one line for non-HTML templates (`.text.erb`, `.css.erb`) and for every ERB template when annotations are disabled, such as in production. When coverage was running, the same mismatch made `strip_annotation_line?` drop everything up to the first `;` of a preamble that was never emitted, so non-HTML templates rendered empty. Move the annotation conditions into `erb_newline_compensation_needed?` on `Template` so every call site fires exactly when Rails emits the annotation. The negative lineno is still avoided under coverage (bugs.ruby-lang.org/issues/19363). `Template::Inline` had the same off-by-one, and decided the compensation when the component class was defined rather than when it compiled. It now shares the predicate and resolves it at compile time, matching `Template::File`. Also add the missing `ensure` to the `without_template_annotations` test helper, so an exception escaping the block no longer leaves annotations disabled for the rest of the process.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Rails 8.1 added a newline to compiled ERB output (rails/rails#53731), and #2502 compensates for it on every ERB template. But Rails only adds that newline when it emits the
<!-- BEGIN ... -->annotation:Line numbers in backtraces are off by one for
.text.erband.css.erbtemplates, and for all ERB templates when annotations are off, which is the default in production.Under coverage, non-HTML templates render empty. The annotation stripping from #2541 / #2552 checks the annotation setting but not the format, so it cuts into the template body.
Fix
Move both conditions into
erb_newline_compensation_needed?onTemplate.linenois still never negative while coverage is running.Template::Inlinehad the same off-by-one. It also picked its lineno when the class was defined instead of when it compiled, so it now uses the shared predicate at compile time likeTemplate::File.Notes
without_template_annotationswas missing anensure, so an exception escaping the block left annotations off for the rest of the process.Tested on Rails 8.1.3.1 / Ruby 4.0.6. I haven't run the full appraisal matrix. Rails < 8.1 is unaffected.