mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Merge #7055
7055: Install rustfmt if needed during formatting request r=matklad a=kinnison In order to fix #6052 and to fix #4249 this PR installs `rustfmt` using `rustup` if `rustfmt --help` fails. In theory we ought to memoise the attempts (both the `--help` and the installation) so that we don't keep retrying something which will fail (e.g. if nightly is missing rustfmt), but this is a first-pass for opinions. In order to make it possible to notify the user of what happened, I added a facility for `GlobalStateSnapshot` to send *messages* to the client indicating what happened. There may be a cleaner way to do this as well but I wasn't sure exactly what might be best given this is my first time in this codebase. It may be worth, longer term, working with `rustup` to provide a way to detect a missing component binary since `rustup` returns `1` if `rustfmt` is not installed, which is not ideal. Co-authored-by: Daniel Silverstone <dsilvers@digital-scurf.org>
This commit is contained in:
commit
53e2cdf18e
1 changed files with 7 additions and 4 deletions
|
@ -861,16 +861,18 @@ pub(crate) fn handle_formatting(
|
|||
}
|
||||
};
|
||||
|
||||
let mut rustfmt = rustfmt.stdin(Stdio::piped()).stdout(Stdio::piped()).spawn()?;
|
||||
let mut rustfmt =
|
||||
rustfmt.stdin(Stdio::piped()).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn()?;
|
||||
|
||||
rustfmt.stdin.as_mut().unwrap().write_all(file.as_bytes())?;
|
||||
|
||||
let output = rustfmt.wait_with_output()?;
|
||||
let captured_stdout = String::from_utf8(output.stdout)?;
|
||||
let captured_stderr = String::from_utf8(output.stderr).unwrap_or_default();
|
||||
|
||||
if !output.status.success() {
|
||||
match output.status.code() {
|
||||
Some(1) => {
|
||||
Some(1) if !captured_stderr.contains("not installed") => {
|
||||
// While `rustfmt` doesn't have a specific exit code for parse errors this is the
|
||||
// likely cause exiting with 1. Most Language Servers swallow parse errors on
|
||||
// formatting because otherwise an error is surfaced to the user on top of the
|
||||
|
@ -886,8 +888,9 @@ pub(crate) fn handle_formatting(
|
|||
format!(
|
||||
r#"rustfmt exited with:
|
||||
Status: {}
|
||||
stdout: {}"#,
|
||||
output.status, captured_stdout,
|
||||
stdout: {}
|
||||
stderr: {}"#,
|
||||
output.status, captured_stdout, captured_stderr,
|
||||
),
|
||||
)
|
||||
.into());
|
||||
|
|
Loading…
Reference in a new issue