Terraform fmt -recursive formats vendor directories

I just ran terraform fmt -recursive and got this output:

$ terraform fmt -recursive
modules/cloudflare/staging/page_rules.tf
node_modules/@financial-times/polyfill-useragent-normaliser/fastly/terraform/main.tf

That first one is a correction in my code. The second is in vendor code. It’s probably not a problem, but it feels a little worrisome to go mucking around in dependencies.

It would be nice if it obeyed .gitignore by default.

Hi @jamesarosen,

Since Terraform isn’t Git, I think it would be surprising to some if it just quietly ignored files not under version control, since a particular file could be excluded from version control for various different reasons.

However, I could see the same logic being applied to Terraform’s own .terraformignore file. Currently that is only to control what subset of files ought to be sent to a remote execution environment in Terraform Cloud, so giving it new meaning would require some care in case there are some interactions I’m not considering, but it is at least something Terraform itself defines.

In the meantime I would suggest not using -recursive if you are running in a mixed language environment where you have directories that are not directly used by your current Terraform directory. That option is a convenience for the common case of a big repository of just Terraform modules, but I don’t expect we will ever add lots of “clever” heuristics to it to try to guess if, for example, there’s a NodeJS application in the same directory as the Terraform module.

In the meantime I would suggest not using -recursive if you are running in a mixed language environment where you have directories that are not directly used by your current Terraform directory.

I’ve found fmt without -recursive to be pretty useless. It strongly disincentivizes good practices like modules.

I could see the same logic being applied to Terraform’s own .terraformignore file

Sounds great to me!

Looking again at your original message, perhaps a reasonable compromise in your case would be to apply terraform fmt -recursive only to the modules directory, so that it will not enter the node_modules directory.

I want to be clear that changing terraform fmt's selection behavior is unlikely to be a priority due to the relatively low impact of that compared to other work, but if you like you could create an enhancement request issue in the Terraform repository so it can participate in the same prioritization process as other enhancement requests.

Thanks!

perhaps a reasonable compromise in your case would be to apply terraform fmt -recursive only to the modules directory

I do have some .tf files in my root, but at least that brings me down to two commands:

$ terraform fmt .
$ terraform fmt -recursive modules/