tofu-fmt
Format infrastructure-as-code configuration files
TLDR
Format the configuration in the current directory
Format the configuration in the current directory and subdirectories
Display diffs of formatting changes
Do not list files that were formatted to stdout
SYNOPSIS
tofu fmt [options] [path...]
PARAMETERS
path...
One or more paths to files or directories to format. If no paths are specified, the current directory is assumed.
-check
Check if the input files are already formatted. Returns exit status 0 if all files are formatted, 1 otherwise. Does not write changes.
-diff
Display diffs of what would be changed by the formatting. Does not write changes.
-write=false
Do not write changes to disk. This option is implied by -check and -diff.
-list=false
Do not list files whose formatting would change. This option is implied by -diff.
-recursive
Recurse into subdirectories when formatting. This is useful for formatting an entire project.
-no-color
Disable color in the output, particularly useful when piping output to other commands or files.
-compact=false
Do not compact the output. By default, tofu fmt compacts output where appropriate, such as within lists.
-width=N
Set the column limit for formatting. Arguments will wrap if they exceed this width.
DESCRIPTION
The tofu fmt command is a specialized utility within the OpenTofu ecosystem designed to automatically rewrite OpenTofu configuration files to a canonical format. It standardizes indentation, spacing, and ordering of elements within HCL (HashiCorp Configuration Language) and JSON configuration files. By ensuring a consistent code style, tofu fmt significantly improves the readability of configuration files, simplifies code reviews, and reduces noise in version control diffs. It is an idempotent operation, meaning that running it multiple times on an already formatted file will not introduce further changes. This command can operate on specific files or entire directories, recursing through subdirectories if specified. It is commonly used in development workflows and CI/CD pipelines to enforce coding standards and maintain project consistency.
CAVEATS
The tofu fmt command modifies files in place by default, which can lead to data loss if not used carefully or without version control. While it aims for consistent formatting, it might reorder certain blocks or arguments in a way that, though functionally identical, might appear unexpected at first glance. It is primarily designed for HCL and JSON configuration files specifically used by OpenTofu and other HashiCorp tools, not general-purpose text files.
IDEMPOTENCY
tofu fmt is an idempotent command. Running it multiple times on the same input files will produce the same formatted output, provided no manual changes have occurred between runs. This characteristic makes it safe and reliable for automated workflows.
EXIT CODES
The command typically exits with 0 on success. If -check is used, it exits with 1 if any files are unformatted. Other non-zero exit codes may indicate errors during execution.
INTEGRATION WITH VERSION CONTROL
It is common practice to integrate tofu fmt into pre-commit hooks or CI/CD pipelines to automatically format code before commits or deployments, ensuring that all code committed to a repository adheres to the defined style.
HISTORY
The tofu fmt command is a direct descendant of the terraform fmt command, which was originally introduced to standardize the formatting of Terraform configuration files. When OpenTofu forked from Terraform, it inherited much of its functionality, including the robust formatting engine. This command has been a staple in the Infrastructure as Code ecosystem, widely adopted for its ability to enforce consistency across large codebases, integrate seamlessly into CI/CD pipelines, and improve collaboration among developers by minimizing stylistic disagreements.