Search code examples
govisual-studio-codegolangci-lint

VSCode does not do auto formatting on save - Go


I am working on a Go project and using VSCode as an IDE. I have installed the official Go extensions and configured my settings in such a way as to have a formatting onSave while coding.

My settings.json file looks like this:

{
  "editor.formatOnPaste": true,
  "git.autofetch": true,
  "[markdown]": {
    "editor.wordWrap": "on"
  },
  "eslint.codeActionsOnSave.rules": null,
  "solidity.telemetry": false,
  "editor.formatOnSave": true,
  "eslint.run": "onSave",
  "workbench.editor.wrapTabs": true,
  "remote.SSH.remoteServerListenOnSocket": true,
  "redhat.telemetry.enabled": false,
  "githubPullRequests.pullBranch": "never",
  "eslint.workingDirectories": [],
  "eslint.timeBudget.onValidation": {
    "warn": 4000,
    "error": 8000
  },
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "[jsonc]": {
    "editor.defaultFormatter": "vscode.json-language-features"
  },
  "[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[python]": {
    "editor.formatOnType": true
  },
  "prettier.ignorePath": "",
  "editor.rulers": [
    120
  ],
  "editor.inlineSuggest.enabled": true,
  "go.alternateTools": {
    "go": "/Users/vitomirpavlov/go/bin"
  },
  "go.toolsManagement.autoUpdate": true,
  "go.lintFlags": [
    "--fast"
  ],
  "go.lintTool": "golangci-lint",
  "go.formatTool": "goimports",
  "go.testTimeout": "15m",
  "go.lintOnSave": "file",
  "go.vetOnSave": "package",
  "[go]": {
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
      "source.organizeImports": true
    }
  },
  "[go.mod]": {
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
      "source.organizeImports": true
    }
  },
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": "explicit"
  },
  "eslint.validate": [
    "javascript",
    "javascriptreact"
  ],
  "prettier.requireConfig": true,
}

I am using golang v1.20.14, gopls v0.15.3 and golangci-lint v1.51.0. I have tried multiple things like re-installing the Go extension, deleting all of the other configurations in the settings.json file, and disabling all the other extensions that I have in order to verify that there are no conflicts.

I have quite long lines that have to be formatted as to be in multi-line, but it doesn't format it at all.

How can I properly configure the linter/formatter?


Solution

  • To format a code file you need a default formatter for that language:

    "[go]": {
      "editor.defaultFormatter": "golang.Go"
    }