vscode-rust icon indicating copy to clipboard operation
vscode-rust copied to clipboard

Blank lines are removed when saving

Open BuckAMayzing opened this issue 4 years ago • 11 comments

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.

BuckAMayzing avatar Sep 12 '19 15:09 BuckAMayzing

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.

theodore-s-beers avatar Oct 11 '19 09:10 theodore-s-beers

Same (also Win10)

Lokathor avatar Nov 15 '19 03:11 Lokathor

I have seen this problem too, except when the blank line is just a new line

Nokel81 avatar Jan 14 '20 19:01 Nokel81

For Nokel's comment, that's likely what I'm seeing, too.

BuckAMayzing avatar Jan 21 '20 13:01 BuckAMayzing

Emacs with rust-lsp and lsp-mode has been exhibiting similar behavior lately. Maybe it's a rust language server issue?

bddap avatar Feb 12 '20 00:02 bddap

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

ZenSoftware avatar Feb 17 '20 04:02 ZenSoftware

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…

theodore-s-beers avatar Feb 17 '20 14:02 theodore-s-beers

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 avatar Jun 13 '21 19:06 mirao

@mirao please try the standalone rust-analyzer extension instead.

lnicola avatar Jun 14 '21 03:06 lnicola

@lnicola rust-analyzer extension looks promising and code formatting doesn't suffer from this bug, thanks.

mirao avatar Jun 14 '21 17:06 mirao

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

chioio avatar Jan 26 '22 17:01 chioio