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

Only parse target Ruby from gemspec if array elements are strings #12756

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12756](https://github.com/rubocop/rubocop/pull/12756): Only parse target Ruby from gemspec if array elements are strings. ([@davidrunger][])
30 changes: 26 additions & 4 deletions docs/modules/ROOT/pages/configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -647,11 +647,33 @@ AllCops:

NOTE: When `ParserEngine: parser_prism` is specified, the values that can be assigned to `TargetRubyVersion` must be `3.3` or higher.

Otherwise, RuboCop will then check your project for a series of files where
the version may be specified already. The files that will be looked for are
If a `TargetRubyVersion` is not specified in your config, then RuboCop will
check your project for a series of other files where the Ruby version may be
specified already. The files that will be checked are (in this order):
`*.gemspec`, `.ruby-version`, `.tool-versions`, and `Gemfile.lock`.
If Gemspec file has an array for `required_ruby_version`, the lowest version will be used.
If none of the files are found a default version value will be used.

If a target Ruby version cannot be found via any of the above sources, then a
default target Ruby version will be used.

=== Finding target Ruby in a `*.gemspec` file

In order for RuboCop to parse a `*.gemspec` file's `required_ruby_version`, the
Ruby version must be specified using one of these syntaxes:

1. a string range, e.g. `'~> 3.2.0'` or `'>= 3.2.2'`
2. an array of strings, e.g. `['>= 3.0.0', '< 3.4.0']`
3. a `Gem::Requirement`, e.g. `Gem::Requirement.new('>= 3.1.2')`

If a `*.gemspec` file specifies a range of supported Ruby versions via any of
these means, then the greater of the following Ruby versions will be used:

- the lowest Ruby version that is compatible with your specified range
- the lowest version of Ruby that is still supported by your version of RuboCop

If a `*.gemspec` file defines its `required_ruby_version` dynamically (e.g. by
reading from a `.ruby-version` file, via an environment variable, referencing a
constant or local variable, etc), then RuboCop will _not_ detect that Ruby
version, and will instead try to find a target Ruby version elsewhere.

== Setting the parser engine

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/target_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def version_from_gemspec_file(file)
def version_from_right_hand_side(right_hand_side)
gem_requirement_versions = gem_requirement_versions(right_hand_side)

if right_hand_side.array_type?
if right_hand_side.array_type? && right_hand_side.children.all?(&:str_type?)
version_from_array(right_hand_side)
elsif gem_requirement_versions
gem_requirement_versions.map(&:value)
Expand Down
19 changes: 17 additions & 2 deletions spec/rubocop/target_ruby_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,25 @@

context 'when file reads the required_ruby_version from another file' do
it 'uses the default target ruby version' do
Comment on lines 189 to 190
Copy link
Contributor Author

Choose a reason for hiding this comment

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

(This is an existing spec that I only modified to have a heredoc without interpolation; it is otherwise unchanged.)

content = <<~HEREDOC
content = <<~'HEREDOC'
Gem::Specification.new do |s|
s.name = 'test'
s.required_ruby_version = Gem::Requirement.new(">= #{File.read('.ruby-version').rstrip}")
s.licenses = ['MIT']
end
HEREDOC

create_file(gemspec_file_path, content)
expect(target_ruby.version).to eq default_version
end
end

context 'when file reads the required_ruby_version from another file in an array' do
it 'uses the default target ruby version' do
Comment on lines +204 to +205
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the new spec that I am adding for this change. Unfortunately (due to how it looks similar to the spec changed above), GitHub is actually not even including the whole new spec in its rendering of this diff. You'll have to click the down arrow to see the last few lines of this new spec, which GitHub is rendering as if they are preexisting lines.

content = <<~'HEREDOC'
Gem::Specification.new do |s|
s.name = 'test'
s.required_ruby_version = Gem::Requirement.new(">= \#{File.read('.ruby-version').rstrip}")
s.required_ruby_version = [">= #{File.read('.ruby-version').rstrip}"]
s.licenses = ['MIT']
end
HEREDOC
Expand Down