rust-lang / rust-lang/rust-analyzer
Report rustfmt errors to the user, and use partial formatting even if there was an error
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 16.9k
- Forks
- 2.2k
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 72
Description
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 fmtor notice that one of their files is not getting reformatted on save. - rustfmt can be configured to error intentionally when the input cannot be formatted to obey the intended limits, but does not contain a syntax error.
In both of these cases, it would be better to
- 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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in crates/rust-analyzer/src/handlers.rs around line 1781, where rustfmt is invoked through a pipe and non-success exits cause its output to be discarded. Trace how rustfmt output and errors reach the user, then check the rustfmt behavior and exit-status distinction described in the issue. Done means usable partial formatting is retained and relevant rustfmt errors are reported without masking existing syntax diagnostics.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- developer-experience, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100