vscode-go
vscode-go copied to clipboard
Format on save for Go source files doesn't respect editor.formatOnSave setting
What version of Go, VS Code & VS Code Go extension are you using?
Version Information
- Run
go version
to get version of Go from the VS Code integrated terminal.- go version go1.17 darwin/amd64
- Run
gopls -v version
to get version of Gopls from the VS Code integrated terminal.
Build info
----------
golang.org/x/tools/gopls v0.7.2
golang.org/x/tools/[email protected] h1:kRKKdvA8GOzra8rhSFDClOR7hV/x8v0J0Vm4C/gWq8s=
github.com/BurntSushi/[email protected] h1:GaI7EiDXDRfa8VshkTj7Fym7ha+y8/XxIgD2okUIjLw=
github.com/google/[email protected] h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
github.com/sergi/[email protected] h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
golang.org/x/[email protected] h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/[email protected] h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/[email protected] h1:WUoyKPm6nCo1BnNUvPGnFG3T5DUVem42yDJZZ4CNxMA=
golang.org/x/[email protected] h1:C0nyHiBU2m0cR6hDiUORWqQIt3h37wsp1255QBSSXqY=
golang.org/x/[email protected] h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
honnef.co/go/[email protected] h1:ws8AfbgTX3oIczLPNPCu5166oBg9ST2vNs0rcht+mDE=
mvdan.cc/[email protected] h1:bi/1aS/5W00E2ny5q65w9SnKpWEF/UIOqDYBILpo9rA=
mvdan.cc/xurls/[email protected] h1:59Olnbt67UKpxF1EwVBopJvkSUBmgtb468E4GVWIZ1I=
-
Run
code -v
orcode-insiders -v
to get version of VS Code or VS Code Insiders.- 1.60.2 7f6ab5485bbc008386c4386d08766667e155244e x64
-
Check your installed extensions to get the version of the VS Code Go extension
- v0.28.1
Describe the bug
- When I disable the
editor.formatOnSave
setting, vscode-go still formats Go files on save. - Only when I use the following manual JSON setting does vscode-go disable format on save:
"[go]": {
"editor.formatOnSave": false
}
- It would be nice if vscode-go respects the global
editor.formatOnSave
setting, as the workaround above must be set up by editing the VSC settings json file; it is not selectable via the GUI settings panel.
Can you please share your gopls
logs? Information on how to capture them can be found here.
The setting works by not sending formatting requests to the extension when formatOnSave is disabled, so if there is a bug here, it would be a VS Code bug, not a Go extension bug.
Thanks for your speedy response!
The setting works by not sending formatting requests to the extension when formatOnSave is disabled, so if there is a bug here, it would be a VS Code bug, not a Go extension bug.
The bug exists whether with gopls enabled or disabled.
Can you please share your
gopls
logs? Information on how to capture them can be found here.
The following was observed with the following VSCode setting:
"editor.formatOnSave": false,
Click to expand
I've redacted paths; but you should get the idea...[Trace - 14:22:21.404 PM] Sending request 'textDocument/codeAction - (57)'.
Params: {"textDocument":{"uri":"file://$PATH_TO_PROJECT/main.go"},"range":{"start":{"line":0,"character":0},"end":{"line":12,"character":0}},"context":{"diagnostics":[{"range":{"start":{"line":3,"character":13},"end":{"line":3,"character":19}},"message":"\"redacted\": Unknown word.","severity":3,"source":"cSpell"}],"only":["source.organizeImports"]}}
[Trace - 14:22:21.416 PM] Received response 'textDocument/codeAction - (57)' in 11ms.
Result: [{"title":"Organize Imports","kind":"source.organizeImports","edit":{"documentChanges":[{"textDocument":{"version":17,"uri":"file://$PATH_TO_PROJECT/main.go"},"edits":[{"range":{"start":{"line":3,"character":7},"end":{"line":3,"character":7}},"newText":"(\n\t\"fmt\"\n\n\t"},{"range":{"start":{"line":3,"character":27},"end":{"line":6,"character":4}},"newText":""}]}]}}]
[Trace - 14:22:21.432 PM] Sending notification 'textDocument/didChange'.
Params: {"textDocument":{"uri":"file://$PATH_TO_PROJECT/main.go","version":18},"contentChanges":[{"range":{"start":{"line":3,"character":27},"end":{"line":6,"character":4}},"rangeLength":17,"text":""},{"range":{"start":{"line":3,"character":7},"end":{"line":3,"character":7}},"rangeLength":0,"text":"(\n\t\"fmt\"\n\n\t"}]}
[Trace - 14:22:21.433 PM] Sending request 'textDocument/formatting - (58)'.
Params: {"textDocument":{"uri":"file://$PATH_TO_PROJECT/main.go"},"options":{"tabSize":2,"insertSpaces":false}}
[Trace - 14:22:21.441 PM] Received notification 'window/logMessage'.
Params: {"type":1,"message":"2021/09/28 14:22:21 command-line-arguments:file=$PATH_TO_PROJECT/main.go: no dep handle for $PkgPath: no metadata for $PkgPath\n\tsnapshot=24\n"}
[Trace - 14:22:21.441 PM] Received response 'textDocument/formatting - (58)' in 8ms.
Result: []
[Error - 2:22:21 PM] 2021/09/28 14:22:21 command-line-arguments:file=$PATH_TO_PROJECT/main.go: no dep handle for $PkgPath: no metadata for $PkgPath
snapshot=24
[Trace - 14:22:21.488 PM] Sending notification 'textDocument/didSave'.
Params: {"textDocument":{"uri":"file://$PATH_TO_PROJECT/main.go"}}
[Trace - 14:22:21.495 PM] Received notification 'window/logMessage'.
Params: {"type":1,"message":"2021/09/28 14:22:21 command-line-arguments:file=$PATH_TO_PROJECT/main.go: no dep handle for $PkgPath: no metadata for $PkgPath\n\tsnapshot=25\n"}
[Error - 2:22:21 PM] 2021/09/28 14:22:21 command-line-arguments:file=$PATH_TO_PROJECT/main.go: no dep handle for $PkgPath: no metadata for $PkgPath
snapshot=25
[Trace - 14:22:21.627 PM] Sending request 'textDocument/foldingRange - (59)'.
Params: {"textDocument":{"uri":"file://$PATH_TO_PROJECT/main.go"}}
[Trace - 14:22:21.627 PM] Received response 'textDocument/foldingRange - (59)' in 0ms.
Result: [{"startLine":3,"startCharacter":8,"endLine":6,"endCharacter":23,"kind":"imports"},{"startLine":9,"startCharacter":13,"endLine":10,"endCharacter":28}]
[Trace - 14:22:21.746 PM] Sending request 'textDocument/codeLens - (60)'.
Params: {"textDocument":{"uri":"file://$PATH_TO_PROJECT/main.go"}}
[Trace - 14:22:21.748 PM] Sending request 'textDocument/documentSymbol - (61)'.
Params: {"textDocument":{"uri":"file://$PATH_TO_PROJECT/main.go"}}
[Trace - 14:22:21.762 PM] Received response 'textDocument/codeLens - (60)' in 16ms.
Result: null
[Trace - 14:22:21.762 PM] Received response 'textDocument/documentSymbol - (61)' in 14ms.
Result: [{"name":"main","detail":"()","kind":12,"range":{"start":{"line":9,"character":0},"end":{"line":11,"character":1}},"selectionRange":{"start":{"line":9,"character":5},"end":{"line":9,"character":9}}}]
[Trace - 14:22:22.290 PM] Sending notification 'workspace/didChangeWatchedFiles'.
Params: {"changes":[{"uri":"file://$PATH_TO_PROJECT/main.go","type":2}]}
[Trace - 14:22:22.302 PM] Received notification 'window/logMessage'.
Params: {"type":1,"message":"2021/09/28 14:22:22 command-line-arguments:file=$PATH_TO_PROJECT/main.go: no dep handle for $PkgPath: no metadata for $PkgPath\n\tsnapshot=26\n"}
[Error - 2:22:22 PM] 2021/09/28 14:22:22 command-line-arguments:file=$PATH_TO_PROJECT/main.go: no dep handle for $PkgPath: no metadata for $PkgPath
snapshot=26
[Trace - 14:22:22.427 PM] Sending request 'textDocument/documentLink - (62)'.
Params: {"textDocument":{"uri":"file://$PATH_TO_PROJECT/main.go"}}
[Trace - 14:22:22.431 PM] Received response 'textDocument/documentLink - (62)' in 3ms.
Result: [{"range":{"start":{"line":4,"character":2},"end":{"line":4,"character":5}},"target":"https://pkg.go.dev/fmt?utm_source=gopls"}]
Thanks for sharing this log. Looks like the textDocument/formatting
request is still present in the log, so I would guess that this could be an issue with your VS Code settings or with VS Code itself. Can you file an issue with the https://github.com/microsoft/vscode repository instead?
I'm happy to file an issue against vscode, though it would be good to rule out vscode-go first. Are you able to reproduce, with and without gopls enabled? (The bug is present with both here.)
Yeah, I actually did repro. I think it's because the VS Code Go extension defaults to formatOnSave being enabled: https://github.com/golang/vscode-go/blob/9b3e92e8c625df63fcd907e9195b765ea68c49d2/package.json#L184, so probably VS Code doesn't correctly override the setting. Please file an issue with VS Code.
Thanks for that line reference; with it I was able to find #65844 and #58995 which refer to the same problem for a variety of VSC-supported languages and associated settings.
The "official" method to control the format-on-save, or any of the other settings under configurationDefaults
is to override them manually in the user's json config. 😲
Given that #58995 is almost three years old and it may not get addressed any time soon, is there any way to flip this issue into a documentation change, or a hack in the vscode-go settings panel to educate the user so they don't end up following the same path of confusion as I did?
My understanding is that the language-specific configuration overrides non-language-specific configuration setting (https://code.visualstudio.com/api/references/vscode-api#WorkspaceConfiguration) so this is working as expected from VSCode's point of view even though surfacing these to users (microsoft/vscode#58995) definitely needs improvement.
This is not the first time this language-specific default override caused confusion. (https://github.com/golang/vscode-go/issues/1231 https://github.com/golang/vscode-go/issues/1805 and many in the old repo)
Some users request basically not to set language-specific defaults but stay away from configurationDefaults
. However, given that most users go with the default setting, recommending the best configuration and encouraging the best practice for 'go' development is also a role of the extension.
If we want to turn this into a documentation change, where is the best place to surface this info? The editor.formatOnSave
setting is owned by vscode so I don't think documentation surfaced through setting UI is an option. We had related doc in https://github.com/golang/vscode-go/blob/master/docs/advanced.md#formatting-code-and-organizing-imports but it doesn't seem easy to discover.
Popup to suggest the best practice setting instead of configurationDefaults
is an option too but I think for Cloud-based IDEs (e.g.codespaces), popups are not considered a great way to set up environments for the best practice.
Any idea to improve documentation is welcome.
"source.organizeImports" has some weird behavior if there are no imports (formatting the whole file). Would this be the root cause or similar? Here's a short video.
@mukunda- Thanks for the report. It sounds like that is a different issue because you have all the per-language settings override. Can you please file a separate issue with a smaller repro case and the gopls trace (instruction)? Thanks!
Change https://go.dev/cl/412054 mentions this issue: docs/faq.md: entries on formatting issues
Change https://go.dev/cl/412383 mentions this issue: docs/faq.md: entries on formatting issues