Skip to content

Conversation

tvanriel
Copy link

@tvanriel tvanriel commented Sep 23, 2025

Put an x into the box if that apply:

  • This PR introduces breaking change.
  • This PR fixes a bug.
  • This PR adds new functionality.
  • This PR enhances existing functionality.

Description of your changes

I included supported filenames taken from the opentofu documentation in the terraform_fmt hook.

How can we test changes

.pre-commit-config.yaml

---
repos:
  - repo: https://github.com/antonbabenko/pre-commit-terraform
    rev: 4d456fbbcab75462c105d3e2e3a342bd8820fd2e
    hooks:
      - id: terraform_fmt

main.tofutest.hcl

run {
  assert {
    condition = false 
    error_message = "This test is supposed to fail."
  }
}

main.tf

terraform {
  required_version = "~> 1.10.0"

  required_providers {
    null = {
      source  = "hashicorp/null"
      version = "3.2.4"
    }
  }
}

provider "null" {
  # Configuration options
}

resource "null_resource" "test" {
  x = "1"
  this_is_a_test = "2"
}

Run pre-commit install && pre-commit

  1. Terraform fmt should fail
  2. line 17 in main.tf should have the '=' correctly aligned.
  3. line 3 in main.tofutest.hcl should have the '=' correctly aligned.

Copy link

coderabbitai bot commented Sep 23, 2025

📝 Walkthrough

Summary by CodeRabbit

  • Chores
    • Expanded Terraform formatting coverage in pre-commit to recognize additional HCL test and mock file variants, improving consistency across Terraform-related files.
    • Contributors will see the formatter run on more files by default, reducing manual adjustments and missed linting.
    • CI may show formatting updates on newly covered files.
    • No functional or user-facing changes to the application.

Walkthrough

Updated the .pre-commit-hooks.yaml terraform_fmt hook file-matching regex to broaden accepted HCL test/mock variants by changing the grouped HCL alternatives; no runtime code changed.

Changes

Cohort / File(s) Summary
Pre-commit hook config
\.pre-commit-hooks.yaml
Updated terraform_fmt hook files pattern: before `.(tf

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested labels

bug

Suggested reviewers

  • antonbabenko
  • yermulnik

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title Check ✅ Passed The title "fix(terraform_fmt): Add support for tofutest files" concisely and accurately describes the primary change — adding tofutest filename support to the terraform_fmt hook — and matches the modifications shown in .pre-commit-hooks.yaml, so it is specific and useful for history scanning.
Description Check ✅ Passed The pull request description directly describes the change (adding OpenTofu/tofutest filename support), cites the source documentation, and includes concrete testing instructions and examples that align with the diff, so it is related and sufficient for this lenient check.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ Finishing touches
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 475fb2b and 4d456fb.

📒 Files selected for processing (1)
  • .pre-commit-hooks.yaml (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • .pre-commit-hooks.yaml

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
.pre-commit-hooks.yaml (2)

16-18: Update the comment to reflect OpenTofu additions.

The comment references Terraform’s list only; we’re also adding OpenTofu-specific files. Tighten the comment to avoid confusion.

-  # Supported extensions by Terraform specified in
-  # https://github.com/hashicorp/terraform/blob/0c63fb2b097edcd5cb1a91322765a414206fbea2/internal/command/fmt.go#L30-L35
+  # Supported extensions by Terraform (link below) plus OpenTofu test/mock files.
+  # https://github.com/hashicorp/terraform/blob/0c63fb2b097edcd5cb1a91322765a414206fbea2/internal/command/fmt.go#L30-L35
+  # Also includes: *.tofutest.hcl, *.tofumock.hcl

18-18: Prefer OpenTofu for .tofu/.tofutest files or exclude them from terraform_fmt

terraform_fmt runs "$tf_path" fmt (hooks/terraform_fmt.sh) and common::get_tf_binary_path (hooks/_common.sh) currently prefers the terraform binary over tofu, so when both are present terraform will format .tofu/.tofutest.hcl files matched by .pre-commit-hooks.yaml (files regex). Either change discovery to prefer tofu for tofu-specific extensions, add per-extension selection in terraform_fmt.sh (call tofu for .tofu/.tofutest/.tofumock), or remove those tofu-specific globs from the terraform_fmt hook.

  • .pre-commit-hooks.yaml: files regex includes tftest/tofutest/tfmock/tofumock (line ~18)
  • hooks/_common.sh: common::get_tf_binary_path (selection order)
  • hooks/terraform_fmt.sh: per_dir_hook_unique_part -> "$tf_path" fmt
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 48b3a29 and d4b00d8.

📒 Files selected for processing (1)
  • .pre-commit-hooks.yaml (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-08-12T19:49:13.257Z
Learnt from: actuarysailor
PR: antonbabenko/pre-commit-terraform#925
File: .pre-commit-hooks.yaml:216-227
Timestamp: 2025-08-12T19:49:13.257Z
Learning: In Terraform projects, most module folders are downloaded dependencies (similar to GitHub Actions) rather than locally maintained code. Users typically want to document only the root module to avoid commit noise from modules they consume but don't maintain. The terraform_docs_docker hook's current design with pass_filenames: false and args targeting the current directory (.) is appropriate for this common single-module repository pattern.

Applied to files:

  • .pre-commit-hooks.yaml
🔇 Additional comments (1)
.pre-commit-hooks.yaml (1)

18-18: Approve tofutest addition; confirm mock-file support

Pattern correctly matches .tftest.hcl and .tofutest.hcl; OpenTofu docs list tftest/tofutest but do not document .tfmock/.tofumock — confirm fmt support for mock files or remove them from the pattern.

yermulnik
yermulnik previously approved these changes Sep 23, 2025
Copy link
Collaborator

@yermulnik yermulnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
@MaxymVlasov FYI

@yermulnik yermulnik changed the title fix(terraform_fmt): add support for tofutest files fix(terraform_fmt): Add support for tofutest files Sep 23, 2025
yermulnik
yermulnik previously approved these changes Sep 23, 2025
# Supported extensions by Terraform specified in
# https://github.com/hashicorp/terraform/blob/0c63fb2b097edcd5cb1a91322765a414206fbea2/internal/command/fmt.go#L30-L35
files: \.(tf|tofu|tfvars|tftest\.hcl|tfmock\.hcl)$
files: \.(tf|tofu|tfvars|(tftest|tofutest|tfmock|tofumock)\.hcl)$
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Techincally, .tofumock.hcl does not exist yet, and looks like it will not be implemented soon (as they asked for community PR) - opentofu/opentofu#1778

Suggested change
files: \.(tf|tofu|tfvars|(tftest|tofutest|tfmock|tofumock)\.hcl)$
files: \.(tf|tofu|tfvars|(tftest|tofutest|tfmock)\.hcl)$

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 this pull request may close these issues.

3 participants