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

Request failed: (nil) in Emacs #605

Closed
dmitryanufriev opened this issue Aug 17, 2015 · 15 comments
Closed

Request failed: (nil) in Emacs #605

dmitryanufriev opened this issue Aug 17, 2015 · 15 comments

Comments

@dmitryanufriev
Copy link

Hello,

I tried to use tern in emacs and got message "Request failed: (nil)" when edit file which contains non english characters.

For example,

error

but if editable file contains only english characters then all works fine

no-error

In what can be a reason?

Emacs: GNU Emacs 25.0.50.1 (x86_64-pc-linux-gnu, GTK+ Version 3.10.8) of 2015-05-19 on lcy01-07
OS: Linux Mint 17.2 Cinnamon

P.S. Sorry for my english, I used Google Translate...

marijnh added a commit that referenced this issue Aug 18, 2015
Who knows what that means, but without it, we get byte soup
when reading a non-ascii response.

Issue #605
@marijnh
Copy link
Member

marijnh commented Aug 18, 2015

I can't reproduce the error message you get ('Request failed: (nil)'), but if I put some cyrillic text into a buffer, I get other errors. I've fixed those in the attached patch. Could you try whether it works for you? If not, could you give me the exact file you are using, and make sure you're on the current version of Tern?

@dmitryanufriev
Copy link
Author

Thank you for reply!

I tried your solution, but got same result - 'Request failed: (nil)'.

What I did:

  1. Got new repository clone via git clone.
  2. Replaced tern.el and tern-auto-complete.el in my emacs packages directory. I use cask for manage packages for Emacs.
  3. Replaced /bin/tern in /usr/lib/node_modules/tern with new one.
  4. Performed the same sequence of actions (opened file and typed 'привет').
  5. Got 'Request failed: (nil)'.

After that, I tried to understand what was going on and found that the error occurs during parsing JSON request in /bin/tern (line 220). I insert a bit logging and look that comes to the server.

error-request

As you can see that at the end of the string with cyrillic missing curly bracket and JSON.pase method throws exception. I do not understand what could be the cause of this error.

And about second question, I'm not quite sure what file I need to give you?

@dmitryanufriev
Copy link
Author

I tried to use plugin for gedit and it works fine with cyrillic. So I can assume that the problem is hidden in tern.el.

@marijnh
Copy link
Member

marijnh commented Aug 19, 2015

Interesting. If I follow those steps, I get string-based completions now.

Are you on Linux? If so, what is your $LANG environment variable? (I.e. is UTF-8 your default encoding?)

Have you tried starting your own Tern server and looking at what it does? (Go to the directory where your file is, maybe killall node to make sure that the old Tern server is gone, and then start tern --verbose before going back to emacs and trying the completion again.)

@dmitryanufriev
Copy link
Author

Yes, I am on Linux, Ubuntu 14.03.
echo $LANG gives me ru_RU.UTF-8.

Yes, I tried starting my own Tern server with --verbose command line argument and --port argument with value from .tern-port. So my Emacs connects with started Tern server and I get output from screenshot above (in my previous comment) - Response: 400 JSON parse error: Unexpected end of input. This occurs because the missing curly brace at the end of JSON request (JSON.parse in /bin/tern line 220).

I checked that no other Tern server running (via ps aux | grep node).

@marijnh
Copy link
Member

marijnh commented Aug 19, 2015

I think I see what you mean. Looks like Emacs' HTTP request is somehow cut off, probably by some multibyte-character-related bug. The question then, is why my emacs isn't doing the same thing. Might be an emacs 25 bug (I'm using 24.5.1). Do you still have an emacs 24 on your system? Can you try with that?

@marijnh
Copy link
Member

marijnh commented Aug 19, 2015

(Also, to test the byte-length vs character-length bug theory, did you test whether more characters are missing from the JSON when there are more multibyte characters in the request?)

@dmitryanufriev
Copy link
Author

I think I see what you mean

Sorry for my english...

I checked with Emacs 24.5.1 and all works fine. So, I can conclude that this is a bug in Emacs 25.

@marijnh
Copy link
Member

marijnh commented Aug 19, 2015

Okay. If you want to submit a bug with the emacs people (this is probably very easy to reproduce outside of Tern), that would probably be the best way to get this addressed.

@marijnh marijnh closed this as completed Aug 19, 2015
@dgutov
Copy link
Contributor

dgutov commented Aug 21, 2015

The main difference in json.el in Emacs 25 you may be triggering is that it doesn't hex-encode every multibyte character. Just the ones it really has to. So "привет" comes through unchanged.

@marijnh
Copy link
Member

marijnh commented Aug 21, 2015

Okay, so then the lack of encoding in the JSON layer is triggering another issue in the way we send the request (or in the way the url library handles it). @dgutov Do you know enough about emacs to debug why the server would be receiving truncated bodies when it is sent non-ascii data?

@dgutov
Copy link
Contributor

dgutov commented Aug 21, 2015

Not truncated, apparently.

This fixes it for me:

diff -u /home/gutov/.emacs.d/backups/\!home\!gutov\!.emacs.d\!elpa\!tern-20150818.317\!tern.el.\~1\~ /home/gutov/.emacs.d/elpa/tern-20150818.317/tern.el
--- /home/gutov/.emacs.d/backups/!home!gutov!.emacs.d!elpa!tern-20150818.317!tern.el.~1~    2015-08-21 13:00:23.717642400 +0300
+++ /home/gutov/.emacs.d/elpa/tern-20150818.317/tern.el 2015-08-21 13:15:48.157667156 +0300
@@ -26,7 +26,7 @@
   (let* ((url-mime-charset-string nil) ; Suppress huge, useless header
          (url-request-method "POST")
          (deactivate-mark nil) ; Prevents json-encode from interfering with shift-selection-mode
-         (url-request-data (json-encode doc))
+         (url-request-data (encode-coding-string (json-encode doc) 'utf-8))
          (url-show-status nil)
          (url (url-parse-make-urlobj "http" nil nil tern-server port "/" nil nil nil)))
     (url-http url #'tern-req-finished (list c))))

Diff finished.  Fri Aug 21 13:17:13 2015

marijnh added a commit that referenced this issue Aug 21, 2015
@marijnh
Copy link
Member

marijnh commented Aug 21, 2015

Thanks, that looks sensible. Attached patch includes it.

@dgutov
Copy link
Contributor

dgutov commented Aug 21, 2015

Thanks.

Although if url-http really truncates request body upon encountering multibyte characters, that would be a good cause for a separate bug report.

@dmitryanufriev
Copy link
Author

Thank you very much!

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

3 participants