Code Actions

August 5, 2025 ยท View on GitHub

The OpenTofu Language Server implements a set of Code Actions which perform different actions on the current document. These commands are typically code fixes to either refactor code, fix problems or to beautify/refactor code.

Available Actions

source.formatAll.opentofu

The server will format a given document according to OpenTofu formatting conventions.

Usage

VS Code

To enable the format code action globally, set source.formatAll.opentofu to true for the editor.codeActionsOnSave setting and set editor.formatOnSave to false.

"editor.formatOnSave": false,
"editor.codeActionsOnSave": {
  "source.formatAll.opentofu": true
},
"[opentofu]": {
  "editor.defaultFormatter": "opentofu.vscode-opentofu",
}

Important: Disable editor.formatOnSave if you are using source.formatAll.opentofu in editor.codeActionsOnSave. The source.formatAll.opentofu code action is meant to be used instead of editor.formatOnSave, as it provides a guarantee of order of execution based on the list provided. If you have both settings enabled, then your document will be formatted twice.

If you would like editor.formatOnSave to be true for other extensions but false for the OpenTofu extension, you can configure your settings as follows:

"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
  "source.formatAll.opentofu": true
},
"[opentofu]": {
  "editor.defaultFormatter": "opentofu.vscode-opentofu",
  "editor.formatOnSave": false,
},

Alternatively, you can include all OpenTofu related Code Actions inside the language specific setting if you prefer:

"editor.formatOnSave": true,
"[opentofu]": {
  "editor.defaultFormatter": "opentofu.vscode-opentofu",
  "editor.formatOnSave": false,
  "editor.codeActionsOnSave": {
    "source.formatAll.opentofu": true
  },
},