rust-analyzer: Format on auto-saving doesn't work

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?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 9
  • Comments: 19 (8 by maintainers)

Most upvoted comments

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.

Maybe it should be this:

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

with

"editor.formatOnSave": true,

all of which go in settings.json

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 😃

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>.

For me it was this setting that made it work:

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

"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.

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

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

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

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.