Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/gopls: support gofmt -s in formatting #37221

Closed
dan-lind opened this issue Feb 14, 2020 · 7 comments
Closed

x/tools/gopls: support gofmt -s in formatting #37221

dan-lind opened this issue Feb 14, 2020 · 7 comments

Comments

@dan-lind
Copy link

@dan-lind dan-lind commented Feb 14, 2020

What version of Go are you using (go version)?

$ go version
go version go1.13.8 linux/amd64

What version of gopls are you using (gopls version)?

$ gopls version
golang.org/x/tools/gopls v0.3.2
    golang.org/x/tools/gopls@v0.3.2 h1:eP1aj1AvT6ynElQH6KP0mmOT2gnWa1gYclHL4wGUbMo=

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/ko1dli/.cache/go-build"
GOENV="/home/ko1dli/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/ko1dli/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/linuxbrew/.linuxbrew/Cellar/go/1.13.8/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/linuxbrew/.linuxbrew/Cellar/go/1.13.8/libexec/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc-5"
CXX="g++-5"
CGO_ENABLED="0"
GOMOD=""
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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build977859433=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Given settings.json with

    "go.formatTool": "gofmt",
    "go.formatFlags": ["-s"],
    "[go]": {
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": true,
        },
        // Optional: Disable snippets, as they conflict with completion ranking.
        "editor.snippetSuggestions": "none",
    },
testKey := []*apigateway.ApiKey{
		&apigateway.ApiKey{
			Name:  aws.String("test"),
			Id:    aws.String("test"),
			Value: aws.String("ThisIsMyApiKey"),
		},
	}

I save my file

What did you expect to see?

Properly formatted code

testKey := []*apigateway.ApiKey{
		{
			Name:  aws.String("test"),
			Id:    aws.String("test"),
			Value: aws.String("ThisIsMyApiKey"),
		},
	}

What did you see instead?

Nothing changes, causing our CI to complain that the files was not formatted using gofmt -s

Relevant logs

gopls -rpc.trace -v check #file# Output
2020/02/14 09:19:17 Info:2020/02/14 09:19:17 Build info
----------
golang.org/x/tools/gopls v0.3.2
    golang.org/x/tools/gopls@v0.3.2 h1:eP1aj1AvT6ynElQH6KP0mmOT2gnWa1gYclHL4wGUbMo=
    github.com/BurntSushi/toml@v0.3.1
    github.com/sergi/go-diff@v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
    golang.org/x/mod@v0.1.1-0.20191105210325-c90efee705ee h1:WG0RUwxtNT4qqaXX3DPA8zHFNm/D9xaBpxzHt1WcA/E=
    golang.org/x/sync@v0.0.0-20190423024810-112230192c58
    golang.org/x/tools@v0.0.0-20200212213342-7a21e308cf6c h1:D2X+P0Z6ychko7xn2jvd38yxQfdU0eksO4AHfd8AWFI=
    golang.org/x/xerrors@v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=
    honnef.co/go/tools@v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM=
    mvdan.cc/xurls/v2@v2.1.0 h1:KaMb5GLhlcSX+e+qhbRJODnUUBvlw01jt4yrjFIHAuA=

Go info

go version go1.13.8 linux/amd64

GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/ko1dli/.cache/go-build"
GOENV="/home/ko1dli/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/ko1dli/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/linuxbrew/.linuxbrew/Cellar/go/1.13.8/libexec"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/linuxbrew/.linuxbrew/Cellar/go/1.13.8/libexec/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc-5"
CXX="g++-5"
CGO_ENABLED="0"
GOMOD="/c/Users/ko1dli/git/base/src/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 -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build981242842=/tmp/go-build -gno-record-gcc-switches"
2020/02/14 09:19:20 Info:2020/02/14 09:19:20 go/packages.Load
snapshot = 0
query = [./... builtin]
packages = 223

gopls logs Output
[Trace - 09:11:49.333 AM] Sending request 'textDocument/codeLens - (15)'.
Params: {"textDocument":{"uri":"file:///c/Users/ko1dli/git/base/src/onboarding/sandbox/pre-token-generation/handler_test.go"}}

