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

Broken cache serialization #2694

Closed
georgyangelov opened this issue Jan 20, 2016 · 1 comment
Closed

Broken cache serialization #2694

georgyangelov opened this issue Jan 20, 2016 · 1 comment

Comments

@georgyangelov
Copy link
Contributor

With the latest version 0.36.0 (using Parser 2.3.0.1, running on ruby 2.3.0 x86_64-darwin15) on OSX I am getting the following when an offence is cached:

Inspecting 453 files
....

4 files inspected, no offenses detected
undefined method `to_sym' for {"name"=>"convention"}:Hash
Did you mean?  to_s
               to_yaml
               to_set
/Users/stormbreaker/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rubocop-0.36.0/lib/rubocop/cop/severity.rb:28:in `name_from_code'
/Users/stormbreaker/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rubocop-0.36.0/lib/rubocop/cop/severity.rb:34:in `initialize'
/Users/stormbreaker/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rubocop-0.36.0/lib/rubocop/cop/offense.rb:61:in `new'
/Users/stormbreaker/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rubocop-0.36.0/lib/rubocop/cop/offense.rb:61:in `initialize'
/Users/stormbreaker/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rubocop-0.36.0/lib/rubocop/cached_data.rb:53:in `new'
/Users/stormbreaker/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rubocop-0.36.0/lib/rubocop/cached_data.rb:53:in `block in deserialize_offenses'
/Users/stormbreaker/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rubocop-0.36.0/lib/rubocop/cached_data.rb:49:in `map!'
/Users/stormbreaker/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rubocop-0.36.0/lib/rubocop/cached_data.rb:49:in `deserialize_offenses'
/Users/stormbreaker/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rubocop-0.36.0/lib/rubocop/cached_data.rb:14:in `from_json'
/Users/stormbreaker/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rubocop-0.36.0/lib/rubocop/result_cache.rb:70:in `load'
/Users/stormbreaker/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rubocop-0.36.0/lib/rubocop/runner.rb:84:in `process_file'
/Users/stormbreaker/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rubocop-0.36.0/lib/rubocop/runner.rb:59:in `block in inspect_files'
/Users/stormbreaker/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rubocop-0.36.0/lib/rubocop/runner.rb:57:in `each'
/Users/stormbreaker/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rubocop-0.36.0/lib/rubocop/runner.rb:57:in `inspect_files'
/Users/stormbreaker/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rubocop-0.36.0/lib/rubocop/runner.rb:35:in `run'
/Users/stormbreaker/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rubocop-0.36.0/lib/rubocop/cli.rb:30:in `run'
/Users/stormbreaker/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rubocop-0.36.0/bin/rubocop:14:in `block in <top (required)>'
/Users/stormbreaker/.rbenv/versions/2.3.0/lib/ruby/2.3.0/benchmark.rb:308:in `realtime'
/Users/stormbreaker/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/rubocop-0.36.0/bin/rubocop:13:in `<top (required)>'
/Users/stormbreaker/.rbenv/versions/2.3.0/bin/rubocop:23:in `load'
/Users/stormbreaker/.rbenv/versions/2.3.0/bin/rubocop:23:in `<main>'

Looking at a serialized offence, the severity seems to not be serialized as a string, but as an object:

[{"severity":{"name":"convention"},"location":{"begin_pos":24672,"end_pos":25005},"message":"Use 2 spaces for indentation in an array, relative to the first position after the preceding left parenthesis.","cop_name":"Style/IndentArray","status":"uncorrected"}, <omitted>]

Removing the cache directory allows it to run properly once since there is no cache to use.

I am still unsure wether this is caused by something in my environment or is a more general problem. The 0.34.2 version was working as expected without this problem.

@georgyangelov
Copy link
Contributor Author

Turns out this is caused by the Oj gem and the way it handles serialization of 'plain' objects. The normal json lib calls #to_s, but Oj (by default) converts the object to a hash using its instance methods. The latter can be configured but there is no option to call #to_s as a last resort.

[1] pry(main)> class A
[1] pry(main)*   def initialize
[1] pry(main)*     @name = "test"
[1] pry(main)*   end  
[1] pry(main)*   
[1] pry(main)*   def to_s
[1] pry(main)*     @name
[1] pry(main)*   end  
[1] pry(main)* end  
=> :to_s
[2] pry(main)> require 'json'
=> true
[3] pry(main)> A.new.to_json
=> "\"test\""
[4] pry(main)> require 'oj'
=> true
[5] pry(main)> require 'oj_mimic_json'
=> true
[6] pry(main)> A.new.to_json
=> "{\"name\":\"test\"}"

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

1 participant