Currently, rust-analyzer always assumes that rustfmt exiting with error is an uninteresting syntax error report, and skips reformatting the file. This behavior is suboptimal in at least two cases:
When rustfmt hits one of the “left behind trailing whitespace” situations, it partially formats the file and reports the situation. But rust-analyzer is calling rustfmt by pipe, and does nothing if the exit code is not success, so the partially formatted output is discarded, and the user has no way of seeing that there is an error. If this error is shown to the user, they can manually delete the offending whitespace and continue; as it is, they are not aware of it until they run a command line cargo fmt or notice that one of their files is not getting reformatted on save.
Use the formatted output instead of discarding the result (since it's easier to manually fix problems if the autoformatter has taken care of what it can).
Tell the user that there's a problem.
Given the stated rationale of ... otherwise an error is surfaced to the user on top of the syntax error diagnostics they're already receiving ..., perhaps a good compromise would be to report rustfmt errors only if there are no other errors.
It also might be necessary to modify rustfmt to give distinct exit codes for “could not parse” and “parsed and formatted but with errors/warnings”. It might help to give rustfmt a file rather than pipes. I'm not familiar with exactly what interfaces/behaviors rustfmt currently offers.
Currently, rust-analyzer always assumes that rustfmt exiting with error is an uninteresting syntax error report, and skips reformatting the file. This behavior is suboptimal in at least two cases:
cargo fmt
or notice that one of their files is not getting reformatted on save.In both of these cases, it would be better to
Given the stated rationale of
... otherwise an error is surfaced to the user on top of the syntax error diagnostics they're already receiving ...
, perhaps a good compromise would be to report rustfmt errors only if there are no other errors.It also might be necessary to modify
rustfmt
to give distinct exit codes for “could not parse” and “parsed and formatted but with errors/warnings”. It might help to give rustfmt a file rather than pipes. I'm not familiar with exactly what interfaces/behaviors rustfmt currently offers.