Bug 447640 - lsp-client gopls autoimports not working
Summary: lsp-client gopls autoimports not working
Status: RESOLVED FIXED
Alias: None
Product: kate
Classification: Applications
Component: general (show other bugs)
Version: 21.12.0
Platform: Arch Linux Linux
: NOR normal
Target Milestone: ---
Assignee: KWrite Developers
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-12-29 12:31 UTC by bokunocode
Modified: 2022-02-03 18:33 UTC (History)
3 users (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description bokunocode 2021-12-29 12:31:50 UTC
SUMMARY
***
NOTE: If you are reporting a crash, please try to attach a backtrace with debug symbols.
See https://community.kde.org/Guidelines_and_HOWTOs/Debugging/How_to_create_useful_crash_reports
***
lsp client  lsp-organize-import some times not working (gopls).

STEPS TO REPRODUCE
1. add folder to project (project plugin enabled)
2. open .go file
3. use feature from a pakage (ex. log). type log. <ctrl+space> to show symbol sugestion

OBSERVED RESULT
symbol suggestion show up and but package automatically not imported when one of them selected.

EXPECTED RESULT
symbol suggestion show up and package automatically imported when one of them selected.

SOFTWARE/OS VERSIONS
Windows: 
macOS: 
Linux/KDE Plasma: 
(available in About System)
Arch linux xfce4
Linux local.pc 5.15.11-arch2-1 #1 SMP PREEMPT Wed, 22 Dec 2021 09:23:54 +0000 x86_64 GNU/Linux
KDE Plasma Version: 
KDE Frameworks Version: 
Qt Version:
qt5-base 5.15.2+kde+r268-1

ADDITIONAL INFORMATION
gopls code action also not working
Comment 1 Adam Jimerson 2022-01-04 22:52:42 UTC
I can reproduce this issue as well, using gopls v0.7.4 with Kate  21.12.0. 

If it helps since the original reporter didn't include much details this is the output from gopls

```
   2022/01/04 19:41:43 go env for /home/vendion/projects/ssh-manage
   (root /home/vendion/projects/ssh-manage)
   (go version go version go1.17.5 linux/amd64)
   (valid build configuration = true)
   (build flags: [])
   GOCACHE=/home/vendion/.cache/go-build
   GOFLAGS=
   GOINSECURE=
   GONOPROXY=
   GOSUMDB=sum.golang.org
   GO111MODULE=
   GONOSUMDB=
   GOROOT=/usr/lib/go
   GOMOD=/home/vendion/projects/ssh-manage/go.mod
   GOPATH=/home/vendion/go
   GOPRIVATE=
   GOMODCACHE=/home/vendion/go/pkg/mod
   GOPROXY=https://proxy.golang.org,direct
   2022/01/04 19:41:43 go/packages.Load
   	snapshot=0
   	directory=/home/vendion/projects/ssh-manage
   	query=[builtin git.sr.ht/~vendion/ssh-manage/...]
   	packages=11
   2022/01/04 19:41:44 falling back to safe trimming due to type errors: [/usr/lib/go/src/runtime/vdso_linux.go:55:38: invalid operation: division by zero /usr/lib/go/src/runtime/vdso_linux.go:56:38: invalid operation: division by zero] or still-missing identifiers: map[memRecordCycle:true pageBits:true]
   	package="runtime"
   2022/01/04 19:41:44 discovered missing identifiers: map[options:true]
   	package="vendor/golang.org/x/text/unicode/bidi"
   2022/01/04 19:41:44 discovered missing identifiers: map[cpuMask:true]
   	package="golang.org/x/sys/unix"
   2022/01/04 19:41:44 falling back to safe trimming due to type errors: [/usr/lib/go/src/os/user/cgo_lookup_unix.go:200:19: int not declared by package C /usr/lib/go/src/os/user/cgo_lookup_unix.go:203:16: bufferKind(C._SC_GETPW_R_SIZE_MAX) (value of type bufferKind) is not constant /usr/lib/go/src/os/user/cgo_lookup_unix.go:204:16: bufferKind(C._SC_GETGR_R_SIZE_MAX) (value of type bufferKind) is not constant] or still-missing identifiers: map[]
   	package="os/user"
   2022/01/04 19:41:44 discovered missing identifiers: map[frameText:true textViewIndex:true]
   	package="github.com/rivo/tview"
   2022/01/04 19:41:45 no signature help: cannot find an enclosing function
   	position={32 4}
   2022/01/04 19:41:51 no signature help: cannot find an enclosing function
   	position={32 5}
   2022/01/04 19:42:06 no signature help: no signature help within a string literal
   	position={32 16}
   2022/01/04 19:42:15 background imports cache refresh starting    2022/01/04 19:42:15 background refresh finished after 46.851335ms
19:41:43 LSP Server Info Setting up workspace [100%] Finished loading packages.
19:41:43 LSP Server Info go@/home/vendion/projects/ssh-manage
19:41:43 LSP Server Info go@/home/vendion/projects/ssh-manage
19:41:44 LSP Server Info go@/home/vendion/projects/ssh-manage
19:41:44 LSP Server Info go@/home/vendion/projects/ssh-manage
19:41:44 LSP Server Info go@/home/vendion/projects/ssh-manage
19:41:44 LSP Server Info go@/home/vendion/projects/ssh-manage
19:41:44 LSP Server Info go@/home/vendion/projects/ssh-manage
19:41:45 LSP Server Error go@/home/vendion/projects/ssh-manage
19:41:51 LSP Server Error go@/home/vendion/projects/ssh-manage
19:42:06 LSP Server Error go@/home/vendion/projects/ssh-manage
19:42:15 LSP Server Info go@/home/vendion/projects/ssh-manage
19:42:15 LSP Server Info go@/home/vendion/projects/ssh-manage
```

SOFTWARE/OS VERSIONS
Operating System: Arch Linux
KDE Plasma Version: 5.23.5
KDE Frameworks Version: 5.89.0
Qt Version: 5.15.2
Kernel Version: 5.15.12-arch1-1 (64-bit)
Graphics Platform: X11
Processors: 8 × Intel® Core™ i7-6820HQ CPU @ 2.70GHz
Memory: 31.2 GiB of RAM
Graphics Processor: Quadro M1000M/PCIe/SSE2
Comment 2 Juris 2022-01-29 16:30:24 UTC
I can confirm this. Basically Organize Imports functionality does not seem to work in LSP Client. This is interesting since enabling trace log for gopls shows response from LSP server side:

--- 8< ---
[Trace - 18:28:12.986 PM] Sending request 'textDocument/rename - (8)'.
Params: {
        "newName": "myFunctionNew",
        "position": {
            "character": 8,
            "line": 3
        },
        "textDocument": {
            "uri": "file:///tmp/goproj/main.go"
        }
    }


[Trace - 18:28:12.987 PM] Received response 'textDocument/rename - (8)' in 0ms.
Result: {"documentChanges":[{"textDocument":{"version":115,"uri":"file:///tmp/goproj/main.go"},"edits":[{"range":{"start":{"line":3,"character":4},"end":{"line":3,"character":14}},"newText":"myFunctionNew"},{"range":{"start":{"line":6,"character":3},"end":{"line":6,"character":13}},"newText":"myFunctionNew"},{"range":{"start":{"line":7,"character":5},"end":{"line":7,"character":15}},"newText":"myFunctionNew"}]}]}
--- 8< ---

Example main.go file, trying to rename myFunction:

--- 8< ---
package main

func main() {
    myFunction()
}

// myFunction is just a dummy function
func myFunction() {
    return
}
--- 8< ---




Kate Version 21.12.1
gopls 0.7.4

Operating System: Arch Linux
KDE Plasma Version: 5.23.5
KDE Frameworks Version: 5.90.0
Qt Version: 5.15.2
Kernel Version: 5.16.2-arch1-1 (64-bit)
Graphics Platform: Wayland
Processors: 12 × AMD Ryzen 5 5600G with Radeon Graphics
Memory: 13,6 GiB of RAM
Graphics Processor: AMD RENOIR
Comment 3 Juris 2022-01-29 16:35:22 UTC
(In reply to Juris from comment #2)
> I can confirm this. Basically Organize Imports functionality does not seem
> to work in LSP Client. This is interesting since enabling trace log for
> gopls shows response from LSP server side:
> 
> --- 8< ---
> [Trace - 18:28:12.986 PM] Sending request 'textDocument/rename - (8)'.
> Params: {
>         "newName": "myFunctionNew",
>         "position": {
>             "character": 8,
>             "line": 3
>         },
>         "textDocument": {
>             "uri": "file:///tmp/goproj/main.go"
>         }
>     }
> 
> 
> [Trace - 18:28:12.987 PM] Received response 'textDocument/rename - (8)' in
> 0ms.
> Result:
> {"documentChanges":[{"textDocument":{"version":115,"uri":"file:///tmp/goproj/
> main.go"},"edits":[{"range":{"start":{"line":3,"character":4},"end":{"line":
> 3,"character":14}},"newText":"myFunctionNew"},{"range":{"start":{"line":6,
> "character":3},"end":{"line":6,"character":13}},"newText":"myFunctionNew"},
> {"range":{"start":{"line":7,"character":5},"end":{"line":7,"character":15}},
> "newText":"myFunctionNew"}]}]}
> --- 8< ---
> 
> Example main.go file, trying to rename myFunction:
> 
> --- 8< ---
> package main
> 
> func main() {
>     myFunction()
> }
> 
> // myFunction is just a dummy function
> func myFunction() {
>     return
> }
> --- 8< ---
> 
> 
> 
> 
> Kate Version 21.12.1
> gopls 0.7.4
> 
> Operating System: Arch Linux
> KDE Plasma Version: 5.23.5
> KDE Frameworks Version: 5.90.0
> Qt Version: 5.15.2
> Kernel Version: 5.16.2-arch1-1 (64-bit)
> Graphics Platform: Wayland
> Processors: 12 × AMD Ryzen 5 5600G with Radeon Graphics
> Memory: 13,6 GiB of RAM
> Graphics Processor: AMD RENOIR

