terraform-ls icon indicating copy to clipboard operation
terraform-ls copied to clipboard

formatOnSave not honoured/working on ,tf files

Open rbtcollins opened this issue 4 years ago • 1 comments

Server Version

$ terraform-ls --version
terraform-ls: command not found

Terraform Version

$ terraform -v
Terraform v1.0.8
on linux_amd64

Client Version

vscode 1.60.2

Expected Behavior

Installing the terraform extension in VS Code should be enough to have it work.

Actual Behavior

No errors were reported by VS Code (in the sense of notification bubbles), but saved files were not formatted.

Steps to Reproduce

  1. Install vscode in a brand new user - no existing settings.
  2. Install the terraform extension
  3. Open a directory with .tf files
  4. Open a .tf file
  5. Add a space somewhere that changes the content to be unformatted but still legitimate .tf
  6. Save the file

rbtcollins avatar Oct 08 '21 10:10 rbtcollins

Same issue for me I think, output from terraform-ls looks like this, any advice?

❯ terraform-ls -v
0.23.0
❯ terraform -v
Terraform v1.0.9
on linux_amd64
2021/10/19 12:51:02 formatting.go:41: formatting document via "/usr/bin/terraform-ls"
2021/10/19 12:51:02 rpc_logger.go:45: Error for "textDocument/formatting" (ID 59): [-32098] fmt was first introduced in Terraform 0.7.7: unable to parse version "": Malformed version: 
2021/10/19 12:51:02 server.go:252: Completed 1 requests [5.759612ms elapsed]
[Error - 12:51:02 PM] Request textDocument/formatting failed.
  Message: fmt was first introduced in Terraform 0.7.7: unable to parse version "": Malformed version: 
  Code: -32098 

EDIT: ok this was quick... I solved it by adding this in VSCode settings

    "terraform-ls.terraformExecPath": "/usr/bin/terraform",

mateuszkozakiewicz avatar Oct 19 '21 10:10 mateuszkozakiewicz

Hi @rbtcollins It looks like we completely forgot about this older issue! Sorry about that.

Either way, the auto-formatting in VS Code should work, as long as the appropriate settings is in place. We have some documentation available under https://github.com/hashicorp/vscode-terraform#formatting

If you can confirm that these settings are in place and formatting still doesn't work I would encourage you to file a new issue and attach all the relevant settings + ideally the log output.

Thanks!

radeksimko avatar Oct 20 '22 08:10 radeksimko

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 details necessary to investigate further.

github-actions[bot] avatar Nov 20 '22 03:11 github-actions[bot]