Skip to content

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

Description

@i-n00b

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions