Skip to content

Commit

Permalink
Only parse target Ruby from gemspec if array elements are strings
Browse files Browse the repository at this point in the history
This fixes an issue mentioned in [this
comment](#12579 (comment))
on issue #12579. (I believe that the main/original issue mentioned in
issue #12579 has already been fixed by #12732.)

This change is a related/similar followup to #12732 (5026393). That PR
avoided an exception that was occurring in the
`RuboCop::TargetRuby::GemspecFile` target Ruby finder when a project's
gemspec determines the value of `required_ruby_version` in some dynamic
way (such as by reading from a `.ruby-version` file).

However, #12732 did not handle a case where the dynamic value for the
gemspec's `required_ruby_version` is wrapped within an array, and, in
such a case, currently, an exception like the following will occur:

```
Gem::Requirement::BadRequirementError:
  Illformed requirement [">= \#{File.read('.ruby-version').rstrip}"]
  ./lib/rubocop/target_ruby.rb:123:in `new'
  ./lib/rubocop/target_ruby.rb:123:in `find_default_minimal_known_ruby'
  ./lib/rubocop/target_ruby.rb:83:in `find_version'
  ./lib/rubocop/target_ruby.rb:29:in `initialize'
  ./lib/rubocop/target_ruby.rb:259:in `new'
```

(As with the scenario that _was_ fixed by #12732, I believe that this is
essentially a "latent" issue that is now occurring in more projects than
previously, due to #12645 having increased the precedence of the
`GemspecFile` target Ruby finder in the `RuboCop::TargetRuby::SOURCES`
list.)

This change avoids this exception by only attempting to parse a target
Ruby version from an array value for a gemspec's `required_ruby_version`
if every value in the array is a string. When the values within the
`required_ruby_version` array are _not_ all strings, now, rather than
raising an unhandled exception, the `GemspecFile` finder will return
`nil` for the target Ruby version, and so we will continue on to the
other TargetRuby finder classes, in order to find a target Ruby version.
  • Loading branch information
davidrunger authored and bbatsov committed Mar 7, 2024
1 parent abdbe1b commit c1e9881
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
@@ -0,0 +1 @@
* [#12756](https://github.com/rubocop/rubocop/pull/12756): Only parse target Ruby from gemspec if array elements are strings. ([@davidrunger][])
2 changes: 1 addition & 1 deletion lib/rubocop/target_ruby.rb
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
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
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
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

0 comments on commit c1e9881

Please sign in to comment.