-
-
Notifications
You must be signed in to change notification settings - Fork 568
Open
Labels
estimate/1weekNeed 1 work week to be doneNeed 1 work week to be donefeatureNew feature or requestNew feature or request
Description
What problem are you facing?
I cannot use pre-commit from Windows cmd.exe or PowerShell as it depends on bash. Only way is to use "Git Bash"
How could pre-commit-terraform help solve your problem?
I think the bash dependency should go away. Git is used on Windows outside of Git Bash all the time... either directly by developers using PowerShell or cmd.exe... or indirectly by other tools/IDEs, etc.
pre-commit itself is a Python script, so any environment where pre-commit is running necessarily already has Python available. I'd like to see those shell scripts converted to Python. It could use the subprocess module to invoke the Terraform binary.
I'm willing to do this work if you're open to it.
Metadata
Metadata
Assignees
Labels
estimate/1weekNeed 1 work week to be doneNeed 1 work week to be donefeatureNew feature or requestNew feature or request
Activity
yermulnik commentedon Mar 21, 2024
Yep, we're open for contributions. We welcome and encourage them. Especially those of such a brave kind 👍🏻 You may take the https://github.com/antonbabenko/pre-commit-terraform/blob/master/.github/CONTRIBUTING.md as a base point. While it is not quite Windows-aware I guess, it may probably help you with the direction.
MaxymVlasov commentedon Mar 22, 2024
Well, if you'll provide at least the same execution speed as in the shell, we will check your PR. If it will work slower - from README:
[-]Windows Support[/-][+]Windows Support / Rewrite hooks to Python[/+]MaxymVlasov commentedon Mar 22, 2024
I'll recommend building PoC on something simple, like hooks/terraform_fmt.sh, test it execution speed on big repo via
pre-commit run -a
and compare it with current realization, to understand is it worth it at all.If you have no big repo - try to test on a tiny one but you'll need aware of fractions of a second and test it many times in a row. And when you're ready - send PoC PR - I have a huge repo where I'll test it :)
ericfrederich commentedon Mar 22, 2024
Sounds good. Just wanted to make sure you'd be okay with taking on Python as a dependency before attempting a prototype.
To me at least, Python makes more sense of a dependency than Bash does since any environment running pre-commit already has Python but not necessarily Bash.
I'll give it a try.
ericfrederich commentedon Mar 22, 2024
I'm noticing that the
hooks_performance_test.sh
seems biased towards hooks of languagescript
.These times include the overhead of
pre-commit
itself creating a virtual environment for the python based scripts. This is a one-time event and perhaps shouldn't be counted in the performance testing. It even says:I would like to continue, but need to know that you would accept a solution that is performant if you concede the installation time. Unfortunately this is inherent to how
pre-commit try-repo
works, so maybe I need to do some manual installation and use a command other thantry-repo
for thehooks_performance_test.sh
no-op tests
I changed both
terraform_fmt
of languagescript
as well asterraform_docs_replace
of languagepython
to immediatelyexit 0
/sys.exit(0)
. So they actually do nothing, but Python will be penalized like 1.5 seconds5 runs 'dummy script'
5 runs 'dummy python'
yermulnik commentedon Mar 22, 2024
As you already mentioned, Python is a hard dependency for
pre-commit
framework thatpre-commit-terraform
is based on. So we actually can't decline Python =)If you feel you can replace all the shell related bits with Python, as @MaxymVlasov already mentioned above we'll be keen to see whether this can be viable and feasible solution.
It's not about just Bash as there are other CLI utilities in use also. Not talking about the "generic" CLI tools like
terraform
,infracost
,checkov
,terraform-docs
,tflint
, and others, which you'd probably not rewrite in Python, but which you would need to implement logic for if you're targeting to support not just Linux/macOS, but also Windows.ericfrederich commentedon Mar 25, 2024
@yermulnik I'm happy to discuss this here as suggested.
I am confused by asking me to look at
man 1 bash
because while the hook is implemented in bash, these--env-vars
are clearly not interpreted via Bash itself.Use the following args...
... and you'll see the behavior differences between what _common.sh supports and what Bash supports.
--env-vars=DEBUG_START="start
"start
--env-vars=DEBUG_END=end"
end"
--env-vars=DEBUG_MID=mi"d
mi"d
--env-vars=DEBUG_BOTH="both"
"both"
( orboth
if #651 gets merged )both
--env-vars=DEBUG_BARE=bare
bare
bare
--env-vars=DEBUG_MSG1="This is not how "bash" works"
This is not how "bash" works
this is not how bash works
--env-vars=DEBUG_MSG2="this is how \"bash\" works"
this is how \"bash\" works
this is how "bash" works
My point is,
man 1 bash
is not the place to look at what these hooks support.For the purposes of this feature, I just need to know what to support. I can implement it to work exactly the same. I'd actually prefer if these hooks didn't fully support Bash interpretation because that would make this feature impossible. 2 lines of simple bash should be about 2 lines in any language.
So... finally, I'll just ask my question: should my Python implementation exactly mimic the behavior in the _common.sh column in the above table?
terraform_fmt
hook to better support Windows #652MaxymVlasov commentedon Mar 25, 2024
For that case, better choose
how Bash would handle it
interpretation.how _common.sh exports it
looks more like a bug than a feature.yermulnik commentedon Mar 25, 2024
They are: https://github.com/antonbabenko/pre-commit-terraform/blob/master/hooks/_common.sh#L536 (not specifically interpreted, which makes that function do half of the task apparently, though still those vars are handled by Bash to get them exported into env).
All of the existing hooks rely upon Bash specifically (apart from
terraform_docs_replace.py
). Bash is the interpreter of those shell scripts.If I got it right, the gist of this feature is to re-implement hooks in Python. For this goal I'd say you wanted to support the result (altogether with existing user-facing options and features) — if you can replicate what benefits these hooks bring to our users, then the goal is accomplished IMHO.
I think the idea must be to re-implement rather than to replicate. Hence you're through the greenfield and have all the options to re-implement the way it is better for users.
I'm more than certain — again — you should be targeting to implement the result, rather than behavior.
yermulnik commentedon Mar 25, 2024
PS: Export vars the way they should be exported from within any language. As @MaxymVlasov already mentioned, the
_common.sh
way to do it is more of a hacky rather than a proper. We had to had a balance between full interpretation (which would implyeval
which @MaxymVlasov is against of and this makes perfect sense) and the missing feature that our users asked for.github-actions commentedon Apr 25, 2024
terraform_docs
): Fix non-GNUsed
issues, introduced in v1.93.0 #704