Sorry, got a mixup. Examples above were for LSP rename functionality (which also does not work, probably a separate bug). Proper example for broken Organize imports follows:

main.go:

package main

func main() {
	fmt.Println("xxxx")
}



Trace logs from gopls:

[Trace - 18:32:58.402 PM] Sending request 'textDocument/codeAction - (3)'.
Params: {
        "context": {
            "diagnostics": [
            ]
        },
        "range": {
            "end": {
                "character": 0,
                "line": 4
            },
            "start": {
                "character": 0,
                "line": 3
            }
        },
        "textDocument": {
            "uri": "file:///tmp/goproj/main.go"
        }
    }


[Trace - 18:32:58.405 PM] Received response 'textDocument/codeAction - (3)' in 2ms.
Result: [{"title":"Organize Imports","kind":"source.organizeImports","edit":{"documentChanges":[{"textDocument":{"version":147,"uri":"file:///tmp/goproj/main.go"},"edits":[{"range":{"start":{"line":1,"character":0},"end":{"line":1,"character":0}},"newText":"\nimport \"fmt\"\n"}]}]}}]
Comment 4 Mark Nauwelaerts 2022-01-30 20:16:26 UTC
See the mentioned other bug #449332.  The server issues a type of response that the client did not claim to support (and as such also does not parse).
Comment 5 bokunocode 2022-01-31 05:07:26 UTC
there is a way to get auto import to work, by reloading the file from disk after running code action.
run code action autoimports >> dont save the file >> reload file from disk
Comment 6 Bug Janitor Service 2022-01-31 22:14:55 UTC
A possibly relevant merge request was started @ https://invent.kde.org/utilities/kate/-/merge_requests/580
Comment 7 Mark Nauwelaerts 2022-02-03 18:33:23 UTC
Git commit e1b89deaec075e9b155877ba5e549ed87baf1395 by Mark Nauwelaerts.
Committed on 31/01/2022 at 21:05.
Pushed by cullmann into branch 'master'.

lspclient: also support some alternative WorkspaceEdit replies
Related: bug 449332

M  +22   -9    addons/lspclient/lspclientpluginview.cpp

https://invent.kde.org/utilities/kate/commit/e1b89deaec075e9b155877ba5e549ed87baf1395