-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Infer TargetRubyVersion version from Gemfile #5610
Comments
I like this idea. A quick question (and one in which I have no vested interest in the answer): should the check of the |
I don't have a huge preference here either. I went with making the Gemfile the authority because I get the feeling more people are including the ruby version in the Gemfile these days. Since RMV|rbenv happily read the ruby version from the Gemfile, there's less of a need for bundler based projects to keep a .ruby-version file. If ruby is specified in both the Gemfile and .ruby-version file:
Some tools are going to consider |
Yeah, for performance reasons alone I'd be inclined to check for But honestly, if there's no big hit either way, then I'm not fussed. Glad we both put these thoughts down though 😄 |
# Gemfile
source 'https://rubygems.org
ruby File.read('.ruby-version', mode: 'rb').chomp
gem 'rails'
...
Are we ok with Rubocop being among the tools that are particular about the format of this line? Or, can we ask Bundler for the Ruby version after it parses the I know that we skipped depending on Bundler in #5518, but the lockfile is safer to parse with a regular expression since it can't contain arbitrary Ruby code. |
This would be very helpful! |
@mikegee Good points. I was leaning towards searching the Gemfile because rubocop doesn't require the project to be bundled; it will happily execute without the Gemfile.lock being present. If we search the Gemfile, we can identify the ruby version when the user hasn't yet bundled the project. I can make the parse logic a little smarter and handle the cases where FWIW - I confirmed with RVM and it can't correctly identify the ruby version when it's on a compound line
I'm comfortable assuming/requiring |
@roberts1000 you don't want to depend on Bundler to parse the Gemfile? I think it would be nice if this Rubocop feature could handle your example: ruby File.read('.ruby-version', mode: 'rb').chomp |
I'm ok with using Bundler. I've been avoiding it so we wouldn't have to introduce a run time dependency on Bundler. It's easy enough to parse the Gemfile text and pluck out the necessary string, so I figured regexing was the quickest path to getting the thumbs up on this issue. However, if we want to support strategies like |
I think the feature should use # Gemfile
source 'https://rubygems.org'
ruby '2.5.0' Generated Gemfile.lock
Pros:
|
Sounds good. PR incoming. |
This has been proposed before, but now that #5473 has been merged (which lets us infer the
TargetRailsVersion
from the bundler lock files), it makes sense to consider the bundlerGemfile|gems.rb
when we choose theTargetRubyVersion
. The new process would look like this:TargetRubyVersion
is specified in the config, use it. Otherwise,Gemfile
orgems.rb
file, use it. Otherwise,.ruby-version
file, read the version from the file and use it, Otherwise,Steps 1, 3 and 4 make up the current process. It would be trivial to pluck the
ruby
line out of theGemfile|gems.rb
and setTargetRubyVersion
based on that value.Pain Point
My company has dozen of Rails apps, but we force them to use a shared RuboCop configuration. We've been setting the
TargetRubyVersion
in that configuration, but not all apps are on the same Ruby. This forces us to use the lowest Ruby version in our shared config, or have the apps override TargetRubyVersion in their own rubocop.yml (which we try to avoid because devs tend to start overriding other stuff we don't want to them override), or keep a.ruby-version
file so RuboCop will read from there (this is our current solution).Unfortunately, my company uses a PaaS that actually ignores the
.ruby-version
file. The PaaS pulls the ruby version from theGemfile|gems.rb
if it's present. The only reason we actually include a.ruby-version
file today, in our particular projects, is to get rubocop to be more dynamic in how it chooses the TargetRubyVersion. (We do use RVM, but it happily reads the ruby version from theGemfile|gems.rb
. )We tried changing our Gemfiles to read the ruby version from the
.ruby-version
file, via code like:This unfortunately causes errors in other tools which expect a raw version string to be present in the Gemfile directly after the
ruby
method name.Benefits
.ruby-version
file from their projects.TargetRailsVersion
- which we now infer by reading theGemfile.lock|gems.locked
file.Implementation
This is a pretty small change. We simply need to require
ruby 'x.x.x'
be on a line by itself (which is how it's typically done) and regex the value out of theGemfile
orgems.rb
.I can submit a PR quickly so #5473, and this issue, can drop in the same release if there's interest.
The text was updated successfully, but these errors were encountered: