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

Force UTF-8 encoding on JSON Podspecs #629

Merged

Conversation

jasonschroeder-sfdc
Copy link
Contributor

Write the file from CDN to disk without any conversions.
Some response_body are coming back with encoding=ASCII-8BIT but are not writable to
disk, with errors like this:

[!] CDN: trunk Repo update failed - 31 error(s):
"\xE2" from ASCII-8BIT to UTF-8

(second line repeated 31 times)

Environment:

  • Cocoapods 1.9.1
  • Ruby 2.5.3, and Ruby 2.7.1 both via rvm
  • MacOS 10.14.6
    Env vars:
  • LANG=en_US.UTF-8

Here is the stacktrace from pod install --verbose

$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-core-1.9.1/lib/cocoapods-core/cdn_source.rb:479:in `rescue in concurrent_requests_catching_errors'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-core-1.9.1/lib/cocoapods-core/cdn_source.rb:474:in `concurrent_requests_catching_errors'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-core-1.9.1/lib/cocoapods-core/cdn_source.rb:121:in `versions'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-core-1.9.1/lib/cocoapods-core/specification/set.rb:99:in `block in versions_by_source'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-core-1.9.1/lib/cocoapods-core/specification/set.rb:98:in `each'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-core-1.9.1/lib/cocoapods-core/specification/set.rb:98:in `each_with_object'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-core-1.9.1/lib/cocoapods-core/specification/set.rb:98:in `versions_by_source'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-core-1.9.1/lib/cocoapods-core/specification/set.rb:56:in `specification_name'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-core-1.9.1/lib/cocoapods-core/cdn_source.rb:216:in `search'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-core-1.9.1/lib/cocoapods-core/source/aggregate.rb:83:in `block in search'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-core-1.9.1/lib/cocoapods-core/source/aggregate.rb:83:in `select'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-core-1.9.1/lib/cocoapods-core/source/aggregate.rb:83:in `search'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-1.9.1/lib/cocoapods/resolver.rb:416:in `create_set_from_sources'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-1.9.1/lib/cocoapods/resolver.rb:385:in `find_cached_set'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-1.9.1/lib/cocoapods/resolver.rb:360:in `specifications_for_dependency'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-1.9.1/lib/cocoapods/resolver.rb:165:in `search_for'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-1.9.1/lib/cocoapods/resolver.rb:274:in `block in sort_dependencies'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-1.9.1/lib/cocoapods/resolver.rb:267:in `each'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-1.9.1/lib/cocoapods/resolver.rb:267:in `sort_by'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-1.9.1/lib/cocoapods/resolver.rb:267:in `sort_dependencies'
$HOME/.rvm/gems/ruby-2.5.3/gems/molinillo-0.6.6/lib/molinillo/delegates/specification_provider.rb:53:in `block in sort_dependencies'
$HOME/.rvm/gems/ruby-2.5.3/gems/molinillo-0.6.6/lib/molinillo/delegates/specification_provider.rb:70:in `with_no_such_dependency_error_handling'
$HOME/.rvm/gems/ruby-2.5.3/gems/molinillo-0.6.6/lib/molinillo/delegates/specification_provider.rb:52:in `sort_dependencies'
$HOME/.rvm/gems/ruby-2.5.3/gems/molinillo-0.6.6/lib/molinillo/resolution.rb:288:in `initial_state'
$HOME/.rvm/gems/ruby-2.5.3/gems/molinillo-0.6.6/lib/molinillo/resolution.rb:210:in `start_resolution'
$HOME/.rvm/gems/ruby-2.5.3/gems/molinillo-0.6.6/lib/molinillo/resolution.rb:168:in `resolve'
$HOME/.rvm/gems/ruby-2.5.3/gems/molinillo-0.6.6/lib/molinillo/resolver.rb:43:in `resolve'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-1.9.1/lib/cocoapods/resolver.rb:94:in `resolve'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-1.9.1/lib/cocoapods/installer/analyzer.rb:1065:in `block in resolve_dependencies'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-1.9.1/lib/cocoapods/user_interface.rb:64:in `section'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-1.9.1/lib/cocoapods/installer/analyzer.rb:1063:in `resolve_dependencies'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-1.9.1/lib/cocoapods/installer/analyzer.rb:124:in `analyze'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-1.9.1/lib/cocoapods/installer.rb:410:in `analyze'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-1.9.1/lib/cocoapods/installer.rb:235:in `block in resolve_dependencies'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-1.9.1/lib/cocoapods/user_interface.rb:64:in `section'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-1.9.1/lib/cocoapods/installer.rb:234:in `resolve_dependencies'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-1.9.1/lib/cocoapods/installer.rb:156:in `install!'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-1.9.1/lib/cocoapods/command/install.rb:52:in `run'
$HOME/.rvm/gems/ruby-2.5.3/gems/claide-1.0.3/lib/claide/command.rb:334:in `run'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-1.9.1/lib/cocoapods/command.rb:52:in `run'
$HOME/.rvm/gems/ruby-2.5.3/gems/cocoapods-1.9.1/bin/pod:55:in `<top (required)>'
$HOME/.rvm/gems/ruby-2.5.3/bin/pod:23:in `load'
$HOME/.rvm/gems/ruby-2.5.3/bin/pod:23:in `<main>'
$HOME/.rvm/gems/ruby-2.5.3/bin/ruby_executable_hooks:24:in `eval'
$HOME/.rvm/gems/ruby-2.5.3/bin/ruby_executable_hooks:24:in `<main>'

Some sample "bad" JSON Podspecs:

@dnkoutso
Copy link
Contributor

Do you think this is related to CocoaPods/CocoaPods#9672?

@jasonschroeder-sfdc
Copy link
Contributor Author

I've done my best to follow the CONTRIBUTING guide from CocoaPods/CONTRIBUTING.md.

These are likely missing before this is mergeable:

  • unit test. Can you offer any guidance? Ruby isn't my most comfortable language
  • a note in the CHANGELOG.md

@jasonschroeder-sfdc
Copy link
Contributor Author

Hi @dnkoutso 👋
Yes, if pod install bombs, then a zero-length JSON file is left in ~/.cocoapods/repos/trunk/... and then I see the same behavior as CocoaPods/CocoaPods#9672

As the JSON podspec exists already, pod update doesn't seem to know it's stale. rming the zero-length file(s) (or everything, rm -rf ~/.cocoapods/repos/) seems to get back into a better state.

@dnkoutso
Copy link
Contributor

awesome. I can guide you tomorrow (PST timezone here) on how to add the test. Can you give access in this PR for me to push? If not I can open a new PR and ensure you receive credit for the fix of course in the changelog entry.

@jasonschroeder-sfdc
Copy link
Contributor Author

Amazing. Thanks so much. I'm happy to make changes with a little guidance.
And I just gave access in this PR for you to push.

Have a good evening!

@dnkoutso
Copy link
Contributor

cc @igor-makarov

@igor-makarov
Copy link
Contributor

Have you managed to figure out why the response encoding is reported as ASCII when the header is Content-Type: text/plain; charset=utf-8?

@jasonschroeder-sfdc
Copy link
Contributor Author

@igor-makarov I haven't figured that out. In a simple Podfile, this issue isn't reproducible. But in our production Podfile, which has a lot more..diversity, it does appear on initial pod install when '~/.cocoapods/reposis empty. The productionPodfile` sources two private repos and the CDN trunk. It also has relative path Pods. I suspect some previous pod is mistakenly altering state but haven't found the smoking gun.

