Repository navigation
Conversation
Rails 8.2 compiles HTML+ERB templates through Herb when an application opts in, and ships `bin/rails herb:check` to find the templates that won't compile. That task reads the view paths, which don't hold sidecar templates, and inline templates aren't files at all, so an application can pass the check and still raise at the first render of a component. Add `bin/rails view_component:herb:check`, which compiles the HTML+ERB templates of every component the way `ActionView::HerbChecker` does, and make it a prerequisite of `herb:check`. Both are defined only when Action View can compile ERB through Herb. Fixes ViewComponent#2717. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VLwjZw7FEdBpwALzxXxXnJ
As a prerequisite of `herb:check`, the component check aborted on the first failing component, so Rails never checked the views. Report component failures there without stopping, and fail once `herb:check` has finished. `view_component:herb:check` still aborts on its own. Also share sidecar lookups across components while checking, and print the line an inline template starts on, since Herb counts lines from the start of the template. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VLwjZw7FEdBpwALzxXxXnJ
Member
|
@czj thank you for the PR, but I think we should just do this in Rails: rails/rails#59137 |
Contributor
Author
|
That's better, indeed ! Thanks :) |
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.
What are you trying to accomplish?
Fixes #2717
Rails 8.2 can compile HTML+ERB templates through Herb (
config.action_view.erb_implementation = :herb) and shipsbin/rails herb:checkto find templates that won't compile before you adopt it. Component templates compile through Herb too:Template#compiled_sourcehands the ERB handler an:htmltemplate. Butherb:checkreads the view paths, which don't include sidecar templates, and inline templates aren't files at all. So an app can passherb:checkand still raiseHerb::Engine::CompilationErrorthe first time a component renders.What approach did you choose and why?
ViewComponent::HerbCheckerworks likeActionView::HerbChecker. It compiles each component's HTML+ERB templates withActionView::Template::Handlers::ERB, usingimplementation: ERB::Herb,validate_ruby: trueand theSecurityValidator. It returnsFailures forHerb::Engine::CompilationErrorandSecurityError. It checks each component's own sidecar and inline templates. It skips#callmethods and inherited templates, which the parent's check already covers.bin/rails view_component:herb:checkeager loads the app, runs the checker and prints failures in the same format as Rails' task.herb:checknow checks components too. A prerequisite prints component failures without stopping, so Rails still checks the views, and anat_exitmakes the command fail once it has finished. People already run that command before switching to Herb. API-only apps don't getherb:checkfrom Rails, so the hook is skipped there.ERB::Herb, which means Rails 8.2 and later.supported?checks this without loading the herb gem.handler_templateandhandler_sourceout ofTemplate#compiled_source, and addedCompiler#source_templates. Compilation itself doesn't change.Alternatives I considered:
Anything you want to highlight for special attention from reviewers?
rails_mainandrails_main_headare locked to Rails revisions from before Herb support (9cc62ec,11417ba), soHerbChecker.supported?is false everywhere CI runs. The tests forsource_templates,handler_template/handler_sourceand the rake wiring run on every Rails version. The Herb tests will start running once therails_mainlock is bumped. I couldn't run the suite locally, so I'd appreciate a run against current Rails main.at_exitin the prerequisite is a little unusual. I couldn't find another way for a prerequisite to report failures and still let Rails check the views. Happy to drop the hook and keepview_component:herb:checkon its own if you'd rather.Rails.root?