Skip to content

Introduce JSON-RPC - #753

Closed
vjeux wants to merge 1 commit into
prettier:masterfrom
vjeux:jsonrpc
Closed

vjeux wants to merge 1 commit into
prettier:masterfrom
vjeux:jsonrpc

Conversation

@vjeux

@vjeux vjeux commented Feb 20, 2017 •

Copy link
Copy Markdown
Contributor

In order to be avoid the startup cost of prettier which makes editor integrations feel very slow, we can keep the process alive and communicate via JSON-RPC. This is a bit more work to do on the integration side but enables <50ms response time instead of >300ms, which makes a huge difference in practice.

I'm using vscode-jsonrpc implementation which has been battle-tested inside of vscode and doesn't have any dependencies.

Fixes #730

In order to be avoid the startup cost of prettier which makes editor integrations feel very slow, we can keep the process alive and communicate via JSON-RPC. This is a bit more work to do on the integration side but enables <50ms response time instead of >300ms, which makes a huge difference in practice.

I'm using vscode-jsonrpc implementation which has been battle-tested inside of vscode and doesn't have any dependencies.
Comment thread test-json-rpc.js
connection.sendRequest('format', 'x(;)').then(({error, formatted}) => {
console.log(error);
})
]).then(() => {

@vjeux vjeux Feb 20, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$ node test-json-rpc.js
(x => {
  x();
});

SyntaxError: Unexpected token (1:2)
> 1 | x(;)
    |   ^

@jlongster

Copy link
Copy Markdown
Member

This looks cool. A few questions:

  • This spawns a long-running process in the background, right? Does it stay up until the computer restarts? This transparently handles all the ugliness of starting something back up if it crashed, etc?

  • We should ship a prettier client already as well. Is there any way we could have a prettier --server command that does two things: checks if an existing server is running, and if not, starts one up. Then it sends code to format to the server and prints the result.

That way users never have to worry about client/servers, which argument to use, etc. If you use --server it makes sure to use server mode, that's all. You don't need --json-rpc and then a different client, or a different argument like prettier --client. What do you think?

I want to just tell Emacs one command to run and have everything work.

@jlongster

Copy link
Copy Markdown
Member

Also, embedding the client and making it all work transparently allows us to change the RPC as we wish. I don't really want to encourage people to access it directly so we don't have to worry about backwards compatibility.

One thing I'd like to do eventually is have the server keep a map of absolute filenames to modification times, and it would only re-prettify files that have actually changed. This lets you send an entire project to prettier and it will be super-fast and only format files that changed. We'll need to chance the RPC to do that though.

@vjeux

vjeux commented Feb 21, 2017

Copy link
Copy Markdown
Contributor Author

I want to just tell Emacs one command to run and have everything work.

That would be ideal, but it's not very realistic unfortunately if we are working in JavaScript. Just starting node takes 40ms, which is longer than running prettier on most files.

I've been playing around different latencies and it's crazy how it makes a difference. If you are more than 200ms, you just run it when you are done coding. But if it's <50ms, you run it when you finish writing an expression.

I believe that if we want people to not care about formatting their code anymore, we absolutely need that super low latency.

@vjeux

vjeux commented Feb 21, 2017

Copy link
Copy Markdown
Contributor Author

This spawns a long-running process in the background, right?

This PR doesn't run anything. This enable prettier to be ran as a child process and communicate with it via stdin/out and json-rpc.

It is up to the editor integration to define its policy regarding how long it wants to keep the client alive, when to kill it... What jest does is to have a separate jest-editor-support ( https://github.com/facebook/jest/tree/master/packages/jest-editor-support ) package that implements this and can be used for editors that have JavaScript runtimes like atom and vscode.

@vjeux

vjeux commented Feb 21, 2017

Copy link
Copy Markdown
Contributor Author

Is there any way we could have a prettier --server command that does two things: checks if an existing server is running, and if not, starts one up. Then it sends code to format to the server and prints the result.

This is what eslint_d does but unfortunately because it is written in JavaScript, it doesn't have the kind of latency that I'm looking for. I'm not opposed writing it in another language but it's likely going to be a compiled language and therefore very annoying to deploy. Maybe bash would be okay, but then wouldn't work on windows.

@vjeux

vjeux commented Feb 21, 2017

Copy link
Copy Markdown
Contributor Author

One thing I'd like to do eventually is have the server keep a map of absolute filenames to modification times

watchman provides this feature fyi. You can get a clock when you run prettier and then use the since command to get all the changed files since that clock.

@vjeux

vjeux commented Feb 21, 2017

Copy link
Copy Markdown
Contributor Author

An empty bash file takes 5ms to run on my computer :(

@jlongster

Copy link
Copy Markdown
Member

I've been playing around different latencies and it's crazy how it makes a difference. If you are more than 200ms, you just run it when you are done coding. But if it's <50ms, you run it when you finish writing an expression.

I agree with the sentiment but I think this is a little strong. I've been using it in Emacs with the normal prettier command which restarts everything each time. It's not really a huge problem. My client is using it too and I've seen many other people use it this way while it's noticeable sometimes, most people are fine if it takes ~100-200ms. I run it with a keystroke after every expression and it feels good.

It is up to the editor integration to define its policy regarding how long it wants to keep the client alive, when to kill it... What jest does is to have a separate jest-editor-support ( https://github.com/facebook/jest/tree/master/packages/jest-editor-support ) package that implements this and can be used for editors that have JavaScript runtimes like atom and vscode.

The problem with this is it makes it really hard to simply use from the CLI. There's that issue about using watchman (or some watcher) from the CLI and passing a command like prettier --write "src/**/*.js to format the whole project. I want to allow people to just add a --server command and it'll automatically run a long-running process and only format files that have changed.

I'm not sure I love pushing the complexity into every single editor integration.

I don't really understand what jest-editor-support does either, if editors have a JS runtime they are going to load in prettier once as a package and use the API anyway. Those editors don't need the server functionality at all.

@aaronjensen

Copy link
Copy Markdown

This is what eslint_d does but unfortunately because it is written in JavaScript, it doesn't have the kind of latency that I'm looking for.

Yes, but it also supports directly writing to the port: https://github.com/mantoni/eslint_d.js#moar-speed

This gives you both options--if you're ok w/ the 50ms node startup time you can just use it as one command. If you want more speed you can write directly to the port either via bash or your editor if it supports it.

@vjeux

vjeux commented Feb 25, 2017

Copy link
Copy Markdown
Contributor Author

I've setup a separate project for this: https://github.com/vjeux/prettier-rpc

We'll likely want to integrate it in the official project at some point but it should be easier to iterate on it outside of the main repo for now.

@vjeux vjeux closed this Feb 25, 2017
@jlongster

Copy link
Copy Markdown
Member

👍 Sounds like a good plan, since it's easy-ish to do separately it would be good to have an existing workflow set up that we can talk about, and you can do whatever you want for now. Thanks for doing all the research!

@vjeux vjeux mentioned this pull request Mar 8, 2017
@josephfrazier

Copy link
Copy Markdown
Collaborator

I've just started working on an eslint_d fork that uses prettier instead. It's still a work-in-progress, but I've published a functional version already: https://github.com/josephfrazier/prettier_d.js

josephfrazier added a commit to josephfrazier/prettier that referenced this pull request Apr 18, 2017
`prettier_d` is like [eslint_d], but it runs `prettier` instead of
`eslint`. This eliminates the Node.js startup delay from all but the
first run of `prettier_d`, making it a snappier experience.

[eslint_d]: https://github.com/mantoni/eslint_d.js

Related discussion:
* prettier#575 (comment)
* prettier#918 (comment)
* prettier#753 (comment)
vjeux pushed a commit that referenced this pull request Apr 18, 2017
`prettier_d` is like [eslint_d], but it runs `prettier` instead of
`eslint`. This eliminates the Node.js startup delay from all but the
first run of `prettier_d`, making it a snappier experience.

[eslint_d]: https://github.com/mantoni/eslint_d.js

Related discussion:
* #575 (comment)
* #918 (comment)
* #753 (comment)
@lock lock Bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jan 20, 2019
@lock lock Bot locked as resolved and limited conversation to collaborators Jan 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants