When running terraform fmt
on a directory, it visits not only the files directly in that directory but also subdirectories.
In particular, it visits .terraform/modules
, which contains configurations that come from other sources.
No other Terraform command recursively visits .tf
files in subdirectories, so I think the behavior should be to work on the given directory only, so it visits the same set of files that terraform plan
(for example) would load given the same directory name.
But at the very least the .terraform/modules
directory should be excluded, since the files in there are not under the control of the containing configuration.
At minimum we should exclude .terraform
, and provide a -r
or similar option (looking up the syntax for gofmt
gets me every time).
I definitely appreciate that this command is recursive, I just want the ability to filter out the .terraform
directory.
Can we have something like .gitignore for terraform fmt? It's making "terraform init -upgrade=true" module updates a bit of a chore. Thanks!
I think it would be best if terrraform fmt
considered the same files that plan
or apply
do (at least in the current directory).
If someone help point me to where in the codebase plan and apply get the list of files, I could take a crack at this.
Though I guess that would ignore all dirs, which would probably be too big of a change.
what is the state of ignoring .terraform folders?
Since there ain't no implemented way to ignore the tf cache, I'm using this inside a git repo:
git grep --cached -Il '' -- '*.tf' '*.tfvars' | xargs -L1 terraform fmt -check=true -diff=true -write=false -list=true
This lists all *.tf
and *.tfvars
files, which are part of the git repository and runs fmt
on each of them individually.
Since the .terraform
dirs should be in the .gitignore
, these dirs are ignored.
This is little different from running terraform fmt
on a directory. fmt only jumps on *.tf
files. I wanted it to also jump on *.tfvars
files. If you prefer the standard way, you can just remove the extension from the command.
Here is the same as a pre-commit hook, only checking files that are about to be added/modified.
git diff --cached --name-only --diff-filter=ACM -z -- '*.tf' '*.tfvars' | xargs -L1 terraform fmt -check=true -diff=true -write=false -list=true
Hi all! Sorry for the long silence here.
For the forthcoming Terraform v0.12.0 release, the terraform fmt
command will work only in the single target directory by default (for consistency with other commands) and will have a -recursive
option to enable the recursive behavior.
When -recursive
is enabled, it will _not_ visit any directory whose name starts with .
, including .terraform
, or any descendant directory of those.
To achieve the old behavior of visiting all subdirectories indiscriminately (something which we expect should be rare in practice) it'll be necessary to compose with other tools, such as:
find . -name '*.tf' -printf "%h\n" | uniq | xargs -n1 terraform fmt
Since this behavior is already implemented in the master branch and included in the Terraform v0.12.0-beta1 release, I'm going to close this out. If you give the beta1 implementation a try and find issues with it, please open a new issue so we can capture all of the information from the issue template, rather than leaving follow-up comments here.
terraform fmt
has of course seen other changes as a result of the language features added in this release, so its behavior in our initial release will take a more conservative attitude than the implementation for 0.11 and earlier had. Therefore you may see it make some different decisions in v0.12 than it would've done in v0.11; those minor differences are expected. We may gradually introduce more formatting rules in subsequent releases as we gain experience with how the new larger language tends to be used.
Is there a technical reason why this feature (ignoring .terraform
) can't be backported to TF11?
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.
Most helpful comment
I definitely appreciate that this command is recursive, I just want the ability to filter out the
.terraform
directory.