Repository navigation
Conversation
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.
| connection.sendRequest('format', 'x(;)').then(({error, formatted}) => { | ||
| console.log(error); | ||
| }) | ||
| ]).then(() => { |
There was a problem hiding this comment.
$ node test-json-rpc.js
(x => {
x();
});
SyntaxError: Unexpected token (1:2)
> 1 | x(;)
| ^|
This looks cool. A few questions:
That way users never have to worry about client/servers, which argument to use, etc. If you use I want to just tell Emacs one command to run and have everything work. |
|
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. |
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. |
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 |
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. |
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. |
|
An empty bash file takes 5ms to run on my computer :( |
I agree with the sentiment but I think this is a little strong. I've been using it in Emacs with the normal
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 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. |
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. |
|
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. |
|
👍 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! |
|
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 |
`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)
`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)
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