How can I get more detailed error messages hooks failing a fmt -check?

Last updated: July 22, 2025

Context

When running Before Stack Hooks in the Planning phase with commands like `tofu fmt -check` and `tofu validate`, the error messages can be quite minimal, showing only an exit code without specific details about what failed. This makes it difficult to identify and fix formatting or validation issues without prior experience with the errors.

Answer

To get more detailed error messages when running format checks in Before Stack Hooks, add the `-diff` flag to your `tofu fmt -check` command. The complete command should be:

tofu fmt -check -diff

This will provide a detailed diff output showing exactly what formatting changes are needed, including:

  • File names that need formatting

  • Line-by-line comparisons showing:

    • Current formatting (-)

    • Required formatting (+)

    • Line numbers where changes are needed

Example output:

provider.tf
--- old/provider.tf
+++ new/provider.tf
@@ -1,7 +1,7 @@
 terraform {
   required_providers {
     aws = {
-      source  = "hashicorp/aws"
+      source = "hashicorp/aws"
     }
   }
 }

This detailed output makes it much easier to identify and fix formatting issues in your Terraform configuration files.