Vscode-terraform: Auto-format stopped working

Created on 6 Sep 2018  Â·  22Comments  Â·  Source: hashicorp/vscode-terraform

I'm not sure when this started but I am able to replicate it on a freshly installed Windows 10 instance.

Steps to reproduce:

  1. Download terraform, ensure it is in the user's PATH
  2. Install vscode and the vscode-terraform extension
  3. Edit a *.tf file, mess up the formatting.
  4. Save the file. Auto-formatting doesn't work.

Most helpful comment

@george-richardson you are right, I forgot to update the README.md after the formatting change in version 1.3

@scarolan can you please verify that you have either editor.formatOnSave or [terraform]editor.formatOnSave set to true?

All 22 comments

I think it's the new version of VSCode that broke it, but I might be mistaken.

As a workaround, run terraform fmt after saving the files. Not the best, but at least formatting will be still consistent.

I'm seeing the same behavior on macOS 10.13.8 with VS Code 1.27.1 and TF plugin 1.3.4.

Is there any chance that other plugins are interfering with this behavior, or are we confident that it's VS Code?

I am only using Powershell 1.8.4 plugin other than Terraform. I would be surprised if there were any interferences.

The output view (Shift-Cmd-U on Mac, Ctrl-Shift-U on Win) has a tab called
Terraform, please post the contents of that tab.

Kind regards,

Mikael

>

There is no such tab on mine...
tf

Please click Output and then select Terraform in the drop-down on the right.
screen shot 2018-09-14 at 4 32 17 pm

LOL, that was silly. Apologies!

Here’s the output:

2018-09-14 15:03:54:597 [INFO ] runner: Found Terraform binary terraform with version 0.11.8

2018-09-14 15:03:54:745 [INFO ] runner: Found Terraform binary C:\Program Files\Terraform\terraform with version 0.11.8

2018-09-14 15:03:54:871 [INFO ] runner: Found Terraform binary terraform with version 0.11.8

that looks weird, can you please post the value of your PATH environment variable? Pasting the following in PowerShell will show the value of the PATH: Get-ChildItem Env:Path (I think just writing $Env:Path also works).

@audiolomb ping

@scarolan @jkodroff can you please post your contents of the Output > Terraform view?

@mauve here is my PATH:

C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\TortoiseSVN\bin;C:\Program Files\SafeNet\Authentication\SAC\x64;C:\Program Files\SafeNet\Authentication\SAC\x32;D:\adteam-dfs\Tools;C:\Program Files\Sysinternals;C:\Program Files\Microsoft Network Monitor 3\;C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio\;C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\130\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\ManagementStudio\;C:\Program FilesTerraform;C:\Program Files (x86)\Amazon\AWSCLI\;C:\Program Files\Microsoft VS Code\bin;C:\Users\XXXX\AppData\Local\Microsoft\WindowsApps;;C:\Program Files\Microsoft VS Code\bin

I recently noticed the same after not using terraform for a while. For me it was the jump to version 1.3 which includes the change "Format On Auto-Save Removal", I did not have the setting "editor.formatOnSave" set to true so the removal of "terraform.format.enable" setting broke any formatting for me. This problem was compounded by the README.md on this repo still referencing the "terraform.format.enable" setting. I only worked out what was happening by downgrading my extension to older versions to find exactly where it broke and then looking at the changelog.

So quick-fix for me was to add "editor.formatOnSave": true to my user settings. I think the README for this extension should be updated to include this requirement as this setting's default on a new install is false.

Love the extension though, only issue I have ever had!

@george-richardson you are right, I forgot to update the README.md after the formatting change in version 1.3

@scarolan can you please verify that you have either editor.formatOnSave or [terraform]editor.formatOnSave set to true?

I am going to close this bug due to inactivity. Please feel free to open again.

Thanks for using my plugin and thanks for taking the time to open bugs.

Hi @mauve, Thanks a lot for your plugin!

I'm running into the same issue.
vscode version : 1.30.1
vscode-terraform version : 1.3.5

I have editor.formatOnSave set to true in my user settings.
Here is the output from the terraform tab :

2019-01-03 05:29:31:346 [INFO ] formatting-provider: running 'terraform fmt' on '/Users/mactynow/terraform/aks-demo/main.tf'
2019-01-03 05:29:31:346 [INFO ] runner: Running terraform cwd='/' path='/usr/local/bin/terraform' (version: 0.11.10) args=[fmt, -]
2019-01-03 05:29:32:179 [INFO ] runner: Running terraform succeeded.
2019-01-03 05:29:32:300 [INFO ] file-system-watcher: Indexed file:///Users/mactynow/terraform/aks-demo/main.tf

I am also running the azure-terraform plugin, could there be conflicts ?

Let me know if I can do anything to help.

I'm having issues with terraform format on save now. I also cannot right click and format manually. I have format on save enabled in the preferences.

Is the language set to Terraform for your files, or is it set to HCL?

On Mon, Feb 4, 2019 at 1:48 PM queglay notifications@github.com wrote:

I'm having issues with terraform format on save now. I also cannot right
click and format manually. I have format on save enabled in the preferences.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/mauve/vscode-terraform/issues/125#issuecomment-460237648,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAwkmNu6lHeSIL8kc8i5yfNWrk6V_2eSks5vKCw1gaJpZM4Wb32I
.

