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: Emacs LSP not formatting the buffer according to before-save-hook #52392

Closed
suntong opened this issue Apr 17, 2022 · 1 comment
Labels
FrozenDueToAge gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@suntong
Copy link

suntong commented Apr 17, 2022

gopls version

$ gopls -v version
Build info
----------
golang.org/x/tools/gopls v0.8.3
    golang.org/x/tools/gopls@v0.8.3 h1:Mxm94ix8oSARQ6svioO6SxKEYWT/VCP54/448LOHzrk=
    github.com/BurntSushi/toml@v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU=
    github.com/google/go-cmp@v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
    github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
    golang.org/x/exp/typeparams@v0.0.0-20220218215828-6cf2b201936e h1:qyrTQ++p1afMkO4DPEeLGq/3oTsdlvdH4vqZUBWzUKM=
    golang.org/x/mod@v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 h1:kQgndtyPBW/JIYERgdxfwMYh3AVStj88WQTlNDi2a+o=
    golang.org/x/sync@v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
    golang.org/x/sys@v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c=
    golang.org/x/text@v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
    golang.org/x/tools@v0.1.11-0.20220407163324-91bcfb1bdf9c h1:rYiuK/jn6TCxjAq1hQ6MR6pgOeP3A5gIHDxgrQ8Vbys=
    golang.org/x/vuln@v0.0.0-20220324005316-18fd808f5c7f h1:9dMzk88fnONra7zrEalqkRMGa9jMGf9B5mdzhYVyI28=
    golang.org/x/xerrors@v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
    honnef.co/go/tools@v0.3.0 h1:2LdYUZ7CIxnYgskbUZfY7FPggmqnh6shBqfWa8Tn3XU=
    mvdan.cc/gofumpt@v0.3.0 h1:kTojdZo9AcEYbQYhGuLf/zszYthRdhDNDUi2JKTxas4=
    mvdan.cc/xurls/v2@v2.4.0 h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc=
go: go1.18

go env

$ go env
GO111MODULE="off"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/tong/.cache/go-build"
GOENV="/home/tong/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/tong/l/wk/Go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/tong/l/wk/Go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/lib/go-1.18"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.18/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.18"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
GOWORK=""
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-build1149016766=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Edit any .go file under Emacs, using the LSP + go mode, using the following hook when saving the file:

  (defun lsp-go-install-save-hooks ()
    "LSP Go save hooks."
    (add-hook 'before-save-hook #'lsp-format-buffer t t)
    (add-hook 'before-save-hook #'lsp-organize-imports t t)
    )

What did you expect to see?

lsp-format-buffer will format the buffer.

What did you see instead?

buffer not formatted. Here is the saved file:

package main

import (
	"fmt"
	"path/filepath"

	  "github.com/go-jsonfile/gojson"
	 "github.com/mkideal/cli"
	"github.com/suntong/enum"
)



    
////////////////////////////////////////////////////////////////////////////
// Global variables definitions

var aa int

var (
	fmtTypeEn  enum.Enum
	fmtJson   = fmtTypeEn.Iota("json")
	fmtYaml   = fmtTypeEn.Iota("yaml")

	parser = []gojson.Parser{gojson.ParseJson, gojson.ParseYaml}
)

image

Editor and settings

Emacs (v28.1 compiled from git source emacs-28.1 branch), using the LSP + go mode.

  go-mode                        20220114.2239  dependency            Major mode for the Go programming language
  lsp-mode                       20220415.1810  dependency            LSP mode

image

See above, and here is my whole init.el part:

