Skip to content

Engine: Inline comments on <% end %> on output blocks produce invalid Ruby#1363

Merged
marcoroth merged 7 commits intomarcoroth:mainfrom
joelhawksley:fix-inline-comment
Mar 11, 2026
Merged

Engine: Inline comments on <% end %> on output blocks produce invalid Ruby#1363
marcoroth merged 7 commits intomarcoroth:mainfrom
joelhawksley:fix-inline-comment

Conversation

@joelhawksley
Copy link
Contributor

@joelhawksley joelhawksley commented Mar 11, 2026

Version

herb 0.8.10 (commit 3a36f815b05b)

Summary

When <% end # comment %> closes a block opened inside a <%= %> output tag, Herb appends the closing )) after the comment text in compiled Ruby. Since # starts a Ruby comment, the )) become part of the comment, leaving __herb.h(( unclosed.

Minimal reproduction

<%= render Foo.new do %>
  hello
<% end # comment %>

Actual compiled Ruby (broken)

_buf << __herb.h((render Foo.new do; _buf << '  hello
'.freeze; end # comment ))

The )) after # comment is commented out — parens never close.

Expected

The )) should be placed before the comment, or the comment should be stripped:

_buf << __herb.h((render Foo.new do; _buf << '  hello
'.freeze; end )) # comment

Prism parse errors

- unexpected end-of-input, assuming it is closing the parent top level context (line 4)
- expected a matching `)` (line 5)
- unexpected end-of-input; expected a `)` to close the arguments (line 4)

Scope

  • Affected: <% end # ... %> closing a block started in <%= %> (where Herb wraps in __herb.h((...)))
  • Not affected: <% end # ... %> inside non-output <% %> tags — no wrapping parens, so comment is harmless

Workaround

Remove inline comments from end tags that close output-tag blocks.

@marcoroth marcoroth changed the title Inline comments on end inside <%= %> output blocks produce invalid Ruby Engine: Inline comments on <% end %> on output blocks produce invalid Ruby Mar 11, 2026
@joelhawksley joelhawksley marked this pull request as ready for review March 11, 2026 20:03
joelhawksley and others added 3 commits March 11, 2026 14:36
Co-authored-by: Marco Roth <marco.roth@intergga.ch>
Signed-off-by: Joel Hawksley <joelhawksley@github.com>
@joelhawksley joelhawksley requested a review from marcoroth March 11, 2026 21:10
Copy link
Owner

@marcoroth marcoroth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thank you @joelhawksley! 🙏🏼

@marcoroth marcoroth merged commit 8d964bf into marcoroth:main Mar 11, 2026
24 checks passed
marcoroth added a commit that referenced this pull request Mar 11, 2026
…1365)

This pull request updates the Ruby CLI to show the compiled source when
the `compile` subcommand produced invalid Ruby

**Before**
```
❯ bin/herb compile test.html.erb
Compiled template produced invalid Ruby:
  - unexpected end-of-input, assuming it is closing the parent top level context (line 4)
  - expected a matching `)` (line 5)
  - unexpected end-of-input; expected a `)` to close the arguments (line 4)
``` 

**After**
```
❯ bin/herb compile test.html.erb
__herb = ::Herb::Engine; _buf = ::String.new; _buf << __herb.h((render Foo.new do; _buf << 'hello'.freeze; end # comment))
 _buf << '
'.freeze;
_buf.to_s

Compiled template produced invalid Ruby:
  - unexpected end-of-input, assuming it is closing the parent top level context (line 4)
  - expected a matching `)` (line 5)
  - unexpected end-of-input; expected a `)` to close the arguments (line 4)
``` 

Inspired while working on #1363
@marcoroth marcoroth added this to the v1.0.0 milestone Mar 11, 2026
@joelhawksley joelhawksley deleted the fix-inline-comment branch March 11, 2026 22:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants