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

Gem::Requirement.new(['>= x', '< y']) in required_ruby_version crashes RuboCop #12105

Closed
ItsEcholot opened this issue Aug 8, 2023 · 4 comments

Comments

@ItsEcholot
Copy link
Contributor

RuboCop crashes when specifying the required_ruby_version in the gemspec using multiple version constraints in the Gem::Requirement initializer.


Expected behavior

RuboCop should be able to handle multiple version constraints in the Gem::Requirement initializer.

Actual behavior

For /Users/.../xyz: configuration from /Users/.../xyz/.rubocop.yml
Inheriting configuration from /Users/.../xyz/vendor/bundle/gems/standards-1.18.0/.standards_rubocop.yml
configuration from /Users/.../xyz/vendor/bundle/gems/rubocop-rails-2.15.2/config/default.yml
configuration from /Users/.../xyz/vendor/bundle/gems/rubocop-rails-2.15.2/config/default.yml
Default configuration from /Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/config/default.yml
undefined method `value' for s(:send,
  s(:const,
    s(:const, nil, :Gem), :Requirement), :new,
  s(:array,
    s(:send,
      s(:str, ">= 3.1"), :freeze),
    s(:send,
      s(:str, "< 3.3"), :freeze))):RuboCop::AST::SendNode
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/target_ruby.rb:202:in `version_from_right_hand_side'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/target_ruby.rb:211:in `find_default_minimal_known_ruby'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/target_ruby.rb:176:in `find_version'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/target_ruby.rb:29:in `initialize'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/target_ruby.rb:253:in `new'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/target_ruby.rb:253:in `block in source'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/target_ruby.rb:253:in `each'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/target_ruby.rb:253:in `each'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/target_ruby.rb:253:in `each'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/target_ruby.rb:253:in `each'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/target_ruby.rb:253:in `each'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/target_ruby.rb:253:in `detect'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/target_ruby.rb:253:in `source'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/target_ruby.rb:257:in `version'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/config_validator.rb:64:in `target_ruby_version'
/opt/ruby/ruby-3.2.1-p31/lib/ruby/3.2.0/forwardable.rb:240:in `target_ruby_version'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/config_obsoletion/parameter_rule.rb:36:in `applies_to_current_ruby_version?'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/config_obsoletion/parameter_rule.rb:22:in `violated?'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/config_obsoletion.rb:97:in `block in obsoletions'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/config_obsoletion.rb:96:in `map'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/config_obsoletion.rb:96:in `obsoletions'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/config_obsoletion.rb:40:in `reject_obsolete!'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/config_validator.rb:78:in `check_obsoletions'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/config_validator.rb:44:in `validate'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/config.rb:55:in `check'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/config.rb:26:in `create'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/config_loader.rb:65:in `load_file'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/config_loader_resolver.rb:214:in `block in base_configs'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/config_loader_resolver.rb:213:in `map'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/config_loader_resolver.rb:213:in `base_configs'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/config_loader_resolver.rb:21:in `resolve_inheritance'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/config_loader.rb:58:in `load_file'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/config_loader.rb:112:in `configuration_from_file'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/config_store.rb:68:in `for_dir'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/config_store.rb:47:in `for_pwd'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/cli.rb:145:in `parallel_by_default!'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/cli.rb:49:in `block in run'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/cli.rb:77:in `profile_if_needed'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/lib/rubocop/cli.rb:43:in `run'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/exe/rubocop:19:in `block in <top (required)>'
/opt/ruby/ruby-3.2.1-p31/lib/ruby/3.2.0/benchmark.rb:311:in `realtime'
/Users/.../xyz/vendor/bundle/gems/rubocop-1.55.0/exe/rubocop:19:in `<top (required)>'
/Users/.../xyz/vendor/bundle/bin/rubocop:25:in `load'
/Users/.../xyz/vendor/bundle/bin/rubocop:25:in `<main>'
Finished in 0.580071999997017 seconds

Steps to reproduce the problem

Add the following to a *.gemspec file:
s.required_ruby_version = Gem::Requirement.new([">= 3.1".freeze, "< 3.3".freeze])

Or, add the following to a Rakefile and generate a new gemspec:
spec.required_ruby_version = '>= 3.1', '< 3.3'

RuboCop version

This call also fails with the above content in the gemspec.
Here the output with required_ruby_version removed:

bin/ruby -S rubocop -V
1.55.0 (using Parser 3.2.2.3, rubocop-ast 1.29.0, running on ruby 3.2.1) [x86_64-darwin22]
  - rubocop-rails 2.15.2
@ItsEcholot
Copy link
Contributor Author

ItsEcholot commented Aug 8, 2023

This seems to fail because the :gem_requirement? matcher only matches for strings in the Gem::Requirement initializer:

def_node_matcher :gem_requirement?, <<~PATTERN
  (send (const(const _ :Gem):Requirement) :new $str)
PATTERN

@alexeyschepin
Copy link
Contributor

Hi @ItsEcholot ,
I believe the issue is in the syntax of your example. Try passing parameters without array like

s.required_ruby_version = Gem::Requirement.new(">= 3.1".freeze, "< 3.3".freeze)

See the test case https://github.com/rubocop/rubocop/blob/master/spec/rubocop/cop/gemspec/required_ruby_version_spec.rb#L43

@ItsEcholot
Copy link
Contributor Author

Hey @alexeyschepin,

s.required_ruby_version = Gem::Requirement.new(">= 3.1".freeze, "< 3.3".freeze)

Also does not work returning the following error:

undefined method `value' for s(:send,
  s(:const,
    s(:const, nil, :Gem), :Requirement), :new,
  s(:send,
    s(:str, ">= 3.1"), :freeze),
  s(:send,
    s(:str, "< 3.3"), :freeze)):RuboCop::AST::SendNode

Using the exact line from the test case you linked also doesn't work?

s.required_ruby_version = Gem::Requirement.new(">= 2.7.0", "<= 2.8")
undefined method `value' for s(:send,
  s(:const,
    s(:const, nil, :Gem), :Requirement), :new,
  s(:str, ">= 2.7.0"),
  s(:str, "<= 2.8")):RuboCop::AST::SendNode

@ItsEcholot
Copy link
Contributor Author

While the required_ruby_version_spec passes (thanks @alexeyschepin for the pointer), the target_ruby_spec has no tests for supporting multiple ruby versions in a Gem::Requirement. And indeed if such a test is added:

          it 'sets first known ruby version that satisfies range requirement' do
            content =
              <<-HEREDOC
                Gem::Specification.new do |s|
                  s.name = 'test'
                  s.required_ruby_version = Gem::Requirement.new('>= 2.3.1', '< 3.0.0')
                  s.licenses = ['MIT']
                end
              HEREDOC

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

It fails with the same exception I experienced and reported in the first post.

@koic koic closed this as completed in 252af0d Aug 17, 2023
koic added a commit that referenced this issue Aug 17, 2023
…n_gem_requirement_matching

[Fix #12105] Adjust target ruby gem requirement matcher and version parsing to support multiple version constraints
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

No branches or pull requests

2 participants