Visitar URL original
Add Ruby 3.1 and Rails 7.0 to CI by petergoldstein · Pull Request #1254 · github/view_component · GitHub
The Wayback Machine - https://web.archive.org/web/20220512140838/https://github.com/github/view_component/pull/1254
Skip to content
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

Merged
merged 1 commit into from Jan 24, 2022
Merged

Add Ruby 3.1 and Rails 7.0 to CI #1254

merged 1 commit into from Jan 24, 2022

Conversation

petergoldstein
Copy link
Contributor

@petergoldstein petergoldstein commented Jan 24, 2022

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.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 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, 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.

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.
@petergoldstein petergoldstein requested review from Spone and as code owners Jan 24, 2022
Copy link
Member

@BlakeWilliams BlakeWilliams left a comment

I had a few questions but overall looks good, thanks for opening this!

.github/workflows/ci.yml Show resolved Hide resolved
.github/workflows/ci.yml Show resolved Hide resolved
@@ -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
Copy link
Member

@BlakeWilliams BlakeWilliams Jan 24, 2022

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?

Copy link
Contributor Author

@petergoldstein petergoldstein Jan 24, 2022

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)
Copy link
Member

@BlakeWilliams BlakeWilliams Jan 24, 2022

Choose a reason for hiding this comment

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

Nice find here!

Copy link
Contributor Author

@petergoldstein petergoldstein Jan 24, 2022

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.

@petergoldstein
Copy link
Contributor Author

@petergoldstein petergoldstein commented Jan 24, 2022

@BlakeWilliams I think I answered all of your questions. Please let me know if you have any followup questions or suggestions.

@BlakeWilliams BlakeWilliams merged commit 5d8413c into github:main Jan 24, 2022
27 checks passed
@BlakeWilliams
Copy link
Member

@BlakeWilliams BlakeWilliams commented Jan 24, 2022

@petergoldstein Looks great 👍. thanks again for opening this!

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.

None yet

2 participants