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

Feature fix encoding fatal error #3000

Merged

Conversation

gerrywastaken
Copy link
Contributor

This was a tricky one to track down. When I was running rubocop against some code it would keep crashing with an encoding error but it wasn't clear why this was happening and seemed inconsistent.

It turns out that it would only occur when the offenses were read from cache as they came back as ASCII-8BIT encoded strings instead of UTF-8 encoded when the cache isn't used. The attempt to convert inside the erb highlighting helper was triggering the crash.

I squashed the commits down into 3 logical changes, but didn't want to squash it into a single commit as it would have made things harder to understand for anybody looking at the changes. It also would have made cherry picking more difficult if it became necessary. If you still would prefer me to squash it further, then I can.

Before submitting a PR make sure the following are checked:

  • Wrote good commit messages.
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it)
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests are passing.
  • The new code doesn't generate RuboCop offenses.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.

This test also demonstrates a bug "incompatible character encodings:
UTF-8 and ASCII-8BIT". This bug only occurs when one of the scanned
files contains a certain characters and the offense results are read
from cache.
@gerrywastaken gerrywastaken force-pushed the feature-fix-encoding-fatal-error branch 2 times, most recently from caecb22 to 9ee213d Compare April 4, 2016 17:58
This tests makes sure that loading from the cache returns utf8
encoded strings. Currently the encoding is being changed to
ASCII_8BIT and this can result in a fatal error for the end user.

This also modifies :location to match the pre cache-load
behaviour in lib/rubocop/processed_source.rb line 88.
If certain characters were encountered, RuboCop would crash during
the syntax highlighting stage. This would only happen when the
offense was read from cache.

When offenses are read from cache they come back with an ASCII-8BIT
encoding. Certain characters, such as curly quotes, cause an error
when there is an attempt to convert the string back to UTF-8 for
output to a HTML file.

This fix changes the cache serialization so that it generates
offenses in the same way as processed_source.rb, where the source
is assigned instead of being read directly from a file by Parser.
@gerrywastaken gerrywastaken force-pushed the feature-fix-encoding-fatal-error branch from 9ee213d to d529b43 Compare April 4, 2016 18:21
@bbatsov bbatsov merged commit 942a409 into rubocop:master Apr 9, 2016
@bbatsov
Copy link
Collaborator

bbatsov commented Apr 9, 2016

👍 Impressive work! I loved your attention to detail!

@gerrywastaken
Copy link
Contributor Author

Thanks mate and cheers for making RuboCop. :)

@ghost
Copy link

ghost commented May 7, 2016

Thanks for fixing this @gerrywastaken! I too was scratching my head trying to reproduce it to file #3099. Glad to see you were way ahead of me.

@gerrywastaken
Copy link
Contributor Author

@burnson Yeah I figured it must be affecting others. Glad this helped you. Good job tracking it down to cached results, that had me banging my head against a wall for quite some time.

@gerrywastaken gerrywastaken deleted the feature-fix-encoding-fatal-error branch May 7, 2016 12:22
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

Successfully merging this pull request may close these issues.

2 participants