DEV Community

Calin Baenen
Calin Baenen

Posted on

How do I disable GoLang's auto-formatting on file save in VSCode?

I'm new to Go, and trying to make something using it, however, each time I press the save button (or use the shortcut Ctrl+S) (so basically, whenever I save the file MANUALLY).

Though, the strange thing is, I have autosave turned on in VSCode, and the file will save after like a second or two, and whenever VSC saves the file, it doesn't get formatted (contrast to when I try to save it myself (as I've just stated)).
There are the GoLang related settings in VSCode's settings.json file:

"go.lintOnSave": "off",
"[go]": {
    "editor.formatOnSave": false,
    "editor.defaultFormatter": null
},
"go.languageServerExperimentalFeatures": {
    "format": false     # Property "format" is not allowed.
}
Enter fullscreen mode Exit fullscreen mode

Can someone please tell me what I'm doing wrong? I looked at this and even looked at what Go themselves had to say on their VSCode extension page, but neither of these are helping.

Thanks a lot in advance!
Cheers!

Discussion (3)

Collapse
esslamb profile image
Esslam Ben Ramadan

I know this probably isn't the answer you wanted but why would you disable it? go specifically built gofmt blog.golang.org/gofmt so that we don't have to worry about formatting. However, all that being said the following works for me so I would double-check you don't have anything overwriting it

"[go]": {
        "editor.formatOnSave": false
}
Enter fullscreen mode Exit fullscreen mode
Collapse
baenencalin profile image
Calin Baenen Author

I'm now on Arch Linux, it's been a while, and I've tried to come back, NOTHING is overwriting it, and it still doesn't work. The bug is driving me fucking insane! - This is my settings.json:

{
    "security.workspace.trust.untrustedFiles": "open",
    "workbench.editor.untitled.hint": "hidden",

    "editor.trimAutoWhitespace": false,
    "editor.insertSpaces": false,
    "editor.tabSize": 4,

    "go.autocompleteUnimportedPackages": true,
    "go.formatTool": null,
    "go.lintOnSave": "off",
    "[go]": {
        "editor.formatOnSave": false,
        "editor.defaultFormatter": null,
        "editor.codeActionsOnSave": {
            "source.fixAll": false
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
wgumenyuk profile image
Wlad Gumenyuk

Adding source.organizeImports: false to editor.codeActionsOnSave fixed it for me!

{
    "[go]": {
        "editor.formatOnSave": false,
        "editor.codeActionsOnSave": {
            "source.fixAll": false,
            "source.organizeImports": false
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Hope this helps!