[Trace - 09:11:49.333 AM] Received response 'textDocument/codeLens - (15)' in 0ms.
Result: {}

[Trace - 09:11:49.543 AM] Sending request 'textDocument/codeLens - (16)'.
Params: {"textDocument":{"uri":"file:///c/Users/ko1dli/git/base/src/onboarding/sandbox/pre-token-generation/handler_test.go"}}

[Trace - 09:11:49.544 AM] Received response 'textDocument/codeLens - (16)' in 1ms.
Result: {}

[Trace - 09:11:49.852 AM] Sending request 'textDocument/codeAction - (17)'.
Params: {"textDocument":{"uri":"file:///c/Users/ko1dli/git/base/src/onboarding/sandbox/pre-token-generation/handler_test.go"},"range":{"start":{"line":0,"character":0},"end":{"line":244,"character":0}},"context":{"diagnostics":[]}}

[Trace - 09:11:49.857 AM] Received response 'textDocument/codeAction - (17)' in 4ms.
Result: {}

[Trace - 09:11:49.864 AM] Sending request 'textDocument/formatting - (18)'.
Params: {"textDocument":{"uri":"file:///c/Users/ko1dli/git/base/src/onboarding/sandbox/pre-token-generation/handler_test.go"},"options":{"tabSize":4,"insertSpaces":false}}

[Trace - 09:11:49.870 AM] Received response 'textDocument/formatting - (18)' in 5ms.
Result: [{"range":{"start":{"line":35,"character":1},"end":{"line":36,"character":1}},"newText":""},{"range":{"start":{"line":43,"character":0},"end":{"line":44,"character":1}},"newText":""}]

[Trace - 09:11:49.938 AM] Sending notification 'textDocument/didChange'.
Params: {"textDocument":{"uri":"file:///c/Users/ko1dli/git/base/src/onboarding/sandbox/pre-token-generation/handler_test.go","version":3},"contentChanges":[{"range":{"start":{"line":43,"character":0},"end":{"line":44,"character":1}},"rangeLength":2,"text":""},{"range":{"start":{"line":35,"character":1},"end":{"line":36,"character":1}},"rangeLength":2,"text":""}]}

[Trace - 09:11:50.033 AM] Sending notification 'textDocument/didSave'.
Params: {"textDocument":{"uri":"file:///c/Users/ko1dli/git/base/src/onboarding/sandbox/pre-token-generation/handler_test.go","version":3}}

[Trace - 09:11:50.102 AM] Sending request 'textDocument/foldingRange - (19)'.
Params: {"textDocument":{"uri":"file:///c/Users/ko1dli/git/base/src/onboarding/sandbox/pre-token-generation/handler_test.go"}}

[Trace - 09:11:50.103 AM] Received response 'textDocument/foldingRange - (19)' in 0ms.
Result: [{"startLine":2,"startCharacter":8,"endLine":13,"endCharacter":37,"kind":"imports"},{"startLine":26,"startCharacter":50,"endLine":58,"endCharacter":103},{"startLine":61,"startCharacter":66,"endLine":114,"endCharacter":103},{"startLine":117,"startCharacter":71,"endLine":169,"endCharacter":93},{"startLine":172,"startCharacter":41,"endLine":183,"endCharacter":93},{"startLine":186,"startCharacter":39,"endLine":197,"endCharacter":93},{"startLine":200,"startCharacter":72,"endLine":233,"endCharacter":25},{"startLine":236,"startCharacter":41,"endLine":240,"endCharacter":48}]

[Trace - 09:11:50.312 AM] Sending request 'textDocument/codeLens - (20)'.
Params: {"textDocument":{"uri":"file:///c/Users/ko1dli/git/base/src/onboarding/sandbox/pre-token-generation/handler_test.go"}}

[Trace - 09:11:50.315 AM] Received response 'textDocument/codeLens - (20)' in 2ms.
Result: {}

