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

Inconsistent behaviour for Provider version numbers when root module only calls modules. #726

Open
rquadling opened this issue Nov 28, 2023 · 1 comment
Labels

Comments

@rquadling
Copy link

Describe the bug

We have multiple repos for different microservices and all of them use pre-commit with terraform-docs setup as:

  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: v1.83.5
    hooks:
      - id: terraform_fmt
      - id: terraform_validate
      - id: terraform_docs
        args:
          - --hook-config=--path-to-file=README.md
          - --hook-config=--add-to-existing-file=true
          - --hook-config=--create-file-if-not-exist=true

No special options regarding the overriding of terraform-docs.

Our provider setups are per project, and will include different providers, but all will be AWS at v5:

terraform {
  required_version = ">= 1.3.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5"
    }
  }
}

The lock file is also present and always has a single version for the provider:

provider "registry.terraform.io/hashicorp/aws" {
  version     = "5.27.0"
  constraints = "~> 5.0"
  hashes = [
    ...
  ]
}

But, depending upon the repo, the output in the README is different.

This project is TF and AWS only with root tf files managing resources.

## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | ~> 5 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.27.0 |

This project is uses the same providers, but all in modules. Nothing is in the root module.

## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | ~> 5 |

## Providers

No providers.

I've tried with and without --lockfile=true (both on the terraform-docs command line and via pre-commit's - --arg=--lockfile=true).

How can we reproduce it?

Several files are needed. Hopefully enough here to make sense.

.terraform-version (used by tfenv if you are using that - not required otherwise)

1.6.3

.terraform.lock.hcl (I'm not sure how much of the content of this file is required, but here it is)

# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.

provider "registry.terraform.io/hashicorp/aws" {
  version     = "5.27.0"
  constraints = "~> 5.0"
  hashes = [
    "h1:7tlujJ/5W0N3haoGhaoZ4fQ6mTXO5JHSQq7gKvwJ9SY=",
    "h1:NM9c2aiYhbCGX4CcBqNEgAqzMKeY3SQvy+l1lB3GygI=",
    "h1:t67hg2gITLxCfO3sewnrqwExFEnM5VVBhnANKLMWy/w=",
    "zh:096b4f356a8518d601334cb1765c1d317494aefbdd6de089420fc68a3facedf0",
    "zh:0c46222baa052b72f077dfed4f9d06b4c71b8d9fdf0b6c4ab2a8b46a41baeb7a",
    "zh:1339deb2384dfef8b7252fbd76784c512cd74f83bb8337cba96776107a1d904c",
    "zh:18c47662a6d9c8f96b6d7cbf6fca739526123fc83f4b2bcee68e7f8774d4078c",
    "zh:2a323401ca7c752749c9b8dbc2a3f587eebfa49344fde704ae9e692bfdd3b9ee",
    "zh:36f6f9b94bc04dcba5e16038cfbedae24fcb035e5a546cf81c8f7632d9dbc3ca",
    "zh:411d884b8f71be03fedcfcb82ead85871647e3dbf38f7264ce3b3aa84be6ead5",
    "zh:47b185810dec758ca7cdeb5d30baaf8119c346753378e72285d152962ec43855",
    "zh:48b896c8c1aa46f536f398d1b87ec6b5141aa569924ee17f5ee5a928effcd554",
    "zh:6560e1ae0f6a3bf55b2d1d8267d766611c38387a17da19bde66f89412dbf6261",
    "zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425",
    "zh:aa518bd94f94a2e38f2d6ffc25da08fc4fd2b2f38d77becd3c1503720c304584",
    "zh:d8d90bb74c87c8e368ec66f3d214828ab4c24bb70e764bc888207a4ed5783550",
    "zh:da3c76e28342e1af7e06ea3d31a8e76cc6bf0070877beaf58c088008f61c48f9",
    "zh:f403fa79a3d612be7ed4fca9d3df6fb30bc7de890ecbafe7881f38bf9b07c382",
  ]
}

.pre-commit-config.yaml

repos:
  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: v1.83.5
    hooks:
      - id: terraform_fmt
      - id: terraform_validate
        exclude: modules
      - id: terraform_docs
        args:
          - --hook-config=--path-to-file=README.md
          - --hook-config=--add-to-existing-file=true
          - --hook-config=--create-file-if-not-exist=true

make_error.tf

terraform {
  required_version = ">= 1.3.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5"
    }
  }
}

provider "aws" {
  region = "eu-west-1"
}

module "readme_error_maker" {
  source = "./modules/readme_error_maker"
}

output "user" {
  description = "User"
  value       = module.readme_error_maker.user
}

modules/readme_error_maker/main.tf

terraform {
  required_version = ">= 1.3.0"
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5"
    }
  }
}

data "aws_caller_identity" "current" {}

output "user" {
  description = "The current user's ARN"
  value       = data.aws_caller_identity.current.arn
}

Once all those are in place, run pre-commit run --all

$ pre-commit run --all
Terraform fmt............................................................Passed
Terraform validate.......................................................Passed
Terraform docs...........................................................Passed

And the following README.md's are created:

README.md

# readme_error

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | ~> 5 |

## Providers

No providers.

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_readme_error_maker"></a> [readme\_error\_maker](#module\_readme\_error\_maker) | ./modules/readme_error_maker | n/a |

## Resources

No resources.

## Inputs

No inputs.

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_user"></a> [user](#output\_user) | User |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

and modules/readme_error_maker/README.md

# readme_error_maker

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | ~> 5 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | ~> 5 |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |

## Inputs

No inputs.

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_user"></a> [user](#output\_user) | The current user's ARN |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

If I remove both README.mds, and run terraform-docs markdown table . --recursive --output-file README.md instead:

$ terraform-docs markdown table . --recursive --output-file README.md
README.md updated successfully
modules/readme_error_maker/README.md updated successfully

then the only difference is the block markers are now <-- BEGIN_TF_DOCS --> / <-- END_TF_DOCS --> and no heading line. The rest of the content is the same.

Environment information

$ terraform-docs --version
terraform-docs version v0.16.0 darwin/amd64

$ go version
go version go1.18.4 darwin/amd64

All on MacOS. NOTE: I'm not compiling my own version of terraform-docs.

@rquadling rquadling added the bug label Nov 28, 2023
@rquadling
Copy link
Author

rquadling commented Nov 28, 2023

OOI, adding a single output in the root module tf (make_error.tf)

data "aws_caller_identity" "current" {}

output "account" {
  description = "The account ID"
  value       = data.aws_caller_identity.current.account_id
}

"solves" the problem with

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.27.0 |

now being present in the root README.md.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant