github / view_component Public
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
Add Ruby 3.1 and Rails 7.0 to CI #1254
Add Ruby 3.1 and Rails 7.0 to CI #1254
Conversation
In addition to the straightforward changes required for the CI configuration, there are a few other changes included in this PR. The first change was the Gemfile.lock in the base of the repo. I updated it to be Rails 7.0.1 as opposed to 7.0.0.alpha.2. This required including a few explicit RAILS_VERSION environment variables for certain steps in CI and linting. Two additional items required to get the specs to green were changes to some argument handling and module prepending. To disambiguate with keyword arguments, it was necessary to add an explicit hash in the lib/view_component/instrumentation.rb implementation of render_in. The second issue, which was a bit more subtle, has to do with the change to how module prepending works IF the module is already in the ancestor chain. This is discussed here - https://rubyreferences.github.io/rubychanges/3.1.html#moduleprepend-behavior-change . Because of initializer code in lib/view_component/engine.rb, the ViewComponent::Instrumentation module is prepended to ViewComponent::Base on engine load. But the prepend call is made for a second time when ViewComponent::Instrumentation is explicitly included. In Ruby 3.0 and earlier this second call is a no-op, but in Ruby 3.1 it adds a second ViewComponent::Instrumentation to the ancestor chain, resulting in a duplicate invocation of the notification. My sense is that there's no reason for the self.included call in ViewComponent::Instrumentation (and in fact, no reason for a component to explicitly include ViewComponent::Instrumentation at all), but given my limited familiarity with the internals of the library I simply restored the Ruby 3.0 and earlier behavior for this case.
I had a few questions but overall looks good, thanks for opening this!
| @@ -7,3 +7,9 @@ rails_version = "#{ENV['RAILS_VERSION'] || 'main'}" | |||
|
|
|||
| gem "capybara", "~> 3" | |||
| gem "rails", rails_version == "main" ? { git: "https://github.com/rails/rails", ref: "main" } : rails_version | |||
|
|
|||
| if RUBY_VERSION >= "3.1" | |||
| gem "net-imap", require: false | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if we omit these gems?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this is a known Rails bug, fixed on the Rails 7 branch (with Rails 7.0.1) but not on Rails 6.1. You can find a discussion here
These gems were included as defaults in Ruby versions 3.0 and earlier, but are not included in 3.1+. Rails depends on them transitively through the mail gem. Rails 7.0.1 fixed this but it can't be fixed by Rails for 6.x because of the need to support Ruby 2.6, where this inclusion causes an error.
On a more practical note, if these are left out then Ruby 3.1 / Rails 6.1 builds fail with an error saying they can't find net-smtp.
| @@ -5,14 +5,16 @@ | |||
| module ViewComponent # :nodoc: | |||
| module Instrumentation | |||
| def self.included(mod) | |||
| mod.prepend(self) | |||
| mod.prepend(self) unless ancestors.include?(ViewComponent::Instrumentation) | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice find here!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. :)
I have to admit it took me a couple of hours to suss this out. I was scratching my head looking at caller outputs and trying to figure out how they could be different between Ruby 3.0 and Ruby 3.1. Very satisfying to get an answer.
|
@BlakeWilliams I think I answered all of your questions. Please let me know if you have any followup questions or suggestions. |
|
@petergoldstein Looks great |


Summary
Changes to CI configuration to add Ruby 3.1 and Rails 7.0 to the CI matrix, along with necessary changes to get everything green and on stable versions.
Other Information
In addition to the straightforward changes required for the CI configuration, there are a few other changes included in this PR.
The first change was the
Gemfile.lockin the base of the repo. I updated it to be Rails 7.0.1 as opposed to 7.0.0.alpha.2. This required including a few explicitRAILS_VERSIONenvironment variables for certain steps in CI and linting.Two additional items required to get the specs to green were changes to some argument handling and module prepending.
To disambiguate with keyword arguments, it was necessary to add an explicit hash in the
lib/view_component/instrumentation.rbimplementation ofrender_in.The second issue, which was a bit more subtle, has to do with the change to how module prepending in Ruby 3.1 works IF the module is already in the ancestor chain. This is discussed here.
Because of initializer code in
lib/view_component/engine.rb, theViewComponent::Instrumentationmodule is prepended toViewComponent::Baseon engine load. But theprependcall is made for a second time whenViewComponent::Instrumentationis explicitly included. In Ruby 3.0 and earlier this second call is a no-op, but in Ruby 3.1 it adds a secondViewComponent::Instrumentationto the ancestor chain, resulting in a duplicate invocation of the notification.My sense is that there's no reason for the
self.includedcall inViewComponent::Instrumentation(and in fact, no reason for a component to explicitly includeViewComponent::Instrumentationat all), but given my limited familiarity with the internals of the library I simply restored the Ruby 3.0 and earlier behavior for this case.