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 hook fails when the terraform binary's path contains a space #713

Closed
walkerk1980 opened this issue Sep 5, 2024 · 7 comments · Fixed by #714
Closed

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

walkerk1980 opened this issue Sep 5, 2024 · 7 comments · Fixed by #714
Labels
area/local_installation bug Something isn't working

Comments

@walkerk1980
Copy link

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
$ ${PWD}/path/to/terraform -v
Terraform v1.9.3
on linux_amd64
+ provider registry.terraform.io/hashicorp/null v3.2.2
$ ${PWD}/path/w\ ith/space/terraform -v
Terraform v1.9.3
on linux_amd64
+ provider registry.terraform.io/hashicorp/null v3.2.2
$ 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
$ 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

  • OS:

Bug is present on both Windows and Linux (initially discovered on Windows due to "Program Files" in path).

  • uname -a and/or systeminfo | Select-String "^OS" output:
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
  • Tools availability and versions:
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
  • .pre-commit-config.yaml:
file content
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"]
@walkerk1980 walkerk1980 added area/local_installation bug Something isn't working labels Sep 5, 2024
@travisgan
Copy link

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

@robinbowes
Copy link
Contributor

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

@walkerk1980
Copy link
Author

walkerk1980 commented Sep 5, 2024

@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
Copy link
Collaborator

yermulnik commented Sep 5, 2024

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)

@walkerk1980
Copy link
Author

walkerk1980 commented Sep 5, 2024

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
Copy link
Contributor

#714 updated.

@antonbabenko
Copy link
Owner

This issue has been resolved in version 1.94.2 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/local_installation bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants