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

[terrraform_validate] Script fails for projects with multiple terraform submodules on first run #171

Closed
i-n00b opened this issue Feb 20, 2021 · 1 comment · Fixed by #172

Comments

@i-n00b
Copy link

i-n00b commented Feb 20, 2021

I ran into an issue with terraform_validate when trying to run in on a project with multiple sub-folders containing .tf files. The solution would be to add the flag require_serial: true to the hook.

Project Tree

│   └── modules
│       ├── module1
│       │   ├── main.tf
│       │   └── variables.tf
│       ├── module2
│       │   ├── main.tf
│       │   └── variables.tf
│       └── module3
│           ├── main.tf
│           └── variables.tf

Issue:

The check fails on the first run of pre-commit. However, it passes on re-running it, which led me to suspect an issue with the parallel threading capabilities of pre-commit.

$ pre-commit run --all-files
Terraform validate.......................................................Failed
- hook id: terraform_validate
- exit code: 1

Validation failed: blueprints/modules/module1

Error: Could not load plugin


Plugin reinitialization required. Please run "terraform init".

Plugins are external binaries that Terraform uses to access and manipulate
resources. The configuration provided requires plugins which can't be located,
don't satisfy the version constraints, or are otherwise incompatible.

Terraform automatically discovers provider requirements from your
configuration, including providers used in child modules. To see the
requirements and constraints, run "terraform providers".

3 problems:

- Failed to instantiate provider "registry.terraform.io/hashicorp/google" to
obtain schema: unknown provider "registry.terraform.io/hashicorp/google"
- Failed to instantiate provider "registry.terraform.io/hashicorp/google-beta"
to obtain schema: unknown provider
"registry.terraform.io/hashicorp/google-beta"
- Failed to instantiate provider "registry.terraform.io/hashicorp/random" to
obtain schema: unknown provider "registry.terraform.io/hashicorp/random"



Validation failed: blueprints/modules/module2

Error: Could not load plugin


Plugin reinitialization required. Please run "terraform init".

Plugins are external binaries that Terraform uses to access and manipulate
resources. The configuration provided requires plugins which can't be located,
don't satisfy the version constraints, or are otherwise incompatible.

Terraform automatically discovers provider requirements from your
configuration, including providers used in child modules. To see the
requirements and constraints, run "terraform providers".

Failed to instantiate provider "registry.terraform.io/hashicorp/google" to
obtain schema: the cached package for registry.terraform.io/hashicorp/google
3.57.0 (in .terraform/providers) does not match any of the checksums recorded
in the dependency lock file

Running a second time:

$ pre-commit run --all-files
Terraform validate.......................................................Passed

Reproduction

I downloaded the scripts terraform_validate.sh and lib_optget to my local machine and setup a local hook as follows:

  - repo: local
    hooks:
      - id: terraform_validate
        name: Terraform validate
        description: Validates all Terraform configuration files.
        entry: './test.sh'
        language: script
        files: (\.tf|\.tfvars)$
        exclude: \.terraform\/.*$

I was able to reproduce the problem successfully.

Solution:

Made the following change to the hook:

  - repo: local
    hooks:
      - id: terraform_validate
        name: Terraform validate
        description: Validates all Terraform configuration files.
        entry: './test.sh'
        language: script
        files: (\.tf|\.tfvars)$
        exclude: \.terraform\/.*$
        require_serial: true

After adding require_serial: true, the pre-commit checks passes on the first try!

@antonbabenko
Copy link
Owner

Thanks for the issue report!

v1.46.0 has been just released.

jperson added a commit to CuriBio/IaC that referenced this issue Feb 23, 2021
jperson added a commit to CuriBio/IaC that referenced this issue Feb 24, 2021
jperson added a commit to CuriBio/IaC that referenced this issue Feb 24, 2021
jperson added a commit to CuriBio/IaC that referenced this issue Feb 24, 2021
jperson added a commit to CuriBio/IaC that referenced this issue Feb 24, 2021
jperson added a commit to CuriBio/IaC that referenced this issue Feb 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants