vscode-rust
vscode-rust copied to clipboard
Blank lines are removed when saving
Running on Win 10, VS Code 1.38, Extension version 0.6.3.
This behavior seems to be consistent, but I haven't figured out when it does or doesn't occur. The auto formatter seems to break on saving new blank lines. I'm intentionally not saying rustfmt because it doesn't appear to be at fault here. This behavior does not occur when I use rustfmt from the command line. Take the following example:
//other code
println!("line a");
println!("line b");
//other code
If I insert a new line between line a and line b and immediately save the file, the new line will be removed. This behavior will also occur if I save without formatting, then immediately save after. If I enter 2 or more new lines, this doesn't happen.
Things I've tried:
- Specifying path for rustfmt (latest stable).
- Closing/reopening file after a "save without formatting" - it still occurs
- Changing the upper bound for blank lines
- Putting a blank line between two other lines of code - both blank lines will be removed.
Workarounds:
- Insert two or more blank lines.
- Enter another line of code elsewhere. Oddly, this behavior only seems to occur if the blank line/lines were the most recent additions to the file. This doesn't happen otherwise.
- Use rustfmt from the command line.
Has anyone else seen this? It could be a configuration thing, but if so, I'm not sure where. I'm using defaults. The only other formatter that I have installed is Prettier, which shouldn't be touching .rs files.
I'm seeing the same issue. macOS 10.15, VS Code 1.39, RLS extension version 0.6.3. The behavior is roughly as you described. I was going through some of my code, adding single empty lines for clarity, and format-on-save kept removing them.
For what it's worth, I don't have any other formatting extensions installed in VS Code.
Same (also Win10)
I have seen this problem too, except when the blank line is just a new line
For Nokel's comment, that's likely what I'm seeing, too.
Emacs with rust-lsp and lsp-mode has been exhibiting similar behavior lately. Maybe it's a rust language server issue?
This is occurring with VSCode for me as well. At first I thought it might be an extraneous extension that was causing the issue. I do have Prettier installed. I tried disabling all extensions that might format code, and the issue still occurs. I'm glad to see people using other editors reporting this. It helps to rule out extraneous VSCode extensions formatting files as the issue.
- Windows 10
- VSCode v1.42.1
- VSCode Rust (rls) 0.7.0
If people are seeing this behavior in both VS Code and Emacs, but not when running rustfmt from the command line, then I suppose RLS is the common factor. Perhaps someone should open an issue in that repository…
Confirmed that when I press Enter at the end of the "line a" and run "Format document", the blank line is removed.
fn main() {
println!("line a");
println!("line b");
}
If I press ENTER multiple times and format document, then only one blank line remains.
Tested with rust-lang.rust 0.7.8
and VSCode 1.57.0 on Ubuntu 21.04
Related RLS issue: https://github.com/rust-lang/rls/issues/1642
Someone in https://github.com/rust-lang/atom-ide-rust/issues/146#issuecomment-626652541 says that rust-analyzer
doesn't have that issue. However I cannot get working rust-analyzer in VSCode (as Rust-client Engine), I'm getting this when downloading a package:
[2021-06-13 21:26:37.109] [exthost] [error] Error: Bad release: {"url":"https://api.github.com/repos/rust-analyzer/rust-analyzer/releases/44527998"
...
@mirao please try the standalone rust-analyzer
extension instead.
@lnicola rust-analyzer
extension looks promising and code formatting doesn't suffer from this bug, thanks.
I found when I print one blank line and format the code then this line will be removed, but when I print >=2
blank lines and format the code then will be keep one blank line.
Platform: Darwin v21.1.0
Tools: VS Code v1.63.2
Plugin: Rust v0.7.8