Repository: golang/vscode-go

Issue #691: formatting: 'modifications' for editor.formatOnSaveMode is not supported
Created: 2020-09-25T21:02:09Z
https://code.visualstudio.com/updates/v1_49#_only-format-modified-text
This is not supported. Moreover, since we don't implement DocumentRangeFormattingEditProvider, vscode even doesn't trigger
any go formatter.
gopls and many other formating tools for go do not range formatting yet.
Workaround: use file mode instead.
When working on multi-language projects and wanting to use the modification mode for other languages, specify it in the go-specific section.
"[go]": {
       ...
    	"editor.formatOnSaveMode": "file",
}

Until we have underlying tool support, we can do either

implement a DocumentRangeFormattingEditProvider that formats the entire file, or
just warn users of this and wait for gopls to implement this feature.

Duplicate issue #721: Format on save doesn't work if editor.formatOnSaveMode is modifications
Created: 2020-10-01T05:32:39Z
What version of Go, VS Code & VS Code Go extension are you using?

Run go version to get version of Go

go version go1.15.2 linux/amd64


Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders

1.49.2 e5e9e69aed6e1984f7499b7af85b3d05f9a6883a x64


Check your installed extensions to get the version of the VS Code Go extension

0.17.1


Run go env to get the go development environment details

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/username/.cache/go-build"
GOENV="/home/username/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/username/src/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/username/src/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.15"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.15/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/username/src/myproject/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build911951109=/tmp/go-build -gno-record-gcc-switches"

Share the Go related settings you have added/edited
	"go.testFlags": [
		"-short"
	],
	"go.lintTool": "golangci-lint",
	"go.buildOnSave": "workspace",
	"go.coverOnSave": true,
	"go.vetOnSave": "workspace",
	"go.testTimeout": "10s",
	"go.testOnSave": true,
	"go.useLanguageServer": true,
	"[go]": {
		"editor.insertSpaces": false,
		"editor.formatOnSave": true,
		"editor.codeActionsOnSave": {
			"source.organizeImports": true
		}
	},
	"go.formatTool": "goreturns",
	"editor.formatOnSaveMode": "modifications",

Describe the bug
If I introduce formatting errors into a source file and hit save, the file is not run through the formatter. Instead I get a linting error that the file isn't formatted:
File is not `gofmt`-d with `-s` (gofmt) go-lint

Running the formatter manually does correctly reformat the file.
Steps to reproduce the behavior:

Set editor.formatOnSaveMode to modifications
Introduce a formatting error (e.g. over/under-indent a line)
Save the file

I've tried changing out the formatter from goreturns to just gofmt, and I've tried disabling the language server, and I've tried enabling the global (not language specific) editor.formatOnSave setting. None of those helped.
What does help is changing editor.formatOnSaveMode from modifications to file. I can do this for just the [go] language section and have it work.

Closing comment (2020-10-01T13:17:22Z): @fastcat thanks for the report. This is a duplicate of #691 (and help wanted)