Its set to terraform

Andrew Graham

On 4 February 2019 at 11:14:18 pm, Mikael Olenfalk ([email protected])
wrote:

Is the language set to Terraform for your files, or is it set to HCL?

On Mon, Feb 4, 2019 at 1:48 PM queglay notifications@github.com wrote:

I'm having issues with terraform format on save now. I also cannot right
click and format manually. I have format on save enabled in the
preferences.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<
https://github.com/mauve/vscode-terraform/issues/125#issuecomment-460237648
,
or mute the thread
<
https://github.com/notifications/unsubscribe-auth/AAwkmNu6lHeSIL8kc8i5yfNWrk6V_2eSks5vKCw1gaJpZM4Wb32I

.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/mauve/vscode-terraform/issues/125#issuecomment-460245539,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AD2-nfLkZ7Mzy9itSY5SWcMYGBDh4Z12ks5vKDIqgaJpZM4Wb32I
.

I'm also experiencing auto-saving not working.

vscode: 1.30.2
vscode-terraform version : 1.3.7
terraform: 0.11.11

Strange part is the command palette and right click options for formatting both work but a ctl+S does not. Auto-saving is configured correctly and I get successful output in the console output.

I downgraded several extension version, 1.3.6, 1.3.5, 1.2.3, none worked. 1.1.2 did work for me.

----- These both work ----------
Right-click Format Document
Command Palette Format Document

Oddly I get a second run of formatting, based on the file in the editor window. The file is updated after the first formatting try in this console output.

2019-02-04 18:46:01:283 [INFO ] formatting-provider: running 'terraform fmt' on 'd:\projects\go\src\...{removed}\dash\terraforms\dash-worker\dash-lambda.tf'
2019-02-04 18:46:01:283 [INFO ] runner: Running terraform cwd='C:\Users\mannb\AppData\Local\Programs\Microsoft VS Code' path='terraform' (version: 0.11.11) args=[fmt, -]
2019-02-04 18:46:02:618 [INFO ] runner: Running terraform succeeded.
2019-02-04 18:46:08:359 [INFO ] formatting-provider: running 'terraform fmt' on 'd:\projects\go\src\...\dash\terraforms\dash-worker\dash-lambda.tf'
2019-02-04 18:46:08:359 [INFO ] runner: Running terraform cwd='C:\Users\mannb\AppData\Local\Programs\Microsoft VS Code' path='terraform' (version: 0.11.11) args=[fmt, -]
2019-02-04 18:46:09:238 [INFO ] file-system-watcher: Indexed file:///d%3A/projects/go/src/.../dash/terraforms/dash-worker/dash-lambda.tf
2019-02-04 18:46:09:746 [INFO ] runner: Running terraform succeeded.

Ctl+S does not work.... although the console output indicates it does. See below:

2019-02-04 18:44:38:795 [INFO ] formatting-provider: running 'terraform fmt' on 'd:\projects\go\src\...\dash\terraforms\dash-worker\dash-lambda.tf'
2019-02-04 18:44:38:795 [INFO ] runner: Running terraform cwd='C:\Users\mannb\AppData\Local\Programs\Microsoft VS Code' path='terraform' (version: 0.11.11) args=[fmt, -]
2019-02-04 18:44:39:671 [INFO ] file-system-watcher: Indexed file:///d%3A/projects/go/src/.../dash/terraforms/dash-worker/dash-lambda.tf
2019-02-04 18:44:40:210 [INFO ] runner: Running terraform succeeded.

One more tidbit I just found. Using Ctl+S, the file timestamp actually does update... but the formatting is not done.

First of all thanks very much for the extension!
I also have issues with format on save on vscode 1.31.1
I have the latest version installed of the extension

I can right click and choose format document and it will work, my settings look like this (with non related details removed):
{
"terraform.format": {
"enable": true,
"ignoreExtensionsOnSave": [
".tfsmurf"
],
"formatOnSave": true
},
"terraform.codelens.enabled": false
}

Hi all.
Terraform extension for vscode is not working anymore? ..... I fix it. How?:

  1. Unistalled the vscode.
  2. I downloaded the lastest version from the vscode page.
  3. I installed that version with the dpkg utility: sudo dpkg -i
  4. I execued a dpkg-reconfigure: sudo dpkg-reconfigure code
  5. I installed the lastest Terraform Extension.
  6. It's working again!.

@alexismosquera You basically reinstalled :)

If anyone is wishing for automatic formatting back, this will do it for you. It uses fswatch to pass files to terraform fmt on save

.vscode/tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Terraform Format",
      "type": "shell",
      "command": "fswatch -r -e terraform/.terraform --format %p terraform | xargs -n1 -I{} terraform fmt {}",
      "group": "test",
      "presentation": {
        "echo": false,
        "reveal": "never",
        "focus": false,
        "panel": "shared",
        "showReuseMessage": false,
        "clear": true
      },
      "runOptions": {
        "runOn": "folderOpen"
      }
    }
  ]
}

See https://go.microsoft.com/fwlink/?LinkId=733558
for the documentation about the tasks.json format

Was this page helpful?
0 / 5 - 0 ratings