Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform-fmt hook behaviour change #48

Open
MGough opened this issue Dec 21, 2020 · 10 comments · May be fixed by #53
Open

terraform-fmt hook behaviour change #48

MGough opened this issue Dec 21, 2020 · 10 comments · May be fixed by #53
Labels
enhancement New feature or request

Comments

@MGough
Copy link

MGough commented Dec 21, 2020

Previously the terraform-fmt hook made changes to the files, whereas now it only shows the difference and errors out. I did some digging and found that it was an intentional change as part of this PR: #46

It would be nice to have the old behaviour as an option. Generally my terraform files are syntactically valid, but incorrectly formatted, the previous behaviour took away the pain point of needing to run terraform fmt after every change. The differences don't need to be analysed, so there's no need for the --diff --check.

Thanks for providing these hooks, they've been super useful!

@brikis98 brikis98 added enhancement New feature or request help wanted labels Jan 6, 2021
@brikis98
Copy link
Member

brikis98 commented Jan 6, 2021

A PR to support some sort of flag that enables the old behavior (overwriting the files) is welcome!

@milanof-huma
Copy link

milanof-huma commented Feb 18, 2021

@brikis98 do you think the following can solve the problem rather than a flag:
terraform-fmt.sh can run terraform fmt
terraform-diff.sh can run terraform fmt -diff -check

@skoblenick
Copy link

I would agree the changes to format in v0.1.12 don't make sense.

If I wanted to know all the errors I would have run lint or validate on the files first. IMHO running format should just auto-correcting issues as the formatter deems fit. It should fail on invalid syntax or logic issues immediately so they can be fixed and need to be re-run, hence why you should validate first if you need to. I don't care about issues it finds regarding spacing, etc that is why I am using the auto-formatter.

Also the diff may matter from the perspective of debugging a CI or a dry-run perspective, but for the default it doesn't. Changes should happen immediately on disk.

I would propose the default behaviour be restored and if you don't want changes made on disk a --dry-run flag be added to the arguments for the hook.

@uberjew666
Copy link

+1 for restoring previous functionality

@christiansaiki
Copy link

Yeah at the moment the way to go is to use v0.1.11 in the meantime.
Maybe in the future I can try to implement @skoblenick approach if you guys agree.

@tyron tyron linked a pull request Aug 9, 2021 that will close this issue
@tyron
Copy link

tyron commented Aug 9, 2021

@brikis98 I filed a PR that allows for both options via a flag (--no-autofix). I reverted the default behaviour to what used to be in place in v0.1.11 as I thought this is more in-line with other pre-commit hooks (e.g. from pre-commit/pre-commit-hooks: trailing-whitespace, sort-simple-yaml, end-of-file-fixer).

Obviously we can invert the logic pretty easily if needed, but I'd like to hear your thoughts first!

@tyron
Copy link

tyron commented Sep 17, 2021

A PR to support some sort of flag that enables the old behavior (overwriting the files) is welcome!

@brikis98 do you think you can review my PR #53 ? Thanks!

topher200 added a commit to memfault/pre-commit that referenced this issue Jan 27, 2022
 ### Issues Addressed
Most of our pre-commit checks just make the changes that are rote,
obvious changes. Not `terraform fmt`! And there's no reason for it, it's
just someone decided to do it that way and the maintainers won't switch
it back. Comment thread:
gruntwork-io#48

 ### Summary of Changes
I patched precommit to use the expected behavior. I included a shell
file to add the patch so it's easy to stay up-to-date with upstream.

 ### Test Plan
- Tested that precommit now automatically updates terraform files when
  using this branch for precommit.
topher200 added a commit to memfault/pre-commit that referenced this issue Jan 27, 2022
 ### Issues Addressed
Most of our pre-commit checks just make the changes that are rote, obvious
changes. Not `terraform fmt`! Instead, it just reports the error and exits,
meaning you have to fix the issue manually. And there's no reason for it, it's
just someone decided to do it that way and the maintainers won't switch it
back. Comment thread: gruntwork-io#48

 ### Summary of Changes
I patched precommit to use the expected behavior. I included a shell
file to add the patch so it's easy to stay up-to-date with upstream.

 ### Test Plan
- Tested that precommit now automatically updates terraform files when
  using this branch for precommit.
@Tomasz-Kluczkowski
Copy link

is this going to be released any time soon? 0.1.17 still does not actually modify the files?

@Tlunch
Copy link

Tlunch commented Feb 26, 2024

Is there any further thoughts on this behavior being added even as an optional argument?

@jannikmi
Copy link

I found this functionality in another repository:
https://github.com/antonbabenko/pre-commit-terraform?tab=readme-ov-file#terraform_fmt

    - repo: https://github.com/antonbabenko/pre-commit-terraform
      rev: v1.92.2
      hooks:
          - id: terraform_fmt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.