Skip to content

Format on auto-saving doesn't work #3256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
djc opened this issue Feb 21, 2020 · 19 comments
Closed

Format on auto-saving doesn't work #3256

djc opened this issue Feb 21, 2020 · 19 comments
Labels
A-vscode vscode plugin issues C-support Category: support questions S-unactionable Issue requires feedback, design decisions or is blocked on other work

Comments

@djc
Copy link

djc commented Feb 21, 2020

I have formatOnSave enabled and autoSave set. Yet automatic saving does not seem to trigger formatting as manual saving does. Is this a rust-analyzer issue?

@flodiebold flodiebold added the A-vscode vscode plugin issues label Feb 21, 2020
@Veetaha
Copy link
Contributor

Veetaha commented Feb 22, 2020

This is interesting, I don't know where this formatting comes at all... @matklad do you know where the Rust formatter comes from?

@zshift
Copy link

zshift commented Feb 22, 2020

I just really started getting into rust, so excuse my ignorance. Same for vscode plugins.

I ran into rustfmt just a few min ago. It's installed with rustup component add rustfmt.

Usage is rustfmt [options] <file>.

@blitzerr
Copy link

blitzerr commented Sep 15, 2020

This isn't working for me either. I have the latest version.

@tbmreza
Copy link

tbmreza commented Nov 22, 2020

@blitzerr running rustfmt from the command line works for me. Have you tried overriding the default formatter?

  1. Install https://marketplace.visualstudio.com/items?itemName=statiolake.vscode-rustfmt
  2. Ctrl+Shift+P, Format Document With... Configure Default Formatter...
  3. Format on save now works on single rs file.

@matthiaskrgr
Copy link
Member

Had the same problem, had
"editor.formatOnSave": true,
and
"files.autoSave": "afterDelay", but files did not get formatted.

I looked into my settings.json and found this

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

and removing this fixed the auto-formatting for me :)

@lnicola
Copy link
Member

lnicola commented Jan 27, 2021

@djc, @blitzerr is this working for you now?

(Marking S-actionable, if you look at this in the future and the OP hasn't answered, ping me so we can close it.)

@lnicola lnicola added the S-actionable Someone could pick this issue up and work on it right now label Jan 27, 2021
@djc
Copy link
Author

djc commented Jan 27, 2021

No, this is still not working for me. I have formatOnSave set to true, files.autoSave set to afterDelay (300ms) but files are not getting formatted automatically. I don't have the defaultFormatter config that Matthias found.

@Veykril
Copy link
Member

Veykril commented Jan 27, 2021

Actually, it seems that this is a vscode specific problem... microsoft/vscode#45997
The docs for formatOnSave state:

  // Format a file on save. A formatter must be available, the file must not be saved after delay, and the editor must not be shutting down.
  "editor.formatOnSave": false,

https://code.visualstudio.com/docs/getstarted/settings
So autosaving after a delay does not trigger the formatter.

@Veykril Veykril added S-unactionable Issue requires feedback, design decisions or is blocked on other work and removed S-actionable Someone could pick this issue up and work on it right now labels Jan 27, 2021
@mukundbhudia
Copy link

Maybe it should be this:

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

with

"editor.formatOnSave": true,

all of which go in settings.json

@Oyami-Srk
Copy link

Perhaps "editor.formatOnSaveMode": "modificationsIfAvailable" or "editor.formatOnSaveMode": "modifications" has a conflict with auto-format of rust-analyzer. Setting it to file on rust specified settings solved this problem for me.

@bjorn3
Copy link
Member

bjorn3 commented Oct 13, 2021

"editor.formatOnSaveMode": "modifications"

That one will definitively not work. Only nightly rustfmt supports range formatting and rust-analyzer doesn't implement support for using this mode.

@Oyami-Srk
Copy link

Oyami-Srk commented Oct 14, 2021 via email

@zydxhs
Copy link

zydxhs commented Mar 3, 2022

"formatOnSave" also does not work when VSCode opens symlinked project. The "Rust Analyzer Client" output error info is:

INFO [3/3/2022, 9:26:04 AM]: Using server binary at /home/zhuyadong/.vscode-server/extensions/matklad.rust-analyzer-0.2.956/server/rust-analyzer
ERROR [3/3/2022, 9:26:04 AM]: Bootstrap error [Error: Failed to execute /home/zhuyadong/.vscode-server/extensions/matklad.rust-analyzer-0.2.956/server/rust-analyzer --version
	at Xb (/home/zhuyadong/.vscode-server/extensions/matklad.rust-analyzer-0.2.956/out/main.js:85:2121)
	at async Bb (/home/zhuyadong/.vscode-server/extensions/matklad.rust-analyzer-0.2.956/out/main.js:84:3252)
	at async Vm (/home/zhuyadong/.vscode-server/extensions/matklad.rust-analyzer-0.2.956/out/main.js:84:3083)
	at async Promise.all (index 0)]

@lnicola
Copy link
Member

lnicola commented Mar 3, 2022

@zydxhs that's probably #11558

@pinkponk
Copy link

I have had problems when the auto format simply stops working and for me it was because I was using too long module statements e.g I was using a function without importing it:
egui::collapsing_header::CollapsingState::load_with_default_open()
but had to instead do:

use egui::collapsing_header;
CollapsingState::load_with_default_open()

in order for the auto formater to come alive again. Might be similar issues for some of you so I thought I might write it here.

@lnicola
Copy link
Member

lnicola commented Nov 16, 2022

@pinkponk when this happens, try cargo fmt. If it doesn't work, it's a rustfmt bug.

@MolotovCherry
Copy link

MolotovCherry commented Dec 21, 2022

This wasn't working for me. After going through my settings, I found
"editor.formatOnSaveMode": "modifications",

I changed it to
"editor.formatOnSaveMode": "file",
And everything started working

Can definitely confirm the other comment about this solution

@Veykril Veykril added the C-support Category: support questions label Feb 9, 2023
@Rendez
Copy link

Rendez commented Feb 10, 2023

For me it was this setting that made it work:

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

@Veykril
Copy link
Member

Veykril commented Sep 16, 2023

There seem to be working workarounds here and as this i a client issue in the first place I'll go ahead and close this

@Veykril Veykril closed this as completed Sep 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-vscode vscode plugin issues C-support Category: support questions S-unactionable Issue requires feedback, design decisions or is blocked on other work
Projects
None yet
Development

No branches or pull requests