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

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

merged 2 commits into from
Mar 7, 2024

Commits on Mar 7, 2024

  1. Only parse target Ruby from gemspec if array elements are strings

    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.
    davidrunger committed Mar 7, 2024
    Configuration menu
    Copy the full SHA
    bf4d1e0 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    699a65e View commit details
    Browse the repository at this point in the history