mschuchard / linter-terraform-syntax

terraform validate linter and formatter for pulsar

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not running format

flamein opened this issue · comments

Given a simple file test.tf:

variable "availability_zone_names" {
  type    = list(string)
    default = ["us-west-1a"]
}

when using Terraform v0.12.28 on macOS Catalina, it doesn't actually format the code when saving

Does the config validation functionality in Atom Editor still behave as expected in your environment? Does Terraform format the config file correctly when executed with the CLI in your environment?

Yes, it executes correctly when running from CLI, Atom is started from that same environment using atom -a .
Also linting functions without issues.

If there is anything more I can do to help debug, ELI5 (Explain Like I'm 5) have next to no experience with Node based tools

Best guess is this is due to validate/plan executing within the current working directory, whereas the formatting targets a directory or file based on the config option (former feature request for only formatting current file), and then there is a pathing syntax issue here when retrieved from Atom/Node.

We are going to old school debug this one. In ~/.atom/packages/linter-terraform-syntax/lib/main.js at line 153, there should be helpers.exec(atom.config.get('linter-terraform-syntax.terraformExecutablePath'), ['fmt', '-list=false', fmt_target]). Paste in the following three lines beneath it:

atom.notifications.addInfo('Debug:', {
  detail: atom.config.get('linter-terraform-syntax.terraformExecutablePath') + ' fmt -list=false ' + fmt_target
})

reload your Atom session after modifying the code. Your next TF config load/save will trigger a blue info box in the upper right with the literal command executed in your shell for formatting. Copy that command, and see if it appears legitimate. If it does, then try pasting and executing it manually within your shell interpreter. If that works, then this becomes very difficult.

I think I see what's going on here, at least in our case:

$ terraform fmt -list=false ~/terraform/project/test.tf
backend.tf not found

Basically we have a wrapper around terraform that picks the right terraform version for each project by looking for the backend.tf in the current working directory and pulling the version from it.
Using the direct path to the proper terraform version works.

We'll look for a solution in our wrapper, thanks for helping to debug.

No idea what caused #20 though, but might be something similar....

Similar cases might benefit from changing the workdir to the directory where the file being linted in is located... direnv comes to mind, but that might be a rabbit hole you'd rather stay away from .....

Yeah the current working directory was the major difference I spotted between the validation and the formatting. It makes the code slightly messier, but functionally seems superior.
Adding this to roadmap. Will close on pushed commit.

Added in 09f02fc. Next release will contain change.