[Trace - 09:11:50.482 AM] Sending notification 'workspace/didChangeWatchedFiles'.
Params: {"changes":[{"uri":"file:///c/Users/ko1dli/git/base/src/onboarding/sandbox/pre-token-generation/handler_test.go","type":2}]}

[Trace - 09:11:50.486 AM] Sending request 'textDocument/codeLens - (21)'.
Params: {"textDocument":{"uri":"file:///c/Users/ko1dli/git/base/src/onboarding/sandbox/pre-token-generation/handler_test.go"}}

[Trace - 09:11:50.489 AM] Received response 'textDocument/codeLens - (21)' in 3ms.
Result: {}

[Trace - 09:11:50.639 AM] Sending request 'textDocument/documentSymbol - (22)'.
Params: {"textDocument":{"uri":"file:///c/Users/ko1dli/git/base/src/onboarding/sandbox/pre-token-generation/handler_test.go"}}

[Trace - 09:11:50.642 AM] Received response 'textDocument/documentSymbol - (22)' in 2ms.
Result: [{"name":"apiMock","detail":"*mocks.MockApigwService","kind":13,"range":{"start":{"line":16,"character":0},"end":{"line":16,"character":35}},"selectionRange":{"start":{"line":16,"character":4},"end":{"line":16,"character":11}}},{"name":"r","detail":"CognitoEventUserPoolsPreTokenGenerationRequest","kind":13,"range":{"start":{"line":18,"character":0},"end":{"line":22,"character":1}},"selectionRange":{"start":{"line":18,"character":4},"end":{"line":18,"character":5}}},{"name":"event","detail":"CognitoEventUserPoolsPreTokenGeneration","kind":13,"range":{"start":{"line":24,"character":0},"end":{"line":24,"character":63}},"selectionRange":{"start":{"line":24,"character":4},"end":{"line":24,"character":9}}},{"name":"TestPreTokenGenerationSuccess","detail":"(t *testing.T)","kind":12,"range":{"start":{"line":26,"character":0},"end":{"line":59,"character":1}},"selectionRange":{"start":{"line":26,"character":5},"end":{"line":26,"character":34}}},{"name":"TestPreTokenGenerationSuccessWithMultipleKeys","detail":"(t *testing.T)","kind":12,"range":{"start":{"line":61,"character":0},"end":{"line":115,"character":1}},"selectionRange":{"start":{"line":61,"character":5},"end":{"line":61,"character":50}}},{"name":"TestPreTokenGenerationNoStageFoundWithMultipleKeys","detail":"(t *testing.T)","kind":12,"range":{"start":{"line":117,"character":0},"end":{"line":170,"character":1}},"selectionRange":{"start":{"line":117,"character":5},"end":{"line":117,"character":55}}},{"name":"TestGetApiKeysFailed","detail":"(t *testing.T)","kind":12,"range":{"start":{"line":172,"character":0},"end":{"line":184,"character":1}},"selectionRange":{"start":{"line":172,"character":5},"end":{"line":172,"character":25}}},{"name":"TestPreTokenNoKeys","detail":"(t *testing.T)","kind":12,"range":{"start":{"line":186,"character":0},"end":{"line":198,"character":1}},"selectionRange":{"start":{"line":186,"character":5},"end":{"line":186,"character":23}}},{"name":"TestValidateCognitoEventUserPoolsPreTokenGeneration","detail":"(t *testing.T)","kind":12,"range":{"start":{"line":200,"character":0},"end":{"line":234,"character":1}},"selectionRange":{"start":{"line":200,"character":5},"end":{"line":200,"character":56}}},{"name":"createMocks","detail":"(t gomock.TestReporter)","kind":12,"range":{"start":{"line":236,"character":0},"end":{"line":241,"character":1}},"selectionRange":{"start":{"line":236,"character":5},"end":{"line":236,"character":16}}}]

[Trace - 09:11:50.650 AM] Sending request 'textDocument/codeLens - (23)'.
Params: {"textDocument":{"uri":"file:///c/Users/ko1dli/git/base/src/onboarding/sandbox/pre-token-generation/handler_test.go"}}

