antonbabenko / pre-commit-terraform

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

Terraform validate has "Failed to read module directory" when using nested modules #139

Closed nshenry03 closed 4 years ago

nshenry03 commented 4 years ago

I am using nested modules as recommended by Terraform; however, I was running into this error:

hnicholas@hnicholas-a01:~/Repositories/velocloud-terraform-modules$ pre-commit run
Terraform fmt............................................................Passed
Terraform validate.......................................................Failed
- hook id: terraform_validate
- exit code: 1

Error: Failed to read module directory

Module directory ../../../../../modules/security_groups does not exist or
cannot be read.

Failed path: terraform-aws-velocloud-uag/modules/security_groups
================================

Terraform docs...........................................................Passed
Terraform validate with tflint...........................................Passed
Terraform validate with tfsec............................................Passed

I was able to work around the issue by updating .cache/pre-commit/repo<some random string>/terraform_validate.sh:

--- /Users/hnicholas/.cache/pre-commit/repov0zt08cv/terraform_validate.sh.bak   2020-08-28 17:24:34.000000000 -0600
+++ /Users/hnicholas/.cache/pre-commit/repov0zt08cv/terraform_validate.sh       2020-08-28 17:05:20.000000000 -0600
@@ -97,6 +97,7 @@ terraform_validate_() {

       local validate_path
       validate_path="${path_uniq#"$terraform_path"}"
+      validate_path="${validate_path#/}"

       # Change to the directory that has been initialized, run validation, then
       # change back to the starting directory.

I'm sure there's a cleaner way as part of validate_path="${path_uniq#"$terraform_path"}" but I don't have the time to dig deeper tonight... Hopefully this is an easy fix or this helps others.

Thanks!


Version Information

hnicholas@hnicholas-a01:~/Repositories/velocloud-terraform-modules/terraform-aws-velocloud-vce$ terraform --version
Terraform v0.12.29
+ provider.aws v3.4.0
+ provider.time v0.5.0

Your version of Terraform is out of date! The latest version
is 0.13.1. You can update by downloading from https://www.terraform.io/downloads.html

hnicholas@hnicholas-a01:~/Repositories/velocloud-terraform-modules/terraform-aws-velocloud-vce$ pre-commit --version
pre-commit 2.7.1

hnicholas@hnicholas-a01:~/Repositories/velocloud-terraform-modules/terraform-aws-velocloud-vce$ cat ../.pre-commit-config.yaml
repos:
- repo: git://github.com/antonbabenko/pre-commit-terraform
  rev: v1.35.0
  hooks:
    - id: terraform_fmt
    - id: terraform_validate
    - id: terraform_docs
    - id: terraform_tflint
    - id: terraform_tfsec

hnicholas@hnicholas-a01:~/Repositories/velocloud-terraform-modules/terraform-aws-velocloud-vce$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.15.6
BuildVersion:   19G2021
antonbabenko commented 4 years ago

Hi @nshenry03 !

Thanks for opening this issue.

I think this problem has been described and fixed in #141. Please update to v1.38.0 and tell if the problem is still there.

nshenry03 commented 4 years ago

Awesome, thanks, I'll try it out!