The terraform fmt command offers the ability to pipe a file into stdin and get the formatted result on stdout, which is used by various plugins to help with formatting files correctly in real-time within an editor. I'm working on adding in support for formatting .hcl files with terragrunt but I'm stuck on the problem that you can't use stdin/stdout in this way with terragrunt's hclfmt command.
Can support for this be added?
We don't have a need for this use case internally at gruntwork, but if anyone wants to add a PR for it that would be great!
We actually use the terraform fixers in our editors. E.g here is my vim config to make this work:
au! BufNewFile,BufRead terragrunt.hcl set filetype=terraform syntax=terraform
terraform fmt works just fine with terragrunt.hcl in a pipeline because it can't do the regular filename check when reading from stdin. With that said, there is no guarantee it will continue to keep working, which was why hclfmt was added. So adding the pipeline support for when that breaks would be good to have.
Yeah, I have something similar, but I wasn't certain that it would catch everything. I assumed there was a reason you included an hclfmt instead of just recommending that route, was that incorrect?
hclfmt was added because it works better in a CI pipeline, where you want to find, run, and update all the files from a root directory. In this use case, terraform fmt fails because it does filename checks.
The other reason is what I mentioned in the previous comment:
With that said, there is no guarantee it will continue to keep working, which was why hclfmt was added
This is based on implication that the terraform team was going to start specializing formatting to be specific to terraform: https://github.com/hashicorp/terraform/pull/22092
@mbainter I found rhadley-recurly/vim-terragrunt today which support format on save for hcl files. Does that fill your need?
For the ones who want something better than terragrunt hclfmt, there is an hclfmt command available in the official HCL repo (but there is no prebuilt executable).
FWIW, the hclfmt functionality uses the same functions as the official hclfmt version (https://github.com/gruntwork-io/terragrunt/blob/master/cli/hclfmt.go#L65), with the main difference being that it looks for and only runs on terragrunt config files.
@yorinasub17 yes I should have said "more flexible" instead of "better" because the formatter is indeed the same.