[Trace - 09:11:50.651 AM] Received response 'textDocument/codeLens - (23)' in 0ms.
Result: {}

[Trace - 09:11:50.906 AM] Sending request 'textDocument/documentLink - (24)'.
Params: {"textDocument":{"uri":"file:///c/Users/ko1dli/git/base/src/onboarding/sandbox/pre-token-generation/handler_test.go"}}

[Trace - 09:11:50.923 AM] Received response 'textDocument/documentLink - (24)' in 16ms.
Result: [{"range":{"start":{"line":3,"character":2},"end":{"line":3,"character":9}},"target":"https://pkg.go.dev/context"},{"range":{"start":{"line":4,"character":2},"end":{"line":4,"character":8}},"target":"https://pkg.go.dev/errors"},{"range":{"start":{"line":5,"character":2},"end":{"line":5,"character":31}},"target":"https://pkg.go.dev/sas/common/aws/lambda/handler"},{"range":{"start":{"line":6,"character":2},"end":{"line":6,"character":21}},"target":"https://pkg.go.dev/sas/common/aws/xray"},{"range":{"start":{"line":7,"character":2},"end":{"line":7,"character":18}},"target":"https://pkg.go.dev/sas/common/mocks"},{"range":{"start":{"line":8,"character":2},"end":{"line":8,"character":9}},"target":"https://pkg.go.dev/testing"},{"range":{"start":{"line":10,"character":2},"end":{"line":10,"character":31}},"target":"https://pkg.go.dev/github.com/aws/aws-sdk-go/aws"},{"range":{"start":{"line":11,"character":2},"end":{"line":11,"character":46}},"target":"https://pkg.go.dev/github.com/aws/aws-sdk-go/service/apigateway"},{"range":{"start":{"line":12,"character":2},"end":{"line":12,"character":31}},"target":"https://pkg.go.dev/github.com/golang/mock/gomock"},{"range":{"start":{"line":13,"character":2},"end":{"line":13,"character":36}},"target":"https://pkg.go.dev/github.com/stretchr/testify/assert"},{"range":{"start":{"line":20,"character":17},"end":{"line":20,"character":24}},"target":"https://test.se"}]

2020/02/14 09:12:02 background imports cache refresh starting
2020/02/14 09:12:11 background refresh finished after 8.699511s with err:

@gopherbot gopherbot added this to the Unreleased milestone Feb 14, 2020
@gopherbot
Copy link

@gopherbot gopherbot commented Feb 14, 2020

Thank you for filing a gopls issue! Please take a look at the Troubleshooting guide, and make sure that you have provided all of the relevant information here.

@stamblerre stamblerre modified the milestones: Unreleased, gopls/v0.4.0 Feb 14, 2020
@stamblerre
Copy link
Contributor

@stamblerre stamblerre commented Feb 14, 2020

gopls does not support the -s flag for formatting yet, and since you are using it for formatting, these settings have no effect:

"go.formatTool": "gofmt",
"go.formatFlags": ["-s"],

You can disable gopls formatting and use gofmt directly by setting

"go.experimentalLanguageServerFeatures": {
    "format": false,
}
@stamblerre stamblerre changed the title x/tools/gopls: gofmt -s not formatting correctly on save x/tools/gopls: support gofmt -s in formatting Feb 14, 2020
@stamblerre stamblerre modified the milestones: gopls/v0.4.0, gopls/v1.0.0 Feb 14, 2020
@stamblerre stamblerre modified the milestones: gopls/v1.0.0, Unreleased Mar 12, 2020
@stamblerre
Copy link
Contributor

@stamblerre stamblerre commented Mar 12, 2020

@gopherbot
Copy link

@gopherbot gopherbot commented Mar 16, 2020

Change https://golang.org/cl/223658 mentions this issue: internal/lsp: add option to toggle "gofmt -s" analyzers

@gopherbot
Copy link

@gopherbot gopherbot commented Mar 16, 2020

Change https://golang.org/cl/223659 mentions this issue: go/analysis: add simplify-slice pass from "gofmt -s"

