treefmt icon indicating copy to clipboard operation
treefmt copied to clipboard

terraform fmt doesn't take files

Open zimbatm opened this issue 3 years ago • 3 comments

terraform fmt doesn't obey the treefmt spec

$ terraform fmt --help
Usage: terraform fmt [options] [DIR]

        Rewrites all Terraform configuration files to a canonical format. Both
        configuration files (.tf) and variables files (.tfvars) are updated.
        JSON files (.tf.json or .tfvars.json) are not modified.

        If DIR is not specified then the current working directory will be used.
        If DIR is "-" then content will be read from STDIN. The given content must
        be in the Terraform language native syntax; JSON is not supported.

Options:

  -list=false    Don't list files whose formatting differs
                 (always disabled if using STDIN)

  -write=false   Don't write to source files
                 (always disabled if using STDIN or -check)

  -diff          Display diffs of formatting changes

  -check         Check if the input is formatted. Exit status will be 0 if all
                 input is properly formatted and non-zero otherwise.

  -no-color      If specified, output won't contain any color.

  -recursive     Also process files in subdirectories. By default, only the
                 given directory (or current directory) is processed.

zimbatm avatar Mar 24 '21 15:03 zimbatm

I found the issue, it only takes one path as an argument.

zimbatm avatar Mar 24 '21 20:03 zimbatm

If you want to help, please :+1: this upstream PR: https://github.com/hashicorp/terraform/pull/28191

zimbatm avatar May 07 '21 19:05 zimbatm

Another workaround is to use hclfmt:

[formatter.hcl]
command = "hclfmt"
options = [
    "-w" # write in place
]
includes = ["*.hcl", "*.tf"]

Note that hclfmt isn't widely distributed. I didn't find it in Homebrew or nixpkgs.

Here is the nix package: https://github.com/NixOS/nixpkgs/pull/182472

zimbatm avatar Jul 22 '22 13:07 zimbatm

@zimbatm Terraform v1.13 got released today and terraform fmt takes a list of files now.

nikolay avatar Sep 22 '22 00:09 nikolay

finally!

zimbatm avatar Sep 22 '22 14:09 zimbatm