VScode格式化保存在Golang中不起作用。

huangapple go评论104阅读模式
英文:

VScode format as save not work for Golang

问题

我正在使用vscode通过remote-ssh开发一个Go项目。然而,保存时的格式化功能不起作用。我发现在没有git的普通项目上它是有效的。如果我移除.git文件夹,它就会按预期工作。有人能给我一些建议吗?

环境的版本如下:

  1. vscode 1.63.2
  2. go 1.17.6
  3. 远程机器 Ubuntu 20.04
  4. go扩展版本 v0.30.0
英文:

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

答案1

得分: 7

我最近也遇到了同样的问题,自动格式化只能在没有.git目录的项目上工作。解决这个问题的方法是将“Format On Save Mode”从“modificationsIfAvailable”更改为“file”。由于我更喜欢在所有情况下使用“modificationsIfAvailable”,所以我将“formatOnSaveMode”仅对go源文件更改为“file”:

"": {
  "editor.formatOnSaveMode": "file"
}
英文:

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:

 "": {
    "editor.formatOnSaveMode": "file"
  }

答案2

得分: 3

尝试通过按下Ctrl + Shift + P手动格式化代码,打开命令面板并选择"格式化文档"。如果您的文件能够正确格式化而没有任何问题,那么说明在"formatOnSave"设置中存在问题。

选择一个默认的格式化程序,而不是null。像这样:

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

逐个卸载其他格式化程序,看看是否有任何冲突。

尝试使其针对特定语言。

为了更好地理解,请参考此链接:https://github.com/microsoft/vscode/issues/108447

关于Git特定问题:

可能是您正在运行"Git Atom"。Atom是一个桌面应用程序,可能导致访问远程安装路径时出现问题。尝试安装"Git Repo"。

还要确保"Go: Install/Update Tools"是最新的。

英文:

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.

huangapple
  • 本文由 发表于 2022年1月13日 05:29:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/70688646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定