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

Treat null as undefined #86

Closed
felixfbecker opened this issue Sep 1, 2016 · 2 comments
Closed

Treat null as undefined #86

felixfbecker opened this issue Sep 1, 2016 · 2 comments

Comments

@felixfbecker
Copy link
Contributor

felixfbecker commented Sep 1, 2016

JavaScript is very special with the seperation between null and undefined. In many other languages such as PHP, if a property has not been set, it is null. Consequently, not-set properties will also JSON-encode to null. There is no way to find out if a property is null because it has been set to null or because the property was not set.

My PHP language server responds with this response to the initialize request:

Content-Type: application/vscode-jsonrpc; charset=utf8
Content-Length: 493

{
   "id":0,
   "result":{
      "capabilities":{
         "textDocumentSync":1,
         "hoverProvider":null,
         "completionProvider":null,
         "signatureHelpProvider":null,
         "definitionProvider":null,
         "referencesProvider":null,
         "documentHighlightProvider":null,
         "documentSymbolProvider":true,
         "workspaceSymbolProvider":null,
         "codeActionProvider":null,
         "codeLensProvider":null,
         "documentFormattingProvider":null,
         "documentRangeFormattingProvider":null,
         "documentOnTypeFormattingProvider":null,
         "renameProvider":null
      }
   },
   "error":null,
   "jsonrpc":"2.0"
}

For a human reading this, it is clear that the request was successful, result is defined and error is not defined. But the language client explicitely checks if error !== undefined here. This makes VS Code treat the request as a failed, and then fail with Cannot read property 'code' of null, because it tries to read the code property of the error.

It makes it impossible to implement the language server protocol with classes in any language that does not have a dedicated undefined type. Please just treat null as undefined.

value != undefined will be true for both null and undefined.

@dbaeumer
Copy link
Member

dbaeumer commented Sep 8, 2016

@felixfbecker we do treat undefined and null 'equal' in the protocol.I even think that I state somewhere that you should send null not undefined since undefined doesn't exist in other languages. On our server side we even have code that explicitly exchanges undefined with null. And the client code always treats undefined and null as 'equal'

However in the case you pointed out I explicitly tried to followed the JSON RPC spec which states:

result
   This member is REQUIRED on success.
   This member MUST NOT exist if there was an error invoking the method.
   The value of this member is determined by the method invoked on the Server.
error
   This member is REQUIRED on error.
   This member MUST NOT exist if there was no error triggered during invocation.
   The value for this member MUST be an Object as defined in section 5.1.

Would it be possible for your php json library to have two type of Response. FailedResponse and SuccessResponse.

@felixfbecker
Copy link
Contributor Author

Good to know you keep it in mind :)
I knew about the protocol, but I thought not following it strictly here would be more beneficial. But using subclasses is a better solution.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants