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

Received malformed format string ArgumentError from rubocop #42

Closed
jedinerd opened this issue Apr 11, 2013 · 11 comments
Closed

Received malformed format string ArgumentError from rubocop #42

jedinerd opened this issue Apr 11, 2013 · 11 comments
Labels

Comments

@jedinerd
Copy link

/Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/cop/offence.rb:17:in%': malformed format string - %[ (ArgumentError)
from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/cop/offence.rb:17:in to_s' from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/report/plain_text.rb:10:injoin'
from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/report/plain_text.rb:10:in generate' from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/report/plain_text.rb:14:indisplay'
from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/cli.rb:67:in block in run' from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/cli.rb:48:ineach'
from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/cli.rb:48:in run' from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/bin/rubocop:12:inblock in <top (required)>'
from /Users/michael.hogg/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/benchmark.rb:295:in realtime' from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/bin/rubocop:11:in<top (required)>'
from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/bin/rubocop:19:in load' from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/bin/rubocop:19:in

'
from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/bin/ruby_noexec_wrapper:14:in eval' from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/bin/ruby_noexec_wrapper:14:in'
`

@jonas054
Copy link
Collaborator

What code were you checking with rubocop when this error occurred? Can you investigate if there's a specific violation in the checked code that triggers this bug? That would help a lot.

@jedinerd
Copy link
Author

Here are the last few lines before the error occurred :

C: 82: Prefer single-quoted strings when you don't need string
interpolation or special symbols.
C: 82: Prefer single-quoted strings when you don't need string
interpolation or special symbols.
C: 83: Trailing whitespace detected.
C: 83: Use snake_case for methods and variables.
/Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/cop/offence.rb:17:in
%': malformed format string - %[ (ArgumentError) from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/cop/offence.rb:17:in to_s'
from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/report/plain_text.rb:10:in
join' from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/report/plain_text.rb:10:in generate'
from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/report/plain_text.rb:14:in
display' from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/cli.rb:67:in block in run'
from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/cli.rb:48:in
each' from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/lib/rubocop/cli.rb:48:in run'
from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/bin/rubocop:12:in
block in <top (required)>' from /Users/michael.hogg/.rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/benchmark.rb:295:in realtime'
from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/gems/rubocop-0.4.0/bin/rubocop:11:in
<top (required)>' from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/bin/rubocop:19:in load'
from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/bin/rubocop:19:in
<main>' from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/bin/ruby_noexec_wrapper:14:in eval'
from /Users/michael.hogg/.rvm/gems/ruby-1.9.3-p392@fig/bin/ruby_noexec_wrapper:14:in
`

'

def self.get_attributes(hostname, attrs = ['puppetVar', 'environment'],
base = 'ou=Hosts,dc=reachlocal,dc=com')
@scope = Net::LDAP::SearchScope_WholeSubtree
@filter = Net::LDAP::Filter.eq("cn", hostname)

  build_connection(LDAPHOST, PORT)
  Fig.logger.info "bind to ldap server " + LDAPHOST + " successful."
  •  varHash = search_for_attributes(base, @scope, @filter, attrs)
    
    return varHash
    end

Here is the snippet of code it was processing. The line i have *'d is line
83. Let me know if there is anything else i can do to help. This def was
in a class, in a module.

On Thu, Apr 11, 2013 at 3:42 PM, Jonas Arvidsson
[email protected]:

What code were you checking with rubocop when this error occurred? Can you
investigate if there's a specific violation in the checked code that
triggers this bug? That would help a lot.


Reply to this email directly or view it on GitHubhttps://github.com//issues/42#issuecomment-16266252
.

@jonas054
Copy link
Collaborator

I'm sorry but I have not been able to reproduce the error using your code snippet with ruby-1.9.3-p392 and rubocop-0.4.0. The error means that ruby found something in a format string that it couldn't process, in this case a percent sign followed by a left square bracket. Line 17 in offence.rb is:

    "#{encode_severity}:%3d: #{message}" % [line_number]

which means that either encode_severity or message contained that sequence of characters, and I don't see how that would be possible, having looked through the code a bit. But it is a little careless to define the format string that way. "%s:%3d: %s" % [encode_severity, line_number, message] would be safer, if we're worried about percent signs in the attributes.

Is it possible that your installation of rubocop is corrupted? Because I'm drawing a blank.

@bbatsov
Copy link
Collaborator

bbatsov commented Apr 15, 2013

Is this problem still present with 0.4.6?

@lloeki
Copy link
Contributor

lloeki commented Apr 16, 2013

Minimal test case:

v = a_method(:sym) %{:foo => bar}