(use-package go-mode
  :ensure t
  :defer t
  :hook (go-mode . (lambda ()
                     (require 'lsp-go)
                     (lsp-deferred)))
)

(defun lsp-go-install-save-hooks ()
    "LSP Go save hooks."
    (add-hook 'before-save-hook #'lsp-format-buffer t t)
    (add-hook 'before-save-hook #'lsp-organize-imports t t)
    )
(add-hook 'go-mode-hook #'lsp-go-install-save-hooks)

Logs

Both the gopls and gopls::stderr buffer are empty.

The log from Messages in Emacs shows Timeout:

Saving file /tmp/test-gopls.go...
Error: (error "Timeout while waiting for response.  Method: textDocument/formatting")
Wrote /tmp/test-gopls.go
Saving file /tmp/test-gopls.go...
Error: (error "Timeout while waiting for response.  Method: textDocument/formatting")
Wrote /tmp/test-gopls.go

I have also seen LSP :: No formatting changes provided line between the Saving and Wrote lines sometime.

The log from *lsp-log* in Emacs looks good, except the Cancelling lines at the bottom:

Command "gopls" is present on the path.
Command "gopls" is present on the path.
Found the following clients for /tmp/test-gopls.go: (server-id gopls, priority 0)
The following clients were selected based on priority: (server-id gopls, priority 0)
2022/04/17 10:26:36 go env for /tmp
(root /tmp)
(go version go version go1.18 linux/amd64)
(valid build configuration = false)
(build flags: [])
GOMODCACHE=/home/tong/l/wk/Go/pkg/mod
GOWORK=
GOFLAGS=
GOINSECURE=
GOMOD=
GOROOT=/usr/lib/go-1.18
GONOSUMDB=
GOPATH=/home/tong/l/wk/Go
GOCACHE=/home/tong/.cache/go-build
GOPRIVATE=
GONOPROXY=
GOSUMDB=sum.golang.org
GOPROXY=https://proxy.golang.org,direct
GO111MODULE=off


2022/04/17 10:26:36 go/packages.Load
	snapshot=0
	directory=/tmp
	query=[./ builtin]
	packages=2

2022/04/17 10:26:36 falling back to safe trimming due to type errors: [/usr/lib/go-1.18/src/runtime/vdso_linux.go:53:38: invalid operation: division by zero /usr/lib/go-1.18/src/runtime/vdso_linux.go:54:38: invalid operation: division by zero] or still-missing identifiers: map[memRecordCycle:true pageBits:true]
	package="runtime"

2022/04/17 10:26:37 discovered missing identifiers: map[options:true]
	package="vendor/golang.org/x/text/unicode/bidi"

2022/04/17 10:26:37 discovered missing identifiers: map[yaml_style_t:true]
	package="gopkg.in/yaml.v2"

2022/04/17 10:26:37 discovered missing identifiers: map[cpuMask:true]
	package="golang.org/x/sys/unix"

Cancelling textDocument/codeAction(24) in hook after-change-functions
Cancelling textDocument/documentLink(22) in hook after-change-functions
Cancelling textDocument/codeAction(21) in hook after-change-functions
Cancelling textDocument/documentLink(18) in hook after-change-functions
Cancelling textDocument/documentLink(14) in hook after-change-functions
Cancelling textDocument/documentLink(11) in hook after-change-functions
Cancelling textDocument/codeLens(7) in hook after-change-functions
Cancelling textDocument/documentLink(6) in hook after-change-functions
Cancelling textDocument/documentLink(40) in hook post-command-hook
Cancelling textDocument/codeAction(39) in hook post-command-hook
Cancelling textDocument/documentLink(34) in hook post-command-hook
Cancelling textDocument/documentLink(28) in hook post-command-hook

What causes all those Cancellings and timeouts?

@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Apr 17, 2022
@gopherbot gopherbot added this to the Unreleased milestone Apr 17, 2022
@suntong suntong closed this as completed Apr 17, 2022
@suntong suntong reopened this Apr 17, 2022
@suntong
Copy link
Author

suntong commented Apr 17, 2022

Hmm... in fact, my above before-save-hook has always been half-broken, and my go file will get scrambled 35% of the cases, depending on which file I'm editing. So I revert to using gofmt-before-save in hook instead. Thus I'm closing this for now, however,

If there is some easy fix, I'm willing to give it try.

thx

@suntong suntong closed this as completed Apr 17, 2022
@golang golang locked and limited conversation to collaborators Apr 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

2 participants