@igor-makarov
Copy link
Contributor

I did some additional checking and it appears that this is a Typhoeus bug - in fact, they say that a lot of HTTP libs make this mistake and disregard the charset header.

I think using force_encoding on the response to force the responce body to be treated as UTF-8 might be a better idea. After all, we do know for sure that the files are UTF-8.

@jasonschroeder-sfdc
Copy link
Contributor Author

jasonschroeder-sfdc commented May 13, 2020 via email

@igor-makarov
Copy link
Contributor

I saw the new fix, seems good. 👍🏻

I'll write an explainer on how to add the unit tests tomorrow, as I'm in GMT+2 and it's nighttime here...

@igor-makarov
Copy link
Contributor

igor-makarov commented May 13, 2020

Perhaps the PR title needs to be changed?

@dnkoutso
Copy link
Contributor

we could actually ship 1.9.2 here with this fix. If we want that we should change destination branch to 1-9-stable.

@jasonschroeder-sfdc jasonschroeder-sfdc changed the title Always use binary mode when writing JSON Podspecs Force UTF-8 encoding on JSON Podspecs May 13, 2020
@jasonschroeder-sfdc
Copy link
Contributor Author

Title updated. Let me know if you'd like these commits squashed.

@igor-makarov
Copy link
Contributor

I think the 1st commit can be squashed into the 2nd.

@igor-makarov
Copy link
Contributor

As for unit testing, in spec/cdn_source_spec.rb:60 there's a helper method called typhoeus_http_response_future that accepts a body string, among other things.

You can use that method to stub Typhoeus.

To check that force_encoding is called, you could stub the String that you pass as the body.

@dnkoutso
Copy link
Contributor

Please point to 1-9-stable branch also. I will make a 1.9.2 release for this as we have gotten many reports regarding this issue.

@dnkoutso dnkoutso closed this May 14, 2020
@dnkoutso dnkoutso reopened this May 14, 2020
@dnkoutso
Copy link
Contributor

(accidental close sorry)

@jasonschroeder-sfdc jasonschroeder-sfdc changed the base branch from master to 1-9-stable May 14, 2020 16:53
There is a bug in Typhoeus. See
typhoeus/typhoeus#580
This will force the response_body to always be UTF-8.

Some response_body are coming back with encoding=ASCII-8BIT but are not writable to
disk, with errors like this:

```
[!] CDN: trunk Repo update failed - 31 error(s):
"\xE2" from ASCII-8BIT to UTF-8
```
Adding PR-629 to the CHANGELOG.md.
Added a unit test for confirming that "force_encoding" is invoked on the
Typhoeus response body.
@jasonschroeder-sfdc
Copy link
Contributor Author

@dnkoutso: Updated the base to 1-9-stable branch, added a unit test, and squashed two commits into one. Ready for your final review, I hope! 🤞

@dnkoutso
Copy link
Contributor

@igor-makarov want to take a quick look before I press merge?

@igor-makarov
Copy link
Contributor

Looks good to me @dnkoutso !

@dnkoutso dnkoutso merged commit ab6d74d into CocoaPods:1-9-stable May 14, 2020
@jasonschroeder-sfdc jasonschroeder-sfdc deleted the cdn_source_encoding branch May 14, 2020 18:53
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.

None yet

3 participants