r7kamura / rubocop-slim

RuboCop plugin for Slim template.
MIT License
7 stars 1 forks source link

Compatibility issues with view_component gem #29

Closed OlegChuev closed 1 hour ago

OlegChuev commented 2 hours ago

Description

In our application, we use the Slim template language alongside the view_component gem. There is a known issue in view_component where internal content is duplicated in nested forms. This issue was previously resolved by enabling the config.view_component.capture_compatibility_patch_enabled = true option in environment.rb.

After migrating from slimcop to rubocop-slim, the duplication issue reappeared. I tested the staging environment and attempted to revert to the deprecated slimcop version, which confirmed that the issue is directly related to rubocop-slim.

Expected Behavior

The duplication issue should not occur when using rubocop-slim, provided the compatibility patch is enabled.

Actual Behavior

Internal content in nested forms is duplicated after the migration to rubocop-slim.

Additional Context

The issue is confirmed to be linked to rubocop-slim since reverting to slimcop resolves the problem. Let me know if you need further details or steps to reproduce the issue.

r7kamura commented 2 hours ago

Reading the description you wrote, I couldn't understand why rubocop-slim, which is just a static parser of slim templates, is related to the view_component gem. Can you elaborate a bit more on the situation?

r7kamura commented 2 hours ago

This is just a guess, but is it possible that the version of other gems (e.g., temple gem) that Gemfile.lock refers to has changed between when you were using slimcop and when you were using rubocop-slim?

Since rubocop-slim is not a library that is loaded at runtime in normal usage, it seems odd that this would change the behavior of a process that uses view_component.

(If you accidentally forgot to add require: false to your Gemfile and inadvertently loaded rubocop-slim at Rails startup, something strange could happen... 🤔)

OlegChuev commented 2 hours ago

Just tested with require: false, and it seems that was the issue. Not entirely sure what the root cause might have been.

Maybe we should update the README to make require: false a mandatory part of the setup instructions?

r7kamura commented 2 hours ago

I see. I'm glad it was resolved 👍

Hmm. As you say, this is a common trap to step through when using Rails and RuboCop together. I will consider adding it. Since require: false is not required for normal Ruby projects, but only for Rails apps, it may be debatable whether to add it to README or not.


There is another good workaround besides adding require: false.

When using RuboCop in a Rails app, I believe it is good practice to provide rubocop gem group as follows:

# Gemfile
group :rubocop do
  gem 'rubocop'
  gem 'rubocop-rails'
  gem 'rubocop-slim'
  ...
end

This way, you don't have to go around adding require: false, and you can also specify BUNDLE_ONLY to avoid installing these unnecessary gems on production or staging environments. (e.g. BUNDLE_ONLY=default:production)