Cannot get autoformatting (format on save) working - VS Code
See original GitHub issueHi There,
After ensuring my VSCode terraform extension was up to date (running 2.5.0 released an hour ago) and adding the proper settings to my settings.json vscode config file:
{
"git.autofetch": true,
"git.confirmSync": false,
"editor.renderControlCharacters": true,
"terraform.languageServer": {
"[terraform]": {
"editor.formatOnSave": true
},
"terraform-ls.experimentalFeatures": {
},
"external": true,
"pathToBinary": "",
"args": [
"serve"
],
"maxNumberOfProblems": 100,
"trace.server": "off"
},
}
I’m not able to get formatting to occur on a save. Is there something I’m missing? I’ve also added the terraform format on save settings block outside of the terraform.languageserver block, but get the same result (nothing happening to intentionally poorly formatted code with no indentions or spaces).
Issue Analytics
- State:
- Created 3 years ago
- Reactions:21
- Comments:34 (8 by maintainers)
Top Results From Across the Web
VS-Code Prettier Format On Save doesn't work - Stack Overflow
Follow these steps: CTRL + SHIFT + P; Format Document (in pop-up bar); Select Format Document; Select Configure Default Formatter.
Read more >Format on Save (prettier) stopped working with latest update
I'm using ruby as well, and I've got the same problem with rubocop formatter: it stopped working as of 1.50.0. And I can't...
Read more >VSCode Format On Save – everything you need to know
Open up VSCode Command Palette by pressing Ctrl + Shift + P. · Search and select Open Settings (UI). · Use the search...
Read more >How To Auto-Format Code in VS Code - Alphr
1. Open Visual Studio Code on your computer. 2. Navigate to the extension tab on the left side of the screen. It’s represented...
Read more >VS Code - You don't need a formatting extension (Prettier and ...
I'd suggest taking the extension settings a step further—configuring Prettier not only to auto-format your code on save, but to also display ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hi folks, I have a hunch that one of the reasons for the functionality to be “flipping” is due to VS Code’s handling of conflicts when multiple extensions claim formatting support for the same file.
In other words: If you have any other extensions supporting Terraform files - have you tried disabling those?
We plan to investigate these conflicts also as part of https://github.com/hashicorp/vscode-terraform/issues/692
You can also explicitly tell VS Code to use this extension for formatting Terraform files using the following snippet of config:
With all that in mind, can anyone reproduce the issue on the latest version (
2.13.1) with the latest LS version (0.19.0) and with other Terraform extensions (if any are installed) disabled? If so I’d be interested in setting up a debugging session with you or gathering more details.I will leave this open for a few more weeks and then close unless I receive any confirmation of the above.
Thanks!
Hello all, thank you for your continued feedback. I am writing a shorter update than I wanted to due to the end of year holidays, but did want to keep you all in the loop and summarize some of what we are seeing.
Most of you are using the settings as documented in our readme. For those not following those settings, please review that section and try those. We have to specify this extension as the
editor.defaultFormatterbecause there are other extensions that advertise the ability to format Terraform files and VS Code chooses which one depending on activation order (something outside of our control) and which is non-deterministic in general and complicated in specific cases.For those trying to use
editor.formatOnSaveMode, you will have to usefilefor now. Thefilesetting works becauseterrafrom fmtwas written to format an entire file, not ranges. ThemodificationsormodificationsIfAvailablesettings use the currently configured SCM to detect file line ranges that have changed and send those ranges to the formatter and expect only those ranges returned. In addition, if you don’t have a SCM enabled for the files you are editing,modificationswon’t work at all. ThemodificationsIfAvailablesetting will fall back tofileif there is no SCM and will appear to work sometimes.In summary, if you want to automatically format your entire Terraform file on save, us the following configuration:
I did not see it mentioned here, but if you are using
editor.codeActionsOnSaveand versions ofterraform-lsolder than 0.24.0 there is a known issue. If you hadeditor.codeActionsOnSaveconfigured withsource.fixAllthere was a bug in our implementation that was fixed in the 0.24.0 release. If you pin yourterraform-lsversion withterrafrom.langaugeServer.requiredVersion, please update to the latest as there are fixes you are missing.If you want to use
editor.codeActionsOnSavewithsource.formatAll.terraformandeditor.FormatOnSave, then I have an explanation and a recommendation for you. VS Code runseditor.codeActionsOnSaveandeditor.FormatOnSaveseparately and will not determine if acodeActionis performing the same operation as a formatter. This means you can format a file more than once on save if both are configured at the same time. This is part of VS Code, not just our extension, so we don’t have much control here.So, if you want to use
editor.codeActionsOnSavewithsource.formatAll.terraformandeditor.FormatOnSaveplease use the following configuration. This will keep the globaleditor.formatOnSavefor other languages you use, and configure the terraform extension to only format during acodeActionsweep.I plan to write some more detailed documentation in an official place after the holidays. Hope this helps in the interim.