question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot get autoformatting (format on save) working - VS Code

See original GitHub issue

Hi 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:closed
  • Created 3 years ago
  • Reactions:21
  • Comments:34 (8 by maintainers)

github_iconTop GitHub Comments

5reactions
radeksimkocommented, Jul 16, 2021

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:

    "[terraform]": {
        "editor.defaultFormatter": "hashicorp.terraform",
        "editor.formatOnSave": true
    }

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!

4reactions
jpograncommented, Dec 23, 2021

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.defaultFormatter because 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 use file for now. The file setting works because terrafrom fmt was written to format an entire file, not ranges. The modifications or modificationsIfAvailable settings 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, modifications won’t work at all. The modificationsIfAvailable setting will fall back to file if 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:

"[terraform]": {
  "editor.defaultFormatter": "hashicorp.terraform",
  "editor.formatOnSave": true,
  "editor.formatOnSaveMode": "file"
},

I did not see it mentioned here, but if you are using editor.codeActionsOnSave and versions of terraform-ls older than 0.24.0 there is a known issue. If you had editor.codeActionsOnSave configured with source.fixAll there was a bug in our implementation that was fixed in the 0.24.0 release. If you pin your terraform-ls version with terrafrom.langaugeServer.requiredVersion, please update to the latest as there are fixes you are missing.

If you want to use editor.codeActionsOnSave with source.formatAll.terraform and editor.FormatOnSave, then I have an explanation and a recommendation for you. VS Code runs editor.codeActionsOnSave and editor.FormatOnSave separately and will not determine if a codeAction is 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.codeActionsOnSave with source.formatAll.terraform and editor.FormatOnSave please use the following configuration. This will keep the global editor.formatOnSave for other languages you use, and configure the terraform extension to only format during a codeAction sweep.

"editor.formatOnSave": true,
"[terraform]": {
  "editor.defaultFormatter": "hashicorp.terraform",
  "editor.formatOnSave": false,
  "editor.codeActionsOnSave": {
    "source.formatAll.terraform": true
  },
},

I plan to write some more detailed documentation in an official place after the holidays. Hope this helps in the interim.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found