Skip to content

Conversation

@MaxymVlasov
Copy link
Collaborator

@MaxymVlasov MaxymVlasov commented Nov 28, 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

Fixes #949
Related #54

How can we test changes

repos:
- repo: https://github.com/antonbabenko/pre-commit-terraform
  rev: f74b9d750396da7ac2b40b99d72d80759576e1ba
  hooks:

    - id: terraform_providers_lock
      args:
      - --hook-config=--mode=only-check-is-current-lockfile-cross-platform
      - --args=-platform=darwin_amd64
      - --args=-platform=linux_amd64

Copilot AI review requested due to automatic review settings November 28, 2025 17:04
@coderabbitai
Copy link

coderabbitai bot commented Nov 28, 2025

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced error handling for Terraform providers lock step with clear, colored error messages providing guidance on required initialization steps when failures occur.

✏️ Tip: You can customize this high-level summary in your review settings.

Walkthrough

Adds runtime error handling to the Terraform providers lock hook: when terraform providers lock exits non‑zero the script prints a red error message with guidance to run terraform init (possibly via terraform_validate) and returns the original exit code.

Changes

Cohort / File(s) Change Summary
Error handling enhancement
hooks/terraform_providers_lock.sh
After running terraform providers lock, detect non‑zero exit code, print a colored red error message including guidance to run terraform init (note about terraform_validate), and return the original exit code. Previously the error returned silently without the message.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

  • Single-file change with small added conditional and message output.
  • Check hooks/terraform_providers_lock.sh for message formatting and exit-code preservation.

Suggested reviewers

  • antonbabenko
  • yermulnik

Pre-merge checks and finishing touches

❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Title check ❓ Inconclusive The title mentions 'Logging in which folder hook failed' but the actual change adds error handling and exit code preservation, not logging folder information. Consider revising the title to accurately reflect the primary change: error handling with guidance messages rather than folder logging.
✅ Passed checks (4 passed)
Check name Status Explanation
Description check ✅ Passed The PR description references the related issues (#949 and #54) and provides test configuration showing how to validate the changes.
Linked Issues check ✅ Passed The changes implement the core requirement from issue #949: capturing the exit code from lockfile_contains_all_needed_sha and returning it immediately in only-check mode.
Out of Scope Changes check ✅ Passed All changes are scoped to the terraform_providers_lock.sh hook and directly address the error handling and exit code requirements specified in issue #949.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch GH-949

📜 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 0dd21f0 and f74b9d7.

📒 Files selected for processing (1)
  • hooks/terraform_providers_lock.sh (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • hooks/terraform_providers_lock.sh
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
  • GitHub Check: 🧹 Linters / pre-commit@🐍3.13@ubuntu-latest
  • GitHub Check: 🧪 Tests / pytest@🐍3.12@macos-15
  • GitHub Check: 🧪 Tests / pytest@🐍3.10@macos-15
  • GitHub Check: 🧪 Tests / pytest@🐍3.14@macos-15
  • GitHub Check: 🧪 Tests / pytest@🐍3.12@macos-15-intel
  • GitHub Check: 🧪 Tests / pytest@🐍3.14@macos-15-intel

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.

@MaxymVlasov MaxymVlasov changed the title fix(terraform_providers_lock): Log in which folder hook failed fix(terraform_providers_lock): Logging in which folder hook failed Nov 28, 2025
Copilot finished reviewing on behalf of MaxymVlasov November 28, 2025 17:05
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR enhances error logging for the terraform_providers_lock hook by displaying which folder the hook failed in, making it easier to debug issues when working with multiple directories.

Key Changes

  • Added error message with directory path when terraform providers lock command fails
  • Included helpful guidance pointing users to common resolution (running terraform init)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
hooks/terraform_providers_lock.sh (1)

149-153: Critical: Core issue #949 is not addressed—early-exit logic still allows fallthrough to terraform providers lock.

The conditional at lines 149–153 remains unchanged and still has the fundamental flaw described in issue #949. When mode == "only-check-is-current-lockfile-cross-platform" and lockfile_contains_all_needed_sha returns non-zero (invalid/incomplete lockfile), the condition evaluates to false, execution continues to line 158, and the hook runs terraform providers lock anyway—triggering unwanted terraform init.

Per issue #949, this block should be restructured to always exit when in that mode, using the exit code from the lockfile check:

- if [ "$mode" == "only-check-is-current-lockfile-cross-platform" ] &&
-   lockfile_contains_all_needed_sha "$platforms_count"; then
-
-   exit 0
- fi
+ if [ "$mode" == "only-check-is-current-lockfile-cross-platform" ]; then
+   lockfile_contains_all_needed_sha "$platforms_count"
+   exit_code=$?
+   return $exit_code
+ fi

This ensures the hook does not run terraform providers lock (and avoid cascading terraform init) when only checking cross-platform lockfile status in check-only mode.

🧹 Nitpick comments (1)
hooks/terraform_providers_lock.sh (1)

161-165: Error message text has grammar and clarity issues.

Minor improvements to user-facing messaging:

  • Line 162–163: "you didn't run requiring 'terraform init'" → awkward phrasing
  • Suggested revision: "you likely skipped 'terraform init'" or "you need to run 'terraform init' first"
- common::colorify "red" "$dir_path run failed. Detailed error above.
- Most common issue is that you didn't run requiring 'terraform init' before running this hook. It can be run by 'terraform_validate' hook - https://github.com/antonbabenko/pre-commit-terraform#terraform_validate
- "
+ common::colorify "red" "$dir_path run failed. See details above.
+ The most common cause is missing 'terraform init'. You can run it via the terraform_validate hook: https://github.com/antonbabenko/pre-commit-terraform#terraform_validate
+ "
📜 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 cba8773 and 0dd21f0.

📒 Files selected for processing (1)
  • hooks/terraform_providers_lock.sh (1 hunks)
🧰 Additional context used
🧠 Learnings (3)
📓 Common learnings
Learnt from: jannis-a
Repo: antonbabenko/pre-commit-terraform PR: 933
File: hooks/terragrunt_providers_lock.sh:18-21
Timestamp: 2025-09-13T19:34:00.317Z
Learning: In terragrunt hooks, the `--` separator should only be added before Terraform commands that are invoked through Terragrunt (like `providers lock`, `validate`), but not before native Terragrunt commands (like `hcl validate`). The separator tells Terragrunt to pass subsequent arguments to the underlying Terraform command, which is not applicable for Terragrunt's own commands.
📚 Learning: 2025-09-13T19:34:00.317Z
Learnt from: jannis-a
Repo: antonbabenko/pre-commit-terraform PR: 933
File: hooks/terragrunt_providers_lock.sh:18-21
Timestamp: 2025-09-13T19:34:00.317Z
Learning: In terragrunt hooks, the `--` separator should only be added before Terraform commands that are invoked through Terragrunt (like `providers lock`, `validate`), but not before native Terragrunt commands (like `hcl validate`). The separator tells Terragrunt to pass subsequent arguments to the underlying Terraform command, which is not applicable for Terragrunt's own commands.

Applied to files:

  • hooks/terraform_providers_lock.sh
📚 Learning: 2025-08-12T19:49:13.257Z
Learnt from: actuarysailor
Repo: antonbabenko/pre-commit-terraform PR: 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:

  • hooks/terraform_providers_lock.sh
🧬 Code graph analysis (1)
hooks/terraform_providers_lock.sh (1)
hooks/_common.sh (1)
  • common::colorify (443-464)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
  • GitHub Check: 🧪 Tests / pytest@🐍3.14@windows-2025
  • GitHub Check: 🧪 Tests / pytest@🐍3.14@macos-15
  • GitHub Check: 🧪 Tests / pytest@🐍3.12@macos-15
  • GitHub Check: 🧪 Tests / pytest@🐍3.13@macos-15
  • GitHub Check: 🧪 Tests / pytest@🐍3.11@windows-2025
  • GitHub Check: 🧪 Tests / pytest@🐍3.11@macos-15
  • GitHub Check: 🧪 Tests / pytest@🐍3.10@macos-15
  • GitHub Check: pre-commit
🔇 Additional comments (1)
hooks/terraform_providers_lock.sh (1)

160-168: Error propagation and return flow are correctly implemented.

The exit code is properly captured, conditionally logged with helpful context, and returned. This ensures failures bubble up correctly and users see actionable guidance.

Co-authored-by: George Yermulnik (Georgii Iermulnik) <[email protected]>
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.

terraform_providers_lock produces error when terraform_validate is not called before

3 participants