Skip to content

[v4] Troubleshooting issue with Slim #2409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

edolix
Copy link

@edolix edolix commented Aug 1, 2025

What are you trying to accomplish?

Trying to upgrade ViewComponent to v4 in one of our apps, @xkraty and i noticed a lot of failures in CI like: Couldn't find a template file or inline render method for SomeSlimComponent.... I was like 🤔
We use Slim everywhere and everything worked as expected with v3.

I tried to dig a bit and i think it could be a race condition between Slim initialization and the cache logic for each component.
I may be wrong so apologies in case all of this doesn't make sense!

In order:

In ViewComponent::Base inherited each component will be automatically compiled (and then cached) as soon as the file is loaded:

def inherited(child)
# Compile so child will inherit compiled `call_*` template methods that
# `compile` defines
__vc_compile

The problem comes with the Slim gem that will register the slim template handler only at the action_view hook causing a (possible) race condition within the ViewComponent::Compiler.gather_templates method:

templates = @component.sidecar_files(
ActionView::Template.template_handler_extensions
).map do |path|

ActionView::Template.template_handler_extensions might not include "slim" at that point.

All templates are gonna be memoized (with the related template_errors as well).

If few components got loaded (and automatically cached due to ViewComponent::Base.inherited) before the action_view hook (that will register slim as a valid template handler)... those components will always raise the Couldn't find a template file or inline render method for ....

What approach did you choose and why?

We created a Rails app trying to reproduce the issue with no luck. I'll try again tomorrow on it.

This PR is a basic/raw/dumb attempt to - at least - highlight the issue and give more context.

Anything you want to highlight for special attention from reviewers?

Nothing - just thanks for the great VC gem! 🙌

@edolix edolix marked this pull request as draft August 1, 2025 08:04
@edolix edolix changed the title wip: [v4] Troubleshooting issue with Slim [v4] Troubleshooting issue with Slim Aug 1, 2025
@edolix
Copy link
Author

edolix commented Aug 4, 2025

note: today i found other two apps where v4 + Slim caused the CI to fail with the same errors

@joelhawksley
Copy link
Member

@edolix thank you for the report and for following up about seeing this issue elsewhere. I too was unable to reproduce it in https://github.com/ViewComponent/demo. (I've now added Slim to the test suite so you can try to reproduce it there if you'd like)

Please keep us posted as you work to reproduce it in isolation- we don't use Slim at GitHub so I can't dedicate much time to this issue at the moment ❤️

@edolix
Copy link
Author

edolix commented Aug 6, 2025

@joelhawksley i found a way to reproduce it in this repo: https://github.com/edolix/view-component-bug-replica

I added piece by piece until i got the same behavior of my apps - i'm adding more info in the README but let me know if you see the same errors 🙏
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants