Vscode-terraform: Can't format files after upgrade to 2.0.1

Created on 11 Jun 2020  ·  17Comments  ·  Source: hashicorp/vscode-terraform

There seems to be no option to format .tf .tfvars .hcl files after the upgrade anymore :(

Most helpful comment

This sounds like the same problem I am having with the 2.0 release. It require that the root of the VS Code workspace contains an inited TF directory. But in my case I have a IaC repo that contains many subdirectories, some of which container TF code, while the top of the workspace does not. I assume this is most common use case. Opening each individual TF directory as a separate VS Code workspace would be tedious.

I am trying to find some sort of documentation or discussion of this, but so far no joy.

All 17 comments

The v2 extension should respect the standard "editor.formatOnSave": true settings which will perform formatting through the language server.

See https://code.visualstudio.com/docs/getstarted/settings

The extension does _not_ format tfvars nor hcl, but tfvars support is planned as per https://github.com/hashicorp/terraform-ls/issues/50

I'm not sure what the best practice is for formatting support in similar extensions and whether we should consider adding a more scoped settings there and how should that interact with the global one. 🤔

@radeksimko It appears it doesn't work anymore :(
I have this section in my settings.json:

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

Still, my .tf won't format when I save them...

Also, is there a plan to support hcl files too? They were supported earlier and now suddenly not.
It's a heavy loss for us Terragrunt users :(

this is my previous usersettings.json
https://github.com/FernandoMiguel/kb/blob/master/VisualCode/usersettings.json
"editor.formatOnSave": true, is there

@FernandoMiguel

    "[terraform]": {},
    "terraform.format": {
        "enable": true,
        "formatOnSave": true
    },

Unfortunately this does not enable formatting in the latest version, you would need to use the [terraform] section, like I mentioned above, unless we add the old options back.

I'd suggest we check how other popular similar (non-Terraform) extensions deal with this and whether they have a dedicated formatting settings, before adding it back.

@NightmareCinema The following seems to be working for me:

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

Can you double check you put this in the right place in the settings file? I'm not sure what else could differ, but it does work for me.

let me try @radeksimko

@radeksimko so should look like this?

    "[terraform]": {
        "editor.formatOnSave": true
    },
    "terraform.languageServer": {
        "external": true,
        // "pathToBinary": "/Users/fernando/.vscode-insiders/extensions/hashicorp.terraform-2.0.1/lsp/terraform-ls",
        "args": [
            "serve"
        ]
    },

formatting isn't working still. not sure what i am missing

In case it helps anybody. This didn't start working for me until I added:

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

and I opened my terraform folder as the root of the workspace. Resolving Message: Workspace not initialized. Please run terraform init is a strict dependency before the formatting starts working.

This is the global setting on a clean install of VS Code:

{
    "editor.formatOnSave": true
}

As others have mentioned, this is the language specific setting:

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

And you would just use the built-in command Format Document. This isn't separated out any longer from the normal VS Code options.

If its still not working, can you grab the logs from the terraform-ls output pane? Its probably having an issue finding the Terraform binary or something similar.

Launching language server: /Users/fernando/.vscode-insiders/extensions/hashicorp.terraform-2.0.1/lsp/terraform-ls serve
2020/06/11 15:50:42 service.go:63: Preparing new session ...
2020/06/11 15:50:42 langserver.go:79: Starting server (pid 21684) ...
2020/06/11 15:50:42 server.go:469: Received 1 new requests
2020/06/11 15:50:42 server.go:165: Processing 1 requests
2020/06/11 15:50:42 server.go:223: Checking request for "initialize": {"processId":21658,"rootPath":"/Users/fernando/REDACTED","rootUri":"file:///Users/fernando/REDACTED","capabilities":{"workspace":{"applyEdit":true,"workspaceEdit":{"documentChanges":true,"resourceOperations":["create","rename","delete"],"failureHandling":"textOnlyTransactional"},"didChangeConfiguration":{"dynamicRegistration":true},"didChangeWatchedFiles":{"dynamicRegistration":true},"symbol":{"dynamicRegistration":true,"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]}},"executeCommand":{"dynamicRegistration":true},"configuration":true,"workspaceFolders":true},"textDocument":{"publishDiagnostics":{"relatedInformation":true},"synchronization":{"dynamicRegistration":true,"willSave":true,"willSaveWaitUntil":true,"didSave":true},"completion":{"dynamicRegistration":true,"contextSupport":true,"completionItem":{"snippetSupport":true,"commitCharactersSupport":true,"documentationFormat":["markdown","plaintext"],"deprecatedSupport":true,"preselectSupport":true},"completionItemKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]}},"hover":{"dynamicRegistration":true,"contentFormat":["markdown","plaintext"]},"signatureHelp":{"dynamicRegistration":true,"signatureInformation":{"documentationFormat":["markdown","plaintext"],"parameterInformation":{"labelOffsetSupport":true}}},"definition":{"dynamicRegistration":true,"linkSupport":true},"references":{"dynamicRegistration":true},"documentHighlight":{"dynamicRegistration":true},"documentSymbol":{"dynamicRegistration":true,"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]},"hierarchicalDocumentSymbolSupport":true},"codeAction":{"dynamicRegistration":true,"codeActionLiteralSupport":{"codeActionKind":{"valueSet":["","quickfix","refactor","refactor.extract","refactor.inline","refactor.rewrite","source","source.organizeImports"]}}},"codeLens":{"dynamicRegistration":true},"formatting":{"dynamicRegistration":true},"rangeFormatting":{"dynamicRegistration":true},"onTypeFormatting":{"dynamicRegistration":true},"rename":{"dynamicRegistration":true,"prepareSupport":true},"documentLink":{"dynamicRegistration":true},"typeDefinition":{"dynamicRegistration":true,"linkSupport":true},"implementation":{"dynamicRegistration":true,"linkSupport":true},"colorProvider":{"dynamicRegistration":true},"foldingRange":{"dynamicRegistration":true,"rangeLimit":5000,"lineFoldingOnly":true},"declaration":{"dynamicRegistration":true,"linkSupport":true}}},"trace":"off","workspaceFolders":[{"uri":"file:///Users/fernando/REDACTED","name":"regional-services"}]}
2020/06/11 15:50:42 rpc_logger.go:29: Incoming request for "initialize" (ID 0): {"processId":21658,"rootPath":"/Users/fernando/REDACTED","rootUri":"file:///Users/fernando/REDACTED","capabilities":{"workspace":{"applyEdit":true,"workspaceEdit":{"documentChanges":true,"resourceOperations":["create","rename","delete"],"failureHandling":"textOnlyTransactional"},"didChangeConfiguration":{"dynamicRegistration":true},"didChangeWatchedFiles":{"dynamicRegistration":true},"symbol":{"dynamicRegistration":true,"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]}},"executeCommand":{"dynamicRegistration":true},"configuration":true,"workspaceFolders":true},"textDocument":{"publishDiagnostics":{"relatedInformation":true},"synchronization":{"dynamicRegistration":true,"willSave":true,"willSaveWaitUntil":true,"didSave":true},"completion":{"dynamicRegistration":true,"contextSupport":true,"completionItem":{"snippetSupport":true,"commitCharactersSupport":true,"documentationFormat":["markdown","plaintext"],"deprecatedSupport":true,"preselectSupport":true},"completionItemKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25]}},"hover":{"dynamicRegistration":true,"contentFormat":["markdown","plaintext"]},"signatureHelp":{"dynamicRegistration":true,"signatureInformation":{"documentationFormat":["markdown","plaintext"],"parameterInformation":{"labelOffsetSupport":true}}},"definition":{"dynamicRegistration":true,"linkSupport":true},"references":{"dynamicRegistration":true},"documentHighlight":{"dynamicRegistration":true},"documentSymbol":{"dynamicRegistration":true,"symbolKind":{"valueSet":[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]},"hierarchicalDocumentSymbolSupport":true},"codeAction":{"dynamicRegistration":true,"codeActionLiteralSupport":{"codeActionKind":{"valueSet":["","quickfix","refactor","refactor.extract","refactor.inline","refactor.rewrite","source","source.organizeImports"]}}},"codeLens":{"dynamicRegistration":true},"formatting":{"dynamicRegistration":true},"rangeFormatting":{"dynamicRegistration":true},"onTypeFormatting":{"dynamicRegistration":true},"rename":{"dynamicRegistration":true,"prepareSupport":true},"documentLink":{"dynamicRegistration":true},"typeDefinition":{"dynamicRegistration":true,"linkSupport":true},"implementation":{"dynamicRegistration":true,"linkSupport":true},"colorProvider":{"dynamicRegistration":true},"foldingRange":{"dynamicRegistration":true,"rangeLimit":5000,"lineFoldingOnly":true},"declaration":{"dynamicRegistration":true,"linkSupport":true}}},"trace":"off","workspaceFolders":[{"uri":"file:///Users/fernando/REDACTED","name":"regional-services"}]}
2020/06/11 15:50:42 exec.go:176: Starting /usr/local/bin/terraform ["terraform" "version"] in "/var/folders/4j/rqhd11ks51v63sjm1sgr3shw0000gn/T/"...
2020/06/11 15:50:42 exec.go:142: Waiting for command to finish ...
2020/06/11 15:50:42 exec.go:168: terraform run (/usr/local/bin/terraform ["terraform" "version"], in "/var/folders/4j/rqhd11ks51v63sjm1sgr3shw0000gn/T/", pid 21685) finished with exit code 0
2020/06/11 15:50:42 initialize.go:90: Found compatible Terraform version (0.12.26) at /usr/local/bin/terraform
2020/06/11 15:50:42 schema_storage.go:334: Adding workspace for watching: "/Users/fernando/REDACTED"
2020/06/11 15:50:42 watcher.go:88: Adding "/Users/fernando/REDACTED/.terraform/plugins/darwin_amd64/lock.json" for watching...
2020/06/11 15:50:42 schema_storage.go:124: Acquiring semaphore before retrieving schema for "/Users/fernando/REDACTED" ...
2020/06/11 15:50:42 rpc_logger.go:45: Error for "initialize" (ID 0): [-32098] Workspace not initialized. Please run `terraform init` in /Users/fernando/REDACTED
2020/06/11 15:50:42 schema_storage.go:133: Retrieving schemas for "/Users/fernando/REDACTED" ...
2020/06/11 15:50:42 server.go:204: Completed 1 requests [135.006023ms elapsed]
2020/06/11 15:50:42 exec.go:176: Starting /usr/local/bin/terraform ["terraform" "providers" "schema" "-json"] in "/Users/fernando/REDACTED"...
[Error - 15:50:42] Server initialization failed.
  Message: Workspace not initialized. Please run `terraform init` in /Users/fernando/REDACTED
  Code: -32098 
2020/06/11 15:50:42 exec.go:142: Waiting for command to finish ...
2020/06/11 15:50:42 server.go:469: Received 1 new requests
2020/06/11 15:50:42 server.go:165: Processing 1 requests
2020/06/11 15:50:42 server.go:223: Checking request for "shutdown": 
2020/06/11 15:50:42 rpc_logger.go:29: Incoming request for "shutdown" (ID 1): 
2020/06/11 15:50:42 rpc_logger.go:50: Response to "shutdown" (ID 1): null
2020/06/11 15:50:42 server.go:204: Completed 1 requests [178.118µs elapsed]
2020/06/11 15:50:42 server.go:469: Received 1 new requests
2020/06/11 15:50:42 server.go:165: Processing 1 requests
2020/06/11 15:50:42 server.go:223: Checking request for "exit": 
2020/06/11 15:50:42 rpc_logger.go:29: Incoming notification for "exit": 
2020/06/11 15:50:42 schema_storage.go:116: error obtaining schemas: Unable to retrieve schemas: failed to get schemas: Execution of ["terraform" "providers" "schema" "-json"] canceled.
signal: killed

This sounds like the same problem I am having with the 2.0 release. It require that the root of the VS Code workspace contains an inited TF directory. But in my case I have a IaC repo that contains many subdirectories, some of which container TF code, while the top of the workspace does not. I assume this is most common use case. Opening each individual TF directory as a separate VS Code workspace would be tedious.

I am trying to find some sort of documentation or discussion of this, but so far no joy.

I believe this is related to #366 .

Echoing @larslevie's point - I work primarily in a Git repo where there are multiple isolated Terraform environments in separate folders, along with a modules folder that, by design, contains reusable components that are meant to be reused across different environments.

In order for the 2.0.1 language server to even function, it expects me to have ran terraform init in the root of my workspace. This isn't feasable because there are many different Terraform environments in the repo. The only way to even get it to function is to change my workflow to have separate workspaces for each of those Terraform environments. But if I'm working in code in the shared modules folder - which can't really be terraform init-ed, then I'm stuck.

As others mentioned, the use case of not having .tf files directly under the root of the workspace is quite common, and, probably why it fails for me too.

Moreover, it proves to be problematic having to run terraform init inside any terraform directory, since not all terraform directories should necessarily be initialised (like Modules directories).
I think it would be great not strict the user to such terms, since it kinds of hurting the user experience. That's something earlier versions excelled at more.

Yes this is definitely an oversight that we should be addressing soon, here is the tracking issue on the LS: https://github.com/hashicorp/terraform-ls/issues/165

Going to close this in favor of that, the LS will contain the fix.

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

okgolove picture okgolove  ·  4Comments

domenjesenovec picture domenjesenovec  ·  6Comments

sugarraysam picture sugarraysam  ·  4Comments

vfab picture vfab  ·  4Comments

QuinnStevens picture QuinnStevens  ·  6Comments