9

I'm using vscode and developing a go project via remote-ssh. However, the format on save does not work. I found it works on the normal project without git. If I remove the .git, it will work as expected. Does anyone have some advice for me?

The version of the environment is as follows:

  1. vscode 1.63.2
  2. go 1.17.6
  3. remote machine Ubuntu 20.04
  4. go extension v0.30.0
2
  • what's your setting like? if you see the language server trace ( see github.com/golang/vscode-go/blob/master/docs/…), are there format request/response messages log? Commented Jan 12, 2022 at 22:07
  • @hana Thank you. I've looked the output log. There is no format related log. Here is the configuration of vscode ``` { "editor.formatOnSave": true, "go.alternateTools": { // "dlv": "${workspaceFolder}/.vscode/dlv-sudo.sh", // "dlv-dap": "${workspaceFolder}/.vscode/dlv-dap-sudo.sh" }, "go.languageServerFlags": [ "-rpc.trace" ], "gopls": { "verboseOutput": true } } ``` Commented Jan 13, 2022 at 23:07

3 Answers 3

17

I had the same issue recently, auto format would only work on projects without a .git directory. What solved the issue for me was to change Format On Save Mode from modificationsIfAvailable to file. Since I prefer modificationsIfAvailable over file for everything, I changed to have formatOnSaveMode to file only for go source files with:

 "[go]": {
    "editor.formatOnSaveMode": "file"
  }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this! I have my top-level set to "editor.formatOnSaveMode": "modificationsIfAvailable" to avoid reformatting entire files for small edits. Looks like the golang extension doesn't know what to do with that mode.
6

Try to format your code manually by pressing Ctrl + Shift + P to open Command Palette and select Format Document. If your file is being properly formatted without any issues, it means there is something wrong in formatOnSave settings.

Choose a default formatter instead of null. like this :

"editor.defaultFormatter": "esbenp.prettier-vscode"

Uninstall other formatters one by one to see if anything causes the conflict.

Try to make it Language specific.

For better understanding refer this link : https://github.com/microsoft/vscode/issues/108447

About Git Specific:

It may be the case that you are running Git Atom. Atom is a desktop application, so may be causing problems in accessing the remote installation path.Try installing Git Repo.

Also make sure Go: Install/Update Tools are up-to-date.

Comments

0

Adding the "editor.defaultFormatter" worked for me.

// File: VSCode - settings.json
{
  "go.formatTool": "gofmt",
  "[go]": {
    "editor.defaultFormatter": "golang.go",
    "editor.formatOnSave": true,
    "editor.formatOnSaveMode": "file",
    "editor.codeActionsOnSave": {
      "quickfix.insertSemicolon": "always",
      "source.organizeImports": "always"
    }
  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.