rust-lang / rust-lang/rust-analyzer

Report rustfmt errors to the user, and use partial formatting even if there was an error

Open
#10,752 3 comments 10 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-feature S-actionable
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:

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.