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

Cop Gemspec/OrderedDependencies breaks when parsing a frozen gem name. #6086

Closed
gdeoliveira opened this issue Jul 8, 2018 · 1 comment · Fixed by #6087
Closed

Cop Gemspec/OrderedDependencies breaks when parsing a frozen gem name. #6086

gdeoliveira opened this issue Jul 8, 2018 · 1 comment · Fixed by #6087
Labels

Comments

@gdeoliveira
Copy link

Expected behavior

Gemspec/OrderedDependencies parses frozen gem names correctly.

Actual behavior

The cop chokes when trying to call downcase after parsing a frozen gem name. The problem seems to come from this line of code since str_content returns nil for a gem name that is explicitly frozen.

Stack trace of the error:

NoMethodError:
  undefined method `downcase' for nil:NilClass

./lib/rubocop/cop/mixin/ordered_gem_node.rb:19:in `case_insensitive_out_of_order?'
./lib/rubocop/cop/gemspec/ordered_dependencies.rb:67:in `block in investigate'
./lib/rubocop/cop/gemspec/ordered_dependencies.rb:104:in `block in dependency_declarations'
./lib/rubocop/ast/node.rb:575:in `block in visit_descendants'
./lib/rubocop/ast/node.rb:185:in `block in each_child_node'
./lib/rubocop/ast/node.rb:183:in `each'
./lib/rubocop/ast/node.rb:183:in `each_child_node'
./lib/rubocop/ast/node.rb:574:in `visit_descendants'
./lib/rubocop/ast/node.rb:576:in `block in visit_descendants'
./lib/rubocop/ast/node.rb:185:in `block in each_child_node'
./lib/rubocop/ast/node.rb:183:in `each'
./lib/rubocop/ast/node.rb:183:in `each_child_node'
./lib/rubocop/ast/node.rb:574:in `visit_descendants'
./lib/rubocop/ast/node.rb:260:in `each_node'
./lib/rubocop/cop/gemspec/ordered_dependencies.rb:102:in `dependency_declarations'
./lib/rubocop/cop/gemspec/ordered_dependencies.rb:65:in `each'
./lib/rubocop/cop/gemspec/ordered_dependencies.rb:65:in `each_cons'
./lib/rubocop/cop/gemspec/ordered_dependencies.rb:65:in `investigate'
./lib/rubocop/cop/commissioner.rb:96:in `block (2 levels) in invoke_custom_processing'
./lib/rubocop/cop/commissioner.rb:105:in `with_cop_error_handling'
./lib/rubocop/cop/commissioner.rb:95:in `block in invoke_custom_processing'
./lib/rubocop/cop/commissioner.rb:92:in `each'
./lib/rubocop/cop/commissioner.rb:92:in `invoke_custom_processing'
./lib/rubocop/cop/commissioner.rb:43:in `investigate'
./lib/rubocop/rspec/cop_helper.rb:71:in `_investigate'
./lib/rubocop/rspec/cop_helper.rb:25:in `inspect_source'
./lib/rubocop/rspec/expect_offense.rb:61:in `expect_no_offenses'

Steps to reproduce the problem

Attempt to run Rubocop with a gemspec file like the following:

spec.add_development_dependency "rspec".freeze
spec.add_development_dependency "rubocop".freeze

RuboCop version

$ rubocop -V
0.58.0 (using Parser 2.5.1.0, running on ruby 2.5.1 x86_64-linux-musl)
@koic
Copy link
Member

koic commented Jul 8, 2018

Thanks for the feedback. I confirmed this reproduction and I opened a PR #6087.

koic added a commit to koic/rubocop that referenced this issue Jul 9, 2018
Fixes rubocop#6086.

This PR fixes an error for `Gemspec/OrderedDependencies` when using
method call to gem names in gemspec.

The following is reproduction steps.

```console
% cat /tmp/foo.gemspec
Gem::Specification.new do |spec|
  spec.add_development_dependency 'rspec'.freeze
  spec.add_development_dependency 'rubocop'.freeze
end
% rubocop foo.gemspec --only Gemspec/OrderedDependencies -d
For /private/tmp: configuration from
/Users/koic/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.0/config/default.yml
Inheriting configuration from
/Users/koic/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.0/config/enabled.yml
Inheriting configuration from
/Users/koic/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.0/config/disabled.yml
Inspecting 1 file
Scanning /private/tmp/foo.gemspec
An error occurred while Gemspec/OrderedDependencies cop was inspecting
/private/tmp/foo.gemspec.
undefined method `downcase' for nil:NilClass

(snip)

/Users/koic/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.0/exe/rubocop:12:in
`<top (required)>'
/Users/koic/.rbenv/versions/2.5.1/bin/rubocop:23:in `load'
/Users/koic/.rbenv/versions/2.5.1/bin/rubocop:23:in `<main>'
.

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Gemspec/OrderedDependencies cop was inspecting
/private/tmp/foo.gemspec.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop-hq/rubocop/issues

Mention the following information in the issue report:
0.58.0 (using Parser 2.5.1.0, running on ruby 2.5.1 x86_64-darwin17)
Finished in 0.43951499997638166 seconds
```

This PR considers the case to be called in the method chain for
the gem name as follows.

```ruby
spec.add_development_dependency 'foo'.bar.baz
```

In this case, the gem name will be resolved searching recursively.
bbatsov pushed a commit that referenced this issue Jul 9, 2018
Fixes #6086.

This PR fixes an error for `Gemspec/OrderedDependencies` when using
method call to gem names in gemspec.

The following is reproduction steps.

```console
% cat /tmp/foo.gemspec
Gem::Specification.new do |spec|
  spec.add_development_dependency 'rspec'.freeze
  spec.add_development_dependency 'rubocop'.freeze
end
% rubocop foo.gemspec --only Gemspec/OrderedDependencies -d
For /private/tmp: configuration from
/Users/koic/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.0/config/default.yml
Inheriting configuration from
/Users/koic/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.0/config/enabled.yml
Inheriting configuration from
/Users/koic/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.0/config/disabled.yml
Inspecting 1 file
Scanning /private/tmp/foo.gemspec
An error occurred while Gemspec/OrderedDependencies cop was inspecting
/private/tmp/foo.gemspec.
undefined method `downcase' for nil:NilClass

(snip)

/Users/koic/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/rubocop-0.58.0/exe/rubocop:12:in
`<top (required)>'
/Users/koic/.rbenv/versions/2.5.1/bin/rubocop:23:in `load'
/Users/koic/.rbenv/versions/2.5.1/bin/rubocop:23:in `<main>'
.

1 file inspected, no offenses detected

1 error occurred:
An error occurred while Gemspec/OrderedDependencies cop was inspecting
/private/tmp/foo.gemspec.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop-hq/rubocop/issues

Mention the following information in the issue report:
0.58.0 (using Parser 2.5.1.0, running on ruby 2.5.1 x86_64-darwin17)
Finished in 0.43951499997638166 seconds
```

This PR considers the case to be called in the method chain for
the gem name as follows.

```ruby
spec.add_development_dependency 'foo'.bar.baz
```

In this case, the gem name will be resolved searching recursively.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants