WingPig99
WingPig99

Reputation: 89

VScode format as save not work for Golang

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

Upvotes: 8

Views: 10573

Answers (2)

Thiago Pontes
Thiago Pontes

Reputation: 606

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"
  }

Upvotes: 16

Ashutosh Singh
Ashutosh Singh

Reputation: 1047

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.

Upvotes: 6

Related Questions