antonbabenko / pre-commit-terraform

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

Terraform hook fails when the terraform binary's path contains a space #713

Closed walkerk1980 closed 1 week ago

walkerk1980 commented 2 weeks ago

Describe the bug

Terraform hook fails when the terraform binary's path contains a space.

How can we reproduce it?

ln -sf $(which terraform) path/to/terraform
ln -sf $(which terraform) path/w\ ith/space/terraform
${PWD}/path/to/terraform -v
${PWD}/path/w\ ith/space/terraform -v
export PCT_TFPATH="${PWD}/path/to/terraform"; pre-commit run --all
export PCT_TFPATH="${PWD}/path/w ith/space/terraform"; pre-commit run --all

Do you have long logs to share? Please use collapsible sections, that can be created via:

LOGS ```bash $ ${PWD}/path/to/terraform -v Terraform v1.9.3 on linux_amd64 + provider registry.terraform.io/hashicorp/null v3.2.2 ``` ```bash $ ${PWD}/path/w\ ith/space/terraform -v Terraform v1.9.3 on linux_amd64 + provider registry.terraform.io/hashicorp/null v3.2.2 ``` ```bash $ export PCT_TFPATH="${PWD}/path/to/terraform"; pre-commit run --all check yaml...............................................................Passed check for added large files..............................................Passed check for case conflicts.................................................Passed check json...........................................(no files to check)Skipped check for merge conflicts................................................Passed check for broken symlinks................................................Passed detect destroyed symlinks................................................Passed detect private key.......................................................Passed terraform-docs...........................................................Passed Terraform fmt............................................................Passed ``` ```bash $ export PCT_TFPATH="${PWD}/path/w ith/space/terraform"; pre-commit run --all check yaml...............................................................Passed check for added large files..............................................Passed check for case conflicts.................................................Passed check json...........................................(no files to check)Skipped check for merge conflicts................................................Passed check for broken symlinks................................................Passed detect destroyed symlinks................................................Passed detect private key.......................................................Passed terraform-docs...........................................................Passed Terraform fmt............................................................Failed - hook id: terraform_fmt - exit code: 127 /home/user1/.cache/pre-commit/repo2_2krz94/hooks/terraform_fmt.sh: line 52: /home/user1/work/applications/it_infra_tf_ccoe_app_bstrp_test/path/w: No such file or directory /home/user1/.cache/pre-commit/repo2_2krz94/hooks/terraform_fmt.sh: line 52: /home/user1/work/applications/it_infra_tf_ccoe_app_bstrp_test/path/w: No such file or directory /home/user1/.cache/pre-commit/repo2_2krz94/hooks/terraform_fmt.sh: line 52: /home/user1/work/applications/it_infra_tf_ccoe_app_bstrp_test/path/w: No such file or directory /home/user1/.cache/pre-commit/repo2_2krz94/hooks/terraform_fmt.sh: line 52: /home/user1/work/applications/it_infra_tf_ccoe_app_bstrp_test/path/w: No such file or directory /home/user1/.cache/pre-commit/repo2_2krz94/hooks/terraform_fmt.sh: line 52: /home/user1/work/applications/it_infra_tf_ccoe_app_bstrp_test/path/w: No such file or directory /home/user1/.cache/pre-commit/repo2_2krz94/hooks/terraform_fmt.sh: line 52: /home/user1/work/applications/it_infra_tf_ccoe_app_bstrp_test/path/w: No such file or directory /home/user1/.cache/pre-commit/repo2_2krz94/hooks/terraform_fmt.sh: line 52: /home/user1/work/applications/it_infra_tf_ccoe_app_bstrp_test/path/w: No such file or directory /home/user1/.cache/pre-commit/repo2_2krz94/hooks/terraform_fmt.sh: line 52: /home/user1/work/applications/it_infra_tf_ccoe_app_bstrp_test/path/w: No such file or directory Terraform validate.......................................................Failed - hook id: terraform_validate - exit code: 127 'terraform init' failed, '/home/user1/work/applications/it_infra_tf_ccoe_app_bstrp_test/path/w ith/space/terraform validate' skipped: . /home/user1/.cache/pre-commit/repo2_2krz94/hooks/_common.sh: line 539: /home/user1/work/applications/it_infra_tf_ccoe_app_bstrp_test/path/w: No such file or directory Validation failed: terraform /home/user1/.cache/pre-commit/repo2_2krz94/hooks/terraform_validate.sh: line 135: /home/user1/work/applications/it_infra_tf_ccoe_app_bstrp_test/path/w: No such file or directory ```