This gives me /Users/lloeki/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rubocop-0.4.6/lib/rubocop/cop/offence.rb:17:in%': malformed format string - %' (ArgumentError)`

In this case, changing the line to:

h = a_method(:sym) % {:foo => bar}

by adding a single space makes rubocop not crash.

Another test case for a similar error:

a_method(:sym) %{:foo => bar}

That last one will give /Users/lloeki/.rbenv/versions/2.0.0-p0/lib/ruby/gems/2.0.0/gems/rubocop-0.4.6/lib/rubocop/cop/offence.rb:17:in '%': too few arguments (ArgumentError), and the space trick does not work around it.

@lloeki
Copy link
Contributor

lloeki commented Apr 16, 2013

Anyway, executing the following code directly with ruby throws the same errors, therefore it's the parser itself that rejects a malformed ruby file.

Case 1:

#!/usr/bin/env ruby
def a_method(s)
  "abc %(foo)s def"
end

bar = "bar"
h = a_method(:sym) % {:foo => bar}

Case 2:

#!/usr/bin/env ruby
def a_method(s)
  "abc %(foo)s def"
end

bar = "bar"
a_method(:sym) % {:foo => bar}

I'm not sure rubocop should crash like this, but rather catch the parsing error and not try to analyze the file, display output similar to the following, and continue with the next one.

==foo.rb==
E:   7: Unparseable file: ArgumentError: in `%': malformed format string - %'

I don't know if it's even possible, but I venture this is how I'd like rubocop to behave in this case.

@bbatsov
Copy link
Collaborator

bbatsov commented Apr 16, 2013

I think we should go with the second option - intercepting the exception and converting it to an offence. Would you like to take a stab at this, @lloeki?

@bbatsov
Copy link
Collaborator

bbatsov commented Apr 17, 2013

Hmm, I tried all the suggest code snippets and none of them seems to break RuboCop master (unreleased). Could you verify this @lloeki?

@lloeki
Copy link
Contributor

lloeki commented Apr 17, 2013

[lloeki@sekhmet rubocop›32a9b9e…›. !]$ cat foo.rb 
a_method(:sym) %{:foo => bar}
[lloeki@sekhmet rubocop›32a9b9e…›. !]$ bin/rubocop foo.rb 
/Users/lloeki/Workspace/forks/rubocop/lib/rubocop/cop/offence.rb:17:in `sprintf': too few arguments (ArgumentError)
    from /Users/lloeki/Workspace/forks/rubocop/lib/rubocop/cop/offence.rb:17:in `to_s'
    from /Users/lloeki/Workspace/forks/rubocop/lib/rubocop/report/plain_text.rb:10:in `join'
    from /Users/lloeki/Workspace/forks/rubocop/lib/rubocop/report/plain_text.rb:10:in `generate'
    from /Users/lloeki/Workspace/forks/rubocop/lib/rubocop/report/plain_text.rb:14:in `display'
    from /Users/lloeki/Workspace/forks/rubocop/lib/rubocop/cli.rb:76:in `block in run'
    from /Users/lloeki/Workspace/forks/rubocop/lib/rubocop/cli.rb:44:in `each'
    from /Users/lloeki/Workspace/forks/rubocop/lib/rubocop/cli.rb:44:in `run'
    from bin/rubocop:13:in `block in <main>'
    from /Users/lloeki/.rbenv/versions/2.0.0-p0/lib/ruby/2.0.0/benchmark.rb:296:in `realtime'
    from bin/rubocop:12:in `<main>'
[lloeki@sekhmet rubocop›32a9b9e…›. ! 1]$ rbenv version
2.0.0-p0 (set by /Users/lloeki/.rbenv/version)
[lloeki@sekhmet rubocop›32a9b9e…›. !]$ 

bbatsov pushed a commit that referenced this issue Apr 17, 2013
@bbatsov
Copy link
Collaborator

bbatsov commented Apr 17, 2013

Wow, I'm always running rubocop with -e and I totally misinterpreted the problem. Try the latest master, should be ok now.

@lloeki
Copy link
Contributor

lloeki commented Apr 17, 2013

Just tested it. Looks fine.

koic added a commit that referenced this issue Sep 15, 2023
This PR resolves the following error when running `rake changelog:merge` in RuboCop Rails.
So it will prevent the same error from occurring in this repository.

```conosle
$ bundle exec rake changelog:merge
rake aborted!
NoMethodError: undefined method `[]' for nil
/Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:139:in `block in contributors'
/Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:138:in `each'
/Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:138:in `flat_map'
/Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:138:in `contributors'
/Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:132:in `new_contributor_lines'
/Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:126:in `merge_content'
/Users/koic/src/github.com/rubocop/rubocop-rails/tasks/changelog.rb:115:in `merge!'
tasks/changelog.rake:21:in `block (2 levels) in <top (required)>'
/Users/koic/.rbenv/versions/3.3.0-dev/bin/bundle:25:in `load'
/Users/koic/.rbenv/versions/3.3.0-dev/bin/bundle:25:in `<main>'
Tasks: TOP => changelog:merge
(See full trace by running task with --trace)
```

This error was caused by the absence of a space after the period:

```diff
-* [#42](rubocop/rubocop-rails#42): Fix an error for `Rails/RedundantActiveRecordAllMethod`.([contributor_name])
+* [#42](rubocop/rubocop-rails#42): Fix an error for `Rails/RedundantActiveRecordAllMethod`. ([contributor_name])
```

The added test verifies that there is a single space between the period and the parentheses.  And tweaks changelog entries

NOTE: For compatibility with outdated formats, if there's no contributor name, it checks that the line ends with a period.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants