antonbabenko / pre-commit-terraform

pre-commit git hooks to take care of Terraform configurations πŸ‡ΊπŸ‡¦
MIT License
3.21k stars 539 forks source link

The `terraform_validate` hook seems to be generating duplicate output #557

Closed avnerv closed 1 year ago

avnerv commented 1 year ago

Describe the bug

terraform_validate hook prints the same error message twice:

Terraform validate.......................................................Failed
- hook id: terraform_validate
- exit code: 1

Error: Unsupported block type

  on locals.tf line 1:
   1: local {

Blocks of type "local" are not expected here. Did you mean "locals"?
Validation failed: hamc/hadc-customer-account/terraform/environment/aws

Error: Unsupported block type

  on locals.tf line 1:
   1: local {

Blocks of type "local" are not expected here. Did you mean "locals"?

How can we reproduce it?

pre-commit run terraform_validate --file=some_tf_file.tf

Environment information

Darwin Avners-MacBook-Pro.local 22.6.0 Darwin Kernel Version 22.6.0: Wed Jul  5 22:21:56 PDT 2023; root:xnu-8796.141.3~6/RELEASE_X86_64 x86_64
GNU bash, version 5.2.2(1)-release (x86_64-apple-darwin21.6.0)
pre-commit 2.21.0
Terraform v1.3.4
python SKIPPED
Python 3.9.7
checkov 2.1.150
terraform-docs version v0.16.0 darwin/amd64
terragrunt SKIPPED
terrascan version: v1.15.2
TFLint version 0.47.0
+ ruleset.terraform (0.4.0-bundled)
tfsec v1.27.5
tfupdate 0.6.6
hcledit 0.2.6
file content ```bash --- repos: - repo: https://github.com/antonbabenko/pre-commit-terraform rev: v1.81.0 hooks: - id: terraform_fmt - id: terraform_validate args: - --args=-no-color # no need to run terraform_validate on the modules exclude: ^(hamc/hadc-customer-account/terraform/modules/|terraform-modules/) - id: terraform_docs args: - --args=--config=.terraform-docs.yml - --hook-config=--path-to-file=README.md - --hook-config=--add-to-existing-file=true - --hook-config=--create-file-if-not-exist=true # duplicated terraform_tflint hook to workaround unused vars # https://h2oai.slack.com/archives/C02PEQYERHS/p1692114976010729?thread_ts=1692114346.807749&cid=C02PEQYERHS - id: terraform_tflint args: - --args=--only=terraform_deprecated_interpolation - --args=--only=terraform_deprecated_index - --args=--only=terraform_unused_declarations - --args=--only=terraform_comment_syntax - --args=--only=terraform_documented_outputs - --args=--only=terraform_documented_variables - --args=--only=terraform_typed_variables - --args=--only=terraform_module_pinned_source - --args=--only=terraform_workspace_remote - --args=--only=terraform_unused_required_providers exclude: ^(hamc/hadc-customer-account/terraform/environment/|hamc/hadc-customer-account/terraform/environment/aws/|hamc/hadc-customer-account/terraform/environment/k8s/|hamc/hadc-customer-account/terraform/environment/applications/) - id: terraform_tflint args: - --args=--only=terraform_deprecated_interpolation - --args=--only=terraform_deprecated_index - --args=--only=terraform_comment_syntax - --args=--only=terraform_documented_outputs - --args=--only=terraform_documented_variables - --args=--only=terraform_typed_variables - --args=--only=terraform_module_pinned_source - --args=--only=terraform_workspace_remote - --args=--only=terraform_unused_required_providers - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.4.0 hooks: - id: check-merge-conflict - id: end-of-file-fixer ```
MaxymVlasov commented 1 year ago

Please provide some_tf_file.tf for minimal reproduction

MaxymVlasov commented 1 year ago

I suppose just

local {}

?

avnerv commented 1 year ago

some_tf_file

I suppose just

local {}

?

yep

MaxymVlasov commented 1 year ago

Not reproduced, for provided by you .pre-commit-config.yaml in Ubuntu. It could be Mac-only bug.

18:09 557 git:(main ?) 
➜ ls -a
.  ..  .git  .pre-commit-config.yaml  main.tf

18:09 557 git:(main ?) 
➜ cat main.tf 
local {}

18:09 557 git:(main ?) 
➜ pre-commit run terraform_validate --file=main.tf
Terraform validate.......................................................Failed
- hook id: terraform_validate
- exit code: 1

Error: Unsupported block type

  on main.tf line 1:
   1: local {}

Blocks of type "local" are not expected here. Did you mean "locals"?
'terraform init' failed, 'terraform validate' skipped: .
There are some problems with the configuration, described below.

The Terraform configuration must be valid before initialization so that
Terraform can determine which modules and providers need to be installed.
β•·
β”‚ Error: Unsupported block type
β”‚ 
β”‚   on main.tf line 1:
β”‚    1: local {}
β”‚ 
β”‚ Blocks of type "local" are not expected here. Did you mean "locals"?
β•΅

Please try next and post here a output:

mkdir /tmp/557 && cd /tmp/557
git init
echo "local {}" > main.tf
echo "
---
repos:
    - repo: https://github.com/antonbabenko/pre-commit-terraform
      rev: v1.81.0
      hooks:
          - id: terraform_fmt
          - id: terraform_validate
            args:
                - --args=-no-color
            # no need to run terraform_validate on the modules
            exclude: ^(hamc/hadc-customer-account/terraform/modules/|terraform-modules/)
          - id: terraform_docs
            args:
                - --args=--config=.terraform-docs.yml
                - --hook-config=--path-to-file=README.md
                - --hook-config=--add-to-existing-file=true
                - --hook-config=--create-file-if-not-exist=true
          # duplicated terraform_tflint hook to workaround unused vars
          # https://h2oai.slack.com/archives/C02PEQYERHS/p1692114976010729?thread_ts=1692114346.807749&cid=C02PEQYERHS
          - id: terraform_tflint
            args:
                - --args=--only=terraform_deprecated_interpolation
                - --args=--only=terraform_deprecated_index
                - --args=--only=terraform_unused_declarations
                - --args=--only=terraform_comment_syntax
                - --args=--only=terraform_documented_outputs
                - --args=--only=terraform_documented_variables
                - --args=--only=terraform_typed_variables
                - --args=--only=terraform_module_pinned_source
                - --args=--only=terraform_workspace_remote
                - --args=--only=terraform_unused_required_providers
            exclude: ^(hamc/hadc-customer-account/terraform/environment/|hamc/hadc-customer-account/terraform/environment/aws/|hamc/hadc-customer-account/terraform/environment/k8s/|hamc/hadc-customer-account/terraform/environment/applications/)
          - id: terraform_tflint
            args:
                - --args=--only=terraform_deprecated_interpolation
                - --args=--only=terraform_deprecated_index
                - --args=--only=terraform_comment_syntax
                - --args=--only=terraform_documented_outputs
                - --args=--only=terraform_documented_variables
                - --args=--only=terraform_typed_variables
                - --args=--only=terraform_module_pinned_source
                - --args=--only=terraform_workspace_remote
                - --args=--only=terraform_unused_required_providers
    - repo: https://github.com/pre-commit/pre-commit-hooks
      rev: v4.4.0
      hooks:
          - id: check-merge-conflict
          - id: end-of-file-fixer
" > .pre-commit-config.yaml 

git add -A
git commit -nm "init"

pre-commit run terraform_validate --file=main.tf
avnerv commented 1 year ago

@MaxymVlasov, got it, even then, I’d expect the second error message to be the output since it contains essential details for parsing.

Validation failed: hamc/hadc-customer-account/terraform/environment/aws

Error: Unsupported block type

  on locals.tf line 1:
   1: local {

Blocks of type "local" are not expected here. Did you mean "locals"?
MaxymVlasov commented 1 year ago

I still don't understand how you get 2nd error msg. Please provide the minimal reproducible file with instructions on how you check it to get 2 same errors

avnerv commented 1 year ago

Please try next and post here a output:

mkdir /tmp/557 && cd /tmp/557
git init
echo "local {}" > main.tf
echo "
---
repos:
    - repo: https://github.com/antonbabenko/pre-commit-terraform
      rev: v1.81.0
      hooks:
          - id: terraform_fmt
          - id: terraform_validate
            args:
                - --args=-no-color
            # no need to run terraform_validate on the modules
            exclude: ^(hamc/hadc-customer-account/terraform/modules/|terraform-modules/)
          - id: terraform_docs
            args:
                - --args=--config=.terraform-docs.yml
                - --hook-config=--path-to-file=README.md
                - --hook-config=--add-to-existing-file=true
                - --hook-config=--create-file-if-not-exist=true
          # duplicated terraform_tflint hook to workaround unused vars
          # https://h2oai.slack.com/archives/C02PEQYERHS/p1692114976010729?thread_ts=1692114346.807749&cid=C02PEQYERHS
          - id: terraform_tflint
            args:
                - --args=--only=terraform_deprecated_interpolation
                - --args=--only=terraform_deprecated_index
                - --args=--only=terraform_unused_declarations
                - --args=--only=terraform_comment_syntax
                - --args=--only=terraform_documented_outputs
                - --args=--only=terraform_documented_variables
                - --args=--only=terraform_typed_variables
                - --args=--only=terraform_module_pinned_source
                - --args=--only=terraform_workspace_remote
                - --args=--only=terraform_unused_required_providers
            exclude: ^(hamc/hadc-customer-account/terraform/environment/|hamc/hadc-customer-account/terraform/environment/aws/|hamc/hadc-customer-account/terraform/environment/k8s/|hamc/hadc-customer-account/terraform/environment/applications/)
          - id: terraform_tflint
            args:
                - --args=--only=terraform_deprecated_interpolation
                - --args=--only=terraform_deprecated_index
                - --args=--only=terraform_comment_syntax
                - --args=--only=terraform_documented_outputs
                - --args=--only=terraform_documented_variables
                - --args=--only=terraform_typed_variables
                - --args=--only=terraform_module_pinned_source
                - --args=--only=terraform_workspace_remote
                - --args=--only=terraform_unused_required_providers
    - repo: https://github.com/pre-commit/pre-commit-hooks
      rev: v4.4.0
      hooks:
          - id: check-merge-conflict
          - id: end-of-file-fixer
" > .pre-commit-config.yaml 

git add -A
git commit -nm "init"

pre-commit run terraform_validate --file=main.tf

the output:

hint: Using 'master' as the name for the initial branch. This default branch name
hint: is subject to change. To configure the initial branch name to use in all
hint: of your new repositories, which will suppress this warning, call:
hint: 
hint:   git config --global init.defaultBranch <name>
hint: 
hint: Names commonly chosen instead of 'master' are 'main', 'trunk' and
hint: 'development'. The just-created branch can be renamed via this command:
hint: 
hint:   git branch -m <name>
Initialized empty Git repository in /private/tmp/557/.git/
[master (root-commit) 121d8bd] init
 2 files changed, 51 insertions(+)
 create mode 100644 .pre-commit-config.yaml
 create mode 100644 main.tf
Terraform validate.......................................................Failed
- hook id: terraform_validate
- exit code: 1

Error: Unsupported block type

  on main.tf line 1:
   1: local {}

Blocks of type "local" are not expected here. Did you mean "locals"?
'terraform init' failed, 'terraform validate' skipped: .
There are some problems with the configuration, described below.

The Terraform configuration must be valid before initialization so that
Terraform can determine which modules and providers need to be installed.
β•·
β”‚ Error: Unsupported block type
β”‚ 
β”‚   on main.tf line 1:
β”‚    1: local {}
β”‚ 
β”‚ Blocks of type "local" are not expected here. Did you mean "locals"?
β•΅

➜  557 git:(master) 
MaxymVlasov commented 1 year ago

Need some-one with Mac to debug and fix it

MaxymVlasov commented 1 year ago

Ah, my bad, it reproducible in linux

MaxymVlasov commented 1 year ago

The first error msg goes from

https://github.com/antonbabenko/pre-commit-terraform/blob/66a1469a7c7954d8469d7a6f79f4ffb8c9ae09bf/hooks/terraform_validate.sh#L116

Second, from

https://github.com/antonbabenko/pre-commit-terraform/blob/66a1469a7c7954d8469d7a6f79f4ffb8c9ae09bf/hooks/terraform_validate.sh#L123

https://github.com/antonbabenko/pre-commit-terraform/blob/66a1469a7c7954d8469d7a6f79f4ffb8c9ae09bf/hooks/_common.sh#L328-L334

If we add to example repo .terraform/modules/ and .terraform/providers/ to suppress second error, we got next:

Terraform validate.......................................................Failed
- hook id: terraform_validate
- exit code: 1

Error: Unsupported block type

  on main.tf line 1:
   1: local {}

Blocks of type "local" are not expected here. Did you mean "locals"?

Validation failed: .

Error: Unsupported block type

  on main.tf line 1:
   1: local {}

Blocks of type "local" are not expected here. Did you mean "locals"?

Which calculated from https://github.com/antonbabenko/pre-commit-terraform/blob/66a1469a7c7954d8469d7a6f79f4ffb8c9ae09bf/hooks/terraform_validate.sh#L130

That means, that we can simply suppress any logs from https://github.com/antonbabenko/pre-commit-terraform/blob/66a1469a7c7954d8469d7a6f79f4ffb8c9ae09bf/hooks/terraform_validate.sh#L116

antonbabenko commented 1 year ago

This issue has been resolved in version 1.83.5 :tada: