Vscode-terraform: editor.formatOnSave is not working as expected

Created on 10 Jul 2019  ยท  5Comments  ยท  Source: hashicorp/vscode-terraform

Hey buddy!

First of all, congratulations for the extension!
There's only one thing that I suggest you to check, it's the editor.formatOnSave. It's not working as expected.

The file changed is ignored when saved. On the other hand, when we use Format Document command on VSCode, the file is correctly formatted.

vscode-terraform version: 1.3.12
Terraform version: 0.12.3
VSCode version 1.36.0

settings.json on VSCode

    "files.associations": {
        "*.ctmpl": "hcl",
        "*.tf": "terraform",
        "*.tfvars": "terraform"
    },

    "[terraform]": {
        "editor.formatOnSave": true
    }

Save file log:

2019-07-10 14:44:26:223 [INFO ] formatting-provider: running 'terraform fmt' on 'c:\Users\MyUser\Desktop\terraform_folder\terraformfile.tf'
2019-07-10 14:44:26:223 [INFO ] runner: Running terraform cwd='C:\Users\MyUser\AppData\Local\Programs\Microsoft VS Code' path='terraform' (version: 0.12.3) args=[fmt, -]
2019-07-10 14:44:27:041 [DEBUG] index-adapter: Ignoring document: file:///c%3A/Users/MyUser/Desktop/terraform_folder/terraformfile.tf
2019-07-10 14:44:32:349 [INFO ] runner: Running terraform succeeded.

Manual Alt Shift F (Format Document):

2019-07-10 14:54:50:445 [INFO ] formatting-provider: running 'terraform fmt' on 'c:\Users\MyUser\Desktop\terraform_folder\terraformfile.tf'
2019-07-10 14:54:50:445 [INFO ] runner: Running terraform cwd='C:\Users\MyUser\AppData\Local\Programs\Microsoft VS Code' path='terraform' (version: 0.12.3) args=[fmt, -]
2019-07-10 14:54:56:071 [INFO ] runner: Running terraform succeeded.
2019-07-10 14:54:56:089 [DEBUG] index-adapter: Ignoring document: file:///c%3A/Users/MyUser/Desktop/terraform_folder/terraformfile.tf

Could you kindly check this please?

Thank you.

Most helpful comment

I was experiencing the same issue starting with the latest update in vscode in which they changed the integration with prettier formater. My proposed solution assumes you also have prettier plugin istalled or any other formater as default.

My settings.json is configured as follows:

"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
  "[terraform]": {
    "editor.defaultFormatter": "mauve.terraform"
  }

With the last part, which overrides the formater for terraform language, my formating started to work again :)

All 5 comments

I am also seeing a similar issue. When using the format on save functionality, equal signs do not get aligned (and will actually be moved out of alignment and just to a single space separating it), but if I use the manual format document or run terraform fmt from the command line manually, equal signs do get aligned.

I was experiencing the same issue starting with the latest update in vscode in which they changed the integration with prettier formater. My proposed solution assumes you also have prettier plugin istalled or any other formater as default.

My settings.json is configured as follows:

"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
  "[terraform]": {
    "editor.defaultFormatter": "mauve.terraform"
  }

With the last part, which overrides the formater for terraform language, my formating started to work again :)

We just released v2.0.0-rc.1 of the extension. The main features include:

  • Added syntax support for 0.12
  • Added terraform-ls usage by default (currently on 0.3.0, which offers basic provider code completion)

You can find additional information and specifics in the release notes and CHANGELOG.

With this release we expect that many of the prior issues and PRs are no longer relevant or have been addressed, and are therefore being closed. If you feel the action taken on an issue or PR is in error, please comment as such and we can figure out the appropriate way to address it.

We plan to add the final 2.0.0 release to the marketplace soon, but are actively seeking your feedback now on the release candidates. You can download the .vsix from the releases page and manually install it in VS Code to try it out.

Using v2.0.1 I'm having the same problem: formatting with prettier does not work out of the box. Updating settings.json with the suggested config does not fix the issue either. I think this issue should be reopened.

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the context necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fcatacut picture fcatacut  ยท  5Comments

tillig picture tillig  ยท  4Comments

avinashkavi447 picture avinashkavi447  ยท  7Comments

jacobisaliveandwell picture jacobisaliveandwell  ยท  3Comments

vazkarvishal picture vazkarvishal  ยท  5Comments