rust-lang / vscode-rust

Rust extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=rust-lang.rust
Other
1.39k stars 167 forks source link

Format not work #534

Open aleksanderd opened 5 years ago

aleksanderd commented 5 years ago

Hi!

I can not get vscode to format code, the error is:

[Trace - 9:32:34 PM] Received response 'textDocument/formatting - (15)' in 0ms. Request failed: Reformat failed to complete successfully (-32603).
[Error - 9:32:34 PM] Request textDocument/formatting failed.
  Message: Reformat failed to complete successfully
  Code: -32603 

rustftm via cmd formats same file ok.

what to check or configure?

thank you!

rustc 1.33.0 (2aa4c46cf 2019-02-28) rustfmt 1.0.1-stable (be13559 2018-12-10) cargo 1.33.0 (f099fe94b 2019-02-12) rustup 1.17.0 (069c88ed6 2019-03-05)

gitowiec commented 5 years ago

I just started learning Rust with VSCode and rls-vscode plugin and got the same error. To check what is the error message, click View > Open View and write Output (panel) then press Enter. Running Ubuntu Budgie 18.04.2 LTS rustc 1.33.0 (2aa4c46cf 2019-02-28) rustfmt 1.0.1-stable (be13559 2018-12-10) cargo 1.33.0 (f099fe94b 2019-02-12) rustup 1.17.0 (069c88ed6 2019-03-05)

Xanewok commented 5 years ago

Unfortunately, this is an issue with RLS itself. That's high on the list so hopefully this will be soon resolved

zombiezen commented 5 years ago

Is the upstream issue https://github.com/rust-lang/rls/issues/1397?

iambudi commented 5 years ago

Any update about this issue?

Dan-True commented 5 years ago

I don't get an error, but I format does nothing on my end either

Licenser commented 5 years ago

I got the same error.

fopsdev commented 5 years ago

Hi there

Issue a "cargo fmt" from within your project dir. This will give more information why formatting can not be used. In my case my toolchain missed the cargo-fmt.exe. after that it worked from commandline and from within vscode

cisaacson commented 5 years ago

I have the same issue and "cargo fmt" works fine, no error when I run from the command line. It used to work until very recently within VSCode too, now I get the error reported above every time.

lukad commented 5 years ago

Same here, cargo fmt works but running Format Document gives me this error:

[Error - 4:45:32 PM] Request textDocument/formatting failed.
  Message: Reformat failed to complete successfully
  Code: -32603

I am using

rustc 1.37.0 (eae3437df 2019-08-13)
rustfmt 1.3.0-stable (d3345024 2019-06-09)
cargo 1.37.0 (9edd08916 2019-08-02)

I also noticed that the RLS [building] spinner in vscode's status bar never changes and there is no output except the above error when I save.

cisaacson commented 5 years ago

I have one machine running VS Code 1.37 that works properly, and another running 1.38.1 that does not work, so it looks like it was a regression from the earlier issue.

Soreine commented 5 years ago

A simple warning for the fellow newbies who are just starting to learn Rust like me. It seems that you need a Cargo.toml file in your project so that rls-vscode formats your code.

vtenfys commented 4 years ago

It seems that you need a Cargo.toml file in your project

Not working for me even with a Cargo.toml file :( I'm using VS Code 1.40.2 on macOS 10.15 - not getting any output displayed or logged when I run format, though nothing is happening, and using rustfmt from the command line works fine.

Edit: working after all - Prettier was being used incorrectly due to being set as the default formatter in my VS Code settings

Skasselbard commented 4 years ago

I get the same error when rustfmt fails. In my case this seems to be related to this issue from rustfmt. In particular this code cause rustfmt to fail:

match var.name{
    name if 
        //BAD COMMENT causes fmt error
        name.contains("smth")
        // other comment
        | name.contains("smth else") => {true}
    _ => false,
}

The caused rls error is quite silent. Initially I checked the settings and updated rust with rustup (which was of no help). Manualy formating with rustfmt leads to this error (hence the link to the rustfmt issue):

rustfmt /path/to/file.rs
error[internal]: left behind trailing whitespace
   --> /path/to/file.rs:157:157:19
    |
157 |             name if 
    |                    ^
    |

warning: rustfmt has failed to format. See previous 1 errors.
tifrel commented 3 years ago

Since I do no longer expect this to get fixed, I propose to install statiolake.vscode-rustfmt. So far, I have no compatibility issues with the official rust extension, and get format on save :)

lnicola commented 3 years ago

Using rust-analyzer (the stand-alone extension) should also work.

DriesVerb commented 2 years ago

If your rust-analyzer does not work try adding this to your settings.json

"[rust]": { "editor.defaultFormatter": "rust-lang.rust-analyzer" },