antonbabenko / pre-commit-terraform

pre-commit git hooks to take care of Terraform configurations 🇺🇦
MIT License
3.21k stars 539 forks source link

A change to 1 file is causing all files in the tf configuration to get formatted #499

Closed jamiekt closed 1 year ago

jamiekt commented 1 year ago

Describe the bug

I have changed 1 file in my terraform configuration.

echo "\n" >> terraform/scm-data/resources.tf

diff is showing one change:

diff --git a/terraform/scm-data/resources.tf b/terraform/scm-data/resources.tf
index 7507646e..1c947cf2 100644
--- a/terraform/scm-data/resources.tf
+++ b/terraform/scm-data/resources.tf
@@ -236,3 +236,5 @@ output "storage_aws_external_id" {
 output "storage_aws_iam_user_arn" {
   value = snowflake_storage_integration.scm-data-intfood-export-live.storage_aws_iam_user_arn
 }
+
+
image

However I then commit my small change and pre-commit formats all the files in the directory:

>  git commit -am "message"       
Terraform fmt............................................................Failed
- hook id: terraform_fmt
- files were modified by this hook

data_strategy_procedures.tf
data_strategy_schemas.tf
data_strategy_sf_account_usage_views.tf
data_strategy_tables.tf
data_strategy_tasks.tf
data_strategy_views.tf
employee_cooked_tables.tf
employee_cooked_tasks.tf
employee_workday_streams.tf
employee_workday_tables.tf
employee_workday_tasks.tf
highjump_employee_table.tf
katana_storage_streams.tf
katana_storage_table_adl.tf
katana_storage_table_ccworkbook.tf
katana_storage_table_odl.tf
katana_storage_table_pdl.tf
katana_storage_task_ccworkbook.tf
katana_storage_task_odl.tf
katana_storage_task_pdl.tf
katana_storage_view_ccworkbook.tf
labor_management_storage_integrations.tf
labor_planning_dashboard_views.tf
labor_planning_procedure.tf
labor_planning_stream.tf
labor_planning_table_lookup.tf
labor_planning_task.tf
labor_tracking_export.tf
labor_tracking_storage_integration.tf
labor_tracking_table.tf
labor_tracking_test_tbl.tf
labor_view.tf
resources.tf
s3_integration_stage.tf
s3_integration_stage_na_account.tf
shn_notification.tf
shn_streams.tf
shn_table_creation.tf
shn_task.tf
shn_view.tf
snowpipes.tf
streams.tf
supplier_bid_tool_snowpipes.tf
supplier_bid_tool_streams.tf
supplier_bid_tool_tables.tf
supplier_bid_tool_tasks.tf
supplier_bid_tool_views.tf
tables.tf
tasks.tf
terraform.tf
views.tf
zipmap_integration.tf
zipmap_stream.tf
zipmap_table.tf
zipmap_task.tf

Ostensibly its not a big problem but there are folks on my team that are uncomfortable making so many changes in a single commit when all they really want to do is make a minor change to one file/

How can we reproduce it?

Not sure that you can. I've used this pre-commit hook many times in the past and never experienced this before. I have attempted to reproduce it by running terraform fmt manually but I didn't get the same behaviour.

Environment information

Darwin MacBook-Pro-5 22.2.0 Darwin Kernel Version 22.2.0: Fri Nov 11 02:04:44 PST 2022; root:xnu-8792.61.2~4/RELEASE_ARM64_T8103 arm64
GNU bash, version 3.2.57(1)-release (arm64-apple-darwin22)
pre-commit 2.21.0
Terraform v1.3.7
Python 3.10.4
Python 3.10.4
checkov checkov SKIPPED
terraform-docs version v0.16.0 darwin/arm64
terragrunt SKIPPED
terrascan terrascan SKIPPED
TFLint version 0.45.0
+ ruleset.terraform (0.2.2-bundled)
tfsec tfsec SKIPPED
tfupdate tfupdate SKIPPED
hcledit hcledit SKIPPED
file content ```bash --- repos: - repo: https://github.com/antonbabenko/pre-commit-terraform rev: v1.77.1 hooks: - id: terraform_fmt - id: terraform_validate - id: terraform_tflint - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.4.0 hooks: - id: check-yaml - id: end-of-file-fixer - id: trailing-whitespace - id: check-added-large-files - id: forbid-new-submodules - id: pretty-format-json args: - --no-sort-keys - --autofix # pretty-format-yaml from # https://github.com/macisamuele/language-formatters-pre-commit-hooks # doesn't jive well with yamllint which is used in super-linter, so # following the advice at # https://github.com/jumanjihouse/pre-commit-hook-yamlfmt#combine-with-yamllint # to use the following instead - repo: https://github.com/adrienverge/yamllint.git rev: v1.29.0 hooks: - id: yamllint args: [--format, parsable, --strict] - repo: https://github.com/jumanjihouse/pre-commit-hook-yamlfmt rev: 0.2.2 hooks: - id: yamlfmt - repo: https://github.com/psf/black rev: 23.1.0 hooks: - id: black - repo: https://github.com/pycqa/flake8 rev: 6.0.0 hooks: - id: flake8 args: - --max-line-length=110 - repo: https://github.com/pre-commit/mirrors-mypy rev: v1.0.1 hooks: - id: mypy name: mypy src files: src ```
antonbabenko commented 1 year ago

pre-commit is verifying/updating all files matching, not just files you've modified.

To avoid confusion in the future, ask your teammates to install pre-commit and use it every time. It will not change all the files unless there are changes in them.

jamiekt commented 1 year ago

Appreciate the quick response @antonbabenko. I'm afraid I don't understand this though:

pre-commit is verifying/updating all files matching, not just files you've modified.

What do you mean by "all files matching"? Only one file has been changed, so what are these files "matching"?

antonbabenko commented 1 year ago

I mean that terraform_fmt hook scans files defined here - all .tf files.

jamiekt commented 1 year ago

I mean that terraform_fmt hook scans files defined here - all .tf files.

got it, thank you. And donation incoming, hugely appreciate what you do here.

MaxymVlasov commented 1 year ago

By the way, @jamiekt feel free to add pre-commit to your CI, then you will force everyone to use pre-commit at least on PR creation stage.

IE: https://github.com/antonbabenko/pre-commit-terraform/blob/master/.github/workflows/pre-commit.yaml

jamiekt commented 1 year ago

Ooo interesting. Thx Maxym