mschuchard / linter-terraform-syntax

terraform validate linter and formatter for pulsar

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Please add exclude for .tfvars files

vtanjga opened this issue · comments

There is already functionality to exclude .tf files (#11), but my variables are declared in .tfvars. Spaces are added there before and after "=" and that breaks terraform run. It would be great if that file could also be ignored.

Those files are ignored by default unless you specify them as inputs. This is especially true for validate in 0.12. Something else is probably occurring for you.

Looks like it's default behavior of terraform 0.12 fmt and I don't see a way to disable it if executed on directory level.

Ok so this is for auto-formatting TF variables files. I will look into that then. If you could provide a MCVE, that would ensure the patch will include your situation.

Yes auto-formating, sorry for not being specific enough.

Two files are in the same directory.
Here is main.tf file:

provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "web" {
  ami           = var.ami
  instance_type = "t2.micro"

}

variable "ami" {}

config.tfvars file:
ami="ami-085925f297f89fce1"

With "Use Terraform Fmt" option checked and "Exclude Regexp for .tf" filled with config, saving main.tf results in formating config.tfvars so that it looks like this:
ami = "ami-085925f297f89fce1"

Expected behavior: "Exclude Regexp for .tf" would make fmt ignore config.tfvars. It would also be OK to just have checkbox that would exclude all .tfvars files. Or whatever way is easier to implement.

Thanks!

Given your MCVE, I can provide some additional information here.

Files with the .tfvars extension are recognized as Terraform Language files by the Atom package providing that functionality (language-terraform). Due to this, if you used the package config option to exclude them, it will behave as expected.

However, given that you have tried that option and the file is still being auto-formatted, my suspicion would be that you are experiencing this because you opened another file in the same directory as the variables, and that file is recognized as a Terraform file by the language package. At that point, Terraform would also format your variables file because it executes on an entire config directory.

The process would be:

  • Open Terraform file in same directory as variables file
  • linter-terraform-syntax has auto-formatting enabled and no exclude on the opened file
  • linter-terraform-syntax triggers terraform fmt on the directory of the file
  • terraform fmt auto-formats all files in the directory, including the variables file

Basically the situation is that if you open the variable file, it will already be ignored with the regexp exclusion config option. If you open another file in the directory, I do not have scope within the limitations of the Atom and Linter API, and the Terraform CLI, to ignore formatting selected files in the same directory. Therefore, this already works if you open the variables file, and cannot be done if you open another file in the same directory.

I can take a harder look at this to see if some obscure functionality exists to enable this feature, but otherwise my action on this will be to update the spec fixture for auto-formatting with your MCVE.

Indeed, as you described it, saving changes of the file in the same directory triggers formatting of .tfvars file. I don't know the ins of linter-terraform-syntax, but I was hoping that it doesn't trigger terraform fmt on directory, but rather lists all files in a directory and executes fmt on a one file at a time (in which case exclusion regex would work as you would have control over on which files to execute command). Would that be possible?

Unfortunately, terraform fmt can only target a directory, and not a file. You may have luck proposing a feature request on the Terraform issue tracker.

I just tested, changed main.tf and executed terraform fmt main.tf. main.tf is formatted but config.tfvars in the same directory is not. So you can feed fmt with specific files and it won't touch others.

Ok the documentation for the terraform fmt command is wrong. That makes this much easier than me trying to figure out how to format the STDIN and write that stream back out somehow.

Will add config setting to format on dir versus specific file, and then add functionality for each.

Resolved in 031df91. Will be in next relese, but main.js can also be patched into currently installed package locally.