hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io
Other
42.77k stars 9.56k forks source link

terraform fmt with `-diff -list=false -check` has empty output #24054

Open matoous opened 4 years ago

matoous commented 4 years ago

When running terraform fmt with following flags: -diff -list=false -check the output is unexpectedly empty.

Terraform Version

❯ terraform -v
Terraform v0.12.20
+ provider.datadog v2.6.0

Terraform Configuration Files

Used in following outputs is this configuration:

resource "datadog_metric_metadata" "http_compression_io" {
  metric = "...http.compression_io"
  short_name = "Bytes before and after compression"
  description = "..."
  type = "rate"
  unit = "byte"
  statsd_interval = var.statsd_interval
}

But the issue is reproducible with any configuration.

Debug Output

Run with -diff only:

❯ terraform fmt -check -diff  terraform/datadog_metrics/main.tf
terraform/datadog_metrics/main.tf
--- old/terraform/datadog_metrics/main.tf
+++ new/terraform/datadog_metrics/main.tf
@@ -30,11 +30,11 @@
 }

 resource "datadog_metric_metadata" "http_compression_io" {
-  metric = "...http.compression_io"
-  short_name = "Bytes before and after compression"
-  description = "..."
-  type = "rate"
-  unit = "byte"
+  metric          = "...http.compression_io"
+  short_name      = "Bytes before and after compression"
+  description     = "..."
+  type            = "rate"
+  unit            = "byte"
   statsd_interval = var.statsd_interval
 }

Run with -list=false only:

❯ terraform fmt -check -list=false terraform/datadog_metrics/main.tf

Run with neither:

❯ terraform fmt -check terraform/datadog_metrics/main.tf
terraform/datadog_metrics/main.tf

Run with both ❗️:

❯ terraform fmt -check -diff -list=false terraform/datadog_metrics/main.tf

Expected Behavior

-diff option already prints the file where the problem was found so the file name is listed unnecessarily twice. -list=false should work the same even when running with -diff, e.g. names of files with errors should not be printed as they are also printed in the diff. This all combined with -check should work as one would expect. E.g. print the diff for not formatted files and fail with non-zero exit code.

Actual Behavior

Running terraform fmt with both -diff and -list=false causes the output to be empty (see Debug output ⬆️ ).

Steps to Reproduce

  1. create fmt error in you .tf file
  2. terraform fmt -check -diff -list=false
  3. observe empty output
martin9700 commented 3 years ago

Sounds like this would be a great linting tool in automation pipelines