Rust-analyzer flycheck blocked on cargo build

Hi, I am working on a web application written in Rust and use rust-analyzer with my editor. In development, I use cargo watch to automatically rebuild and rerun the web application whenever a source file changes.

When I save a file in my editor, rust-analyzer's flycheck runs cargo check at the same time that cargo watch invokes cargo run. This causes one or the other to block waiting for the other to finish. As a result, my iteration speed is cut in half.

Does anyone have recommendations for improving this setup?

One option I have considered is using separate target directories for each process. However, this is still doing double the typechecking. I think the ideal solution would be for rust-analyzer to build and run the web application and receive any compile errors encountered. Is this possible?

1 Like

You could set rust-analyzer.checkOnSave.command for the rust-analyzer plugin so it runs cargo check && cargo run every time you hit save (you might need to add --message-format=json or wrap it in a sh -c "...").

I believe rust-analyzer will scan the output from a command for diagnostics as they are generated instead of waiting for the command to complete. I assume when you trigger a new check it'll kill any previous checks, so you shouldn't end up with multiple servers running (@matklad feel free to correct me here).

1 Like

Setting the command to run would be a bad idea, since rust-analyzer does not expect the command to block indefinitely. Setting it to build should work though; you can continue to run cargo watch in the terminal, and whichever build starts second should just block and then immediately get cached results as soon as the other finishes.

1 Like

Thank you @Michael-F-Bryan and @fdiebold, that's a great idea.