Environment information

Linux iac-dev-box-user1 6.5.0-1025-azure #26~22.04.1-Ubuntu SMP Thu Jul 11 22:33:04 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
pre-commit 3.6.0
OpenTofu v1.8.0
Terraform v1.9.3
python SKIPPED
Python 3.10.12
checkov 3.2.140
infracost SKIPPED
terraform-docs SKIPPED
terragrunt SKIPPED
terrascan SKIPPED
TFLint version 0.52.0
+ ruleset.terraform (0.8.0-bundled)
tfsec SKIPPED
trivy SKIPPED
tfupdate SKIPPED
hcledit SKIPPED
file content ```yaml repos: - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.6.0 hooks: # - id: trailing-whitespace # - id: end-of-file-fixer - id: check-yaml - id: check-added-large-files - id: check-case-conflict - id: check-json - id: check-merge-conflict - id: check-symlinks - id: destroyed-symlinks # - id: detect-aws-credentials - id: detect-private-key - repo: https://github.com/terraform-docs/terraform-docs rev: "v0.18.0" hooks: - id: terraform-docs-go args: ["markdown", "--output-file", "../README.md", "terraform"] - repo: https://github.com/antonbabenko/pre-commit-terraform rev: "v1.94.1" hooks: - id: terraform_fmt args: ["terraform"] - id: terraform_validate args: ["terraform"] ```
travisgan commented 2 weeks ago

Issue appears to be started occurring from v1.90.0 when custom tf / tofu path is introduced.

robinbowes commented 2 weeks ago

@walkerk1980 Can you please try the fix in #714?

walkerk1980 commented 2 weeks ago

@walkerk1980 Can you please try the fix in #714?

The quoting $tf_path on line 52 of hooks/terraform_fmt.sh does indeed fix the first instance of the error.

'terraform init' failed, '/home/user1/work/applications/it_infra_tf_ccoe_app_bstrp_test/path/w ith/space/terraform validate' skipped: .
/home/user1/.cache/pre-commit/reposq7m00vx/hooks/_common.sh: line 539: /home/user1/work/applications/it_infra_tf_ccoe_app_bstrp_test/path/w: No such file or directory

Validation failed: terraform
/home/user1/.cache/pre-commit/reposq7m00vx/hooks/terraform_validate.sh: line 135: /home/user1/work/applications/it_infra_tf_ccoe_app_bstrp_test/path/w: No such file or directory

My errors are fixed entirely after quoting all unquoted instances of $tf_path in both hooks/_common.sh and hooks/terraform_validate.sh. Note that I did not need to quote all instances to get successful result, only the line numbers mentioned in the errors above, but was still successful after quoting all instances of the variable that were not within double quotes.

yermulnik commented 2 weeks ago

My errors are fixed entirely after quoting all unquoted instances of $tf_path in both hooks/_common.sh and hooks/terraform_validate.sh. Note that I did not need to quote all instances to get successful result, only the line numbers mentioned in the errors above, but was still successful after quoting all instances of the variable that were not within double quotes.

Would you mind contributing fix via PR (if @robinbowes doesn't object as he already started the #417)?

«quoting all instances of the variable that were not within double quotes» — this is the way to go if you go with PR. Thanks

PS: https://github.com/antonbabenko/pre-commit-terraform/pull/714#issuecomment-2332565074

walkerk1980 commented 2 weeks ago

My errors are fixed entirely after quoting all unquoted instances of $tf_path in both hooks/_common.sh and hooks/terraform_validate.sh. Note that I did not need to quote all instances to get successful result, only the line numbers mentioned in the errors above, but was still successful after quoting all instances of the variable that were not within double quotes.

Would you mind contributing fix via PR (if @robinbowes doesn't object as he already started the #417)?

«quoting all instances of the variable that were not within double quotes» — this is the way to go if you go with PR. Thanks

PS: #714 (comment)

Sure if the existing PR does not get updated by @robinbowes I can probably submit a PR tomorrow.

robinbowes commented 2 weeks ago

714 updated.

antonbabenko commented 1 week ago

This issue has been resolved in version 1.94.2 :tada: