tofu-fmt
tofu fmt is the built-in OpenTofu command that rewrites configuration files to a canonical format and style. OpenTofu is the community-driven, MPL-2.0 licensed fork of Terraform hosted by the Linux Foundation, and its formatter follows the same canonical style as terraform fmt.
Key Features:
- Official Formatter: Built into OpenTofu core, so formatting always matches the version of OpenTofu you run
- Open Source: OpenTofu stays under the MPL-2.0 license, unlike Terraform which moved to BUSL-1.1
- Canonical Formatting: Applies consistent indentation, spacing and alignment across all configuration files
- Broad File Coverage: Formats configuration (
.tf), variables (.tfvars) and test (.tftest.hcl) files - Automatic Fixing: Rewrites files in place when APPLY_FIXES is enabled
- Diff Mode: Shows the formatting changes it would make, so reviews stay readable
- Zero Configuration: Intentionally opinionated, with no configuration or ignore file to maintain
Analyzed files
By default this linter only analyzes .tofu files, the OpenTofu specific extension that Terraform does not recognize. .tf files are left to terraform fmt, so both formatters can stay enabled without doing the same work twice.
Formatting .tf files with OpenTofu
If your project runs OpenTofu and keeps its configuration in .tf files, point this linter at them and disable the Terraform one, so a single formatter owns them:
TERRAFORM_TOFU_FMT_FILE_EXTENSIONS: [".tofu", ".tf", ".tfvars"]
DISABLE_LINTERS: [TERRAFORM_TERRAFORM_FMT]
tofu fmt also formats .tftest.hcl and .tofutest.hcl test files. They are not analyzed by default because the .hcl extension is already claimed by terragrunt in this descriptor: add them to TERRAFORM_TOFU_FMT_FILE_EXTENSIONS only if you also disable TERRAFORM_TERRAGRUNT, or you would format them twice.
tofu-fmt documentation
- Version in MegaLinter: 1.12.6
- Visit Official Web Site
- See How to configure tofu-fmt rules
Configuration in MegaLinter
- Enable tofu-fmt by adding
TERRAFORM_TOFU_FMTin ENABLE_LINTERS variable - Disable tofu-fmt by adding
TERRAFORM_TOFU_FMTin DISABLE_LINTERS variable
- Enable autofixes by adding
TERRAFORM_TOFU_FMTin APPLY_FIXES variable
| Variable | Description | Default value |
|---|---|---|
| TERRAFORM_TOFU_FMT_ARGUMENTS | User custom arguments to add in linter CLI call Ex: -s --foo "bar" |
|
| TERRAFORM_TOFU_FMT_COMMAND_REMOVE_ARGUMENTS | User custom arguments to remove from command line before calling the linter Ex: -s --foo "bar" |
|
| TERRAFORM_TOFU_FMT_FILTER_REGEX_INCLUDE | Custom regex including filter Ex: (src\|lib) |
Exclude no file |
| TERRAFORM_TOFU_FMT_FILTER_REGEX_EXCLUDE | Custom regex excluding filter Ex: (test\|examples) |
Exclude no file |
| TERRAFORM_TOFU_FMT_CLI_LINT_MODE | Override default CLI lint mode - list_of_files: Call the linter with the list of files as argument |
list_of_files |
| TERRAFORM_TOFU_FMT_FILE_EXTENSIONS | Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all filesEx: [".py", ""] |
[".tofu"] |
| TERRAFORM_TOFU_FMT_FILE_NAMES_REGEX | File name regex filters. Regular expression list for filtering files by their base names using regex full match. Empty list includes all files Ex: ["Dockerfile(-.+)?", "Jenkinsfile"] |
Include every file |
| TERRAFORM_TOFU_FMT_PRE_COMMANDS | List of bash commands to run before the linter | None |
| TERRAFORM_TOFU_FMT_POST_COMMANDS | List of bash commands to run after the linter | None |
| TERRAFORM_TOFU_FMT_UNSECURED_ENV_VARIABLES | List of env variables explicitly not filtered before calling TERRAFORM_TOFU_FMT and its pre/post commands | None |
| TERRAFORM_TOFU_FMT_DISABLE_ERRORS | Run linter but consider errors as warnings | true |
| TERRAFORM_TOFU_FMT_DISABLE_ERRORS_IF_LESS_THAN | Maximum number of errors allowed | 0 |
| TERRAFORM_TOFU_FMT_TIMEOUT_SECONDS | Maximum duration in seconds of the linter run, after which the linter process and its child processes are killed and reported as an error (exit code 124). Overrides LINTER_TIMEOUT_SECONDS. 0 disables the timeout | 300 |
| TERRAFORM_TOFU_FMT_CLI_EXECUTABLE | Override CLI executable | ['tofu'] |
IDE Integration
Use tofu-fmt in your favorite IDE to catch errors before MegaLinter !
| IDE | Extension Name | Install | |
|---|---|---|---|
| IDEA | Terraform and HCL | ||
| Visual Studio Code | OpenTofu | ![]() |
MegaLinter Flavors
This linter is available in the following flavors
| Flavor | Description | Embedded linters | Info | |
|---|---|---|---|---|
![]() |
all | Default MegaLinter Flavor | 132 | |
| cupcake | MegaLinter for the most commonly used languages | 99 | ||
| formatters | Contains only formatters | 20 | ||
| terraform | Optimized for TERRAFORM based projects | 57 |
Behind the scenes
How are identified applicable files
- File extensions:
.tofu
How the linting is performed
- tofu-fmt is called once with the list of files as arguments (
list_of_filesCLI lint mode)
Example calls
tofu fmt -diff -check myfile.tf
tofu fmt myfile.tf
Help content
Usage: tofu [global options] <subcommand> [args]
The available commands for execution are listed below.
The primary workflow commands are given first, followed by
less common or more advanced commands.
Main commands:
init Prepare your working directory for other commands
validate Check whether the configuration is valid
plan Show changes required by the current configuration
apply Create or update infrastructure
destroy Destroy previously-created infrastructure
All other commands:
console Try OpenTofu expressions at an interactive command prompt
fmt Reformat your configuration in the standard style
force-unlock Release a stuck lock on the current workspace
get Install or upgrade remote OpenTofu modules
graph Generate a Graphviz graph of the steps in an operation
import Associate existing infrastructure with a OpenTofu resource
login Obtain and save credentials for a remote host
logout Remove locally-stored credentials for a remote host
metadata Metadata related commands
output Show output values from your root module
providers Show the providers required for this configuration
refresh Update the state to match remote systems
show Show the current state or a saved plan
state Advanced state management
taint Mark a resource instance as not fully functional
test Execute integration tests for OpenTofu modules
untaint Remove the 'tainted' state from a resource instance
version Show the current OpenTofu version
workspace Workspace management
Global options (use these before the subcommand, if any):
-chdir=DIR Switch to a different working directory before executing the
given subcommand.
-help Show this help output, or the help for a specified subcommand.
-version An alias for the "version" subcommand.
Installation on mega-linter Docker image
- Dockerfile commands :
# renovate: datasource=docker depName=ghcr.io/opentofu/opentofu ARG TERRAFORM_TOFU_FMT_VERSION=1.12.6-minimal FROM ghcr.io/opentofu/opentofu:${TERRAFORM_TOFU_FMT_VERSION} AS opentofu COPY --link --from=opentofu /usr/local/bin/tofu /usr/bin/