@gopherbot
Copy link

@gopherbot gopherbot commented Mar 16, 2020

Change https://golang.org/cl/223660 mentions this issue: go/analysis: add simplify-composite-lit pass from "gofmt -s"

@gopherbot
Copy link

@gopherbot gopherbot commented Mar 16, 2020

Change https://golang.org/cl/223661 mentions this issue: go/analysis: add simplify-range pass from "gofmt -s"

gopherbot pushed a commit to golang/tools that referenced this issue Mar 26, 2020
This change adds support for analyzers that have suggested fixes of the kind Source.FixAll. This will allow these fixes to be applied on save if the user desires.

To auto apply these fixes on save, make sure your settings.json looks like:

"[go]": {
	"editor.codeActionsOnSave": {
		...
		"source.fixAll": true,
		...
	},
	...
}

Update golang/go#37221

Change-Id: I534e4f6c8c51ec2848cf2899aab68f587ba68423
Reviewed-on: https://go-review.googlesource.com/c/tools/+/223658
Run-TryBot: Rohan Challa <rohan@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
gopherbot pushed a commit to golang/tools that referenced this issue Mar 26, 2020
This is one of the simplifications that "gofmt -s" applies.
https://golang.org/cmd/gofmt/#hdr-The_simplify_command

A range of the form:
	for x, _ = range v {...}
will be simplified to:
	for x = range v {...}

A range of the form:
	for _ = range v {...}
will be simplified to:
	for range v {...}

Updates golang/go#37221

Change-Id: Ic6babbd0b8ab961ebb4d0d6df72df52d9acde6e7
Reviewed-on: https://go-review.googlesource.com/c/tools/+/223661
Run-TryBot: Rohan Challa <rohan@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
gopherbot pushed a commit to golang/tools that referenced this issue Mar 26, 2020
This is one of the simplifications that "gofmt -s" applies.
https://golang.org/cmd/gofmt/#hdr-The_simplify_command

An array, slice, or map composite literal of the form:
	[]T{T{}, T{}}
will be simplified to:
	[]T{{}, {}}

Updates golang/go#37221

Change-Id: I2dca46501983c8af3581c9319d973da5cf690388
Reviewed-on: https://go-review.googlesource.com/c/tools/+/223660
Run-TryBot: Rohan Challa <rohan@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
gopherbot pushed a commit to golang/tools that referenced this issue Mar 26, 2020
This is one of the simplifications that "gofmt -s" applies.
https://golang.org/cmd/gofmt/#hdr-The_simplify_command

A slice expression of the form:
	s[a:len(s)]
will be simplified to:
	s[a:]

Updates golang/go#37221

Change-Id: Ibd4dedaadc9b129d5d39524f0c1ccc8a95bf7e0d
Reviewed-on: https://go-review.googlesource.com/c/tools/+/223659
Run-TryBot: Rohan Challa <rohan@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
onap-github pushed a commit to onap/integration that referenced this issue Nov 26, 2020
Utility "gopls" used to analyse Go source did not support code
simplifications as of writing "sslendpoints" tool [1].

Simplifying composite literals is now supported [2].

[1] golang/go#37221
[2] golang/tools@e428a8e

Issue-ID: SECCOM-261
Change-Id: I757ff8aefed4c7653f3992f9c1b7b0f3e6c10ea3
Signed-off-by: Pawel Wieczorek <p.wieczorek2@samsung.com>
onap-github pushed a commit to onap/integration that referenced this issue Nov 26, 2020
Utility "gopls" used to analyse Go source did not support code
simplifications as of writing "sslendpoints" tool [1].

Simplifying composite literals is now supported [2].

[1] golang/go#37221
[2] golang/tools@e428a8e

Issue-ID: SECCOM-261
Change-Id: I757ff8aefed4c7653f3992f9c1b7b0f3e6c10ea3
Signed-off-by: Pawel Wieczorek <p.wieczorek2@samsung.com>
(cherry picked from commit 8ff725b)
@golang golang locked and limited conversation to collaborators Mar 26, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants