golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.43k stars 17.59k forks source link

x/tools/gopls: change links in hover for replaced modules #39532

Open DmitriyVTitov opened 4 years ago

DmitriyVTitov commented 4 years ago

Describe the bug

When I enable Go Language Server all my local Go modules become unavailable. image

All these modules are local to project folder and are available using replace:

replace (
    transflow.ru/pkg/api v0.0.0 => ../api
    transflow.ru/pkg/assign v0.0.0 => ../assign
    transflow.ru/pkg/cache v0.0.0 => ../cache
    transflow.ru/pkg/consts v0.0.0 => ../consts
)

But even struct Server which is defined at the very same package is unavailable as you may see at screenshot.

hyangah commented 4 years ago

@DmitriyVTitov Did you open the editor from the directory where the go.mod file with the replace statement exists? Can you please capture the language server log and share it with us for investigation? (cc @stamblerre)

Here is the instruction for log capturing. https://github.com/golang/tools/blob/master/gopls/doc/troubleshooting.md#vs-code

Thanks for the issue report.

DmitriyVTitov commented 4 years ago

Did you open the editor from the directory where the go.mod file with the replace statement exists?

I'm not sure that I understood your question correctly. I have several folders in my VSCode workspace (Go, Flutter, JS). One of them (transflow.ru) uses local modules, which are located at: transflow.ru\pkg\*. In the screenshot above I pictured transflow.ru\pkg\server module with part of it's go mod file. When I start VSCode with language server enabled the folowing log appears in the output (I removed lines, related to my other non-Go folders).

Here is the log:

[Info  - 18:17:43] 2020/06/11 18:17:43 Build info
----------
golang.org/x/tools/gopls 0.4.1
    golang.org/x/tools/gopls@v0.4.1 h1:0e3BPxGV4B3cd0zdMuccwW72SgmHp92lAjOyxX/ScAw=
    github.com/BurntSushi/toml@v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
    github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
    golang.org/x/mod@v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
    golang.org/x/sync@v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
    golang.org/x/tools@v0.0.0-20200513154647-78b527d18275 h1:e7nYe9s94RHunFJ7b+mmPxiQMOKMVSqYASToWb1EcHs=
    golang.org/x/xerrors@v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
    honnef.co/go/tools@v0.0.1-2020.1.3 h1:sXmLre5bzIR6ypkjXCDI3jHPssRhc8KD/Ome589sc3U=
    mvdan.cc/xurls/v2@v2.1.0 h1:KaMb5GLhlcSX+e+qhbRJODnUUBvlw01jt4yrjFIHAuA=

Go info
-------
go version go1.14.4 windows/amd64

[Info  - 18:17:44] 2020/06/11 18:17:44 go env for C:\Users\dtsp\YandexDisk\go\src\transflow.ru
(valid build configuration = true)
(build flags: [-modfile=C:\Users\dtsp\AppData\Local\Temp\go.transflow.ru.363357867.mod])
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\dtsp\AppData\Local\go-build
set GOENV=C:\Users\dtsp\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\dtsp\YandexDisk\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\GO
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\GO\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\dtsp\YandexDisk\go\src\transflow.ru\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\dtsp\AppData\Local\Temp\go-build350374462=/tmp/go-build -gno-record-gcc-switches

[Info  - 18:17:54] 2020/06/11 18:17:54 go/packages.Load
    snapshot=0
    directory=C:\Users\dtsp\YandexDisk\go\src\transflow.ru
    query=[./... builtin]
    packages=29

[Info  - 18:17:56] 2020/06/11 18:17:56 go/packages.Load
    snapshot=2
    directory=C:\Users\dtsp\YandexDisk\go\src\transflow.ru
    query=[file=C:\Users\dtsp\YandexDisk\go\src\transflow.ru\pkg\server\services.go]
    packages=1

[Info  - 18:17:56] 2020/06/11 18:17:56 go/packages.Load
    snapshot=2
    package_path="command-line-arguments"
    files=[C:\Users\dtsp\YandexDisk\go\src\transflow.ru\pkg\server\services.go]

[Error - 18:17:56] 2020/06/11 18:17:56 no dep handle: no metadata for transflow.ru/pkg/consts
    package="transflow.ru/pkg/consts"

[Error - 18:17:56] 2020/06/11 18:17:56 no dep handle: no metadata for transflow.ru/pkg/yandex
    package="transflow.ru/pkg/yandex"

[Error - 18:17:56] 2020/06/11 18:17:56 no dep handle: no metadata for transflow.ru/pkg/models
    package="transflow.ru/pkg/models"

    snapshot=2
    directory=C:\Users\dtsp\YandexDisk\go\src\transflow.ru
    query=[file=C:\Users\dtsp\YandexDisk\go\src\transflow.ru\pkg\server\server.go]
    packages=1

[Info  - 18:18:00] 2020/06/11 18:18:00 go/packages.Load
    snapshot=2
    package_path="command-line-arguments"
    files=[C:\Users\dtsp\YandexDisk\go\src\transflow.ru\pkg\server\server.go]

go.mod for go\src\transflow.ru\pkg\server\server.go:

module transflow.ru/pkg/server

go 1.13

replace (
    transflow.ru/pkg/api v0.0.0 => ../api
    transflow.ru/pkg/assign v0.0.0 => ../assign
    transflow.ru/pkg/cache v0.0.0 => ../cache
    transflow.ru/pkg/consts v0.0.0 => ../consts
    transflow.ru/pkg/geom v0.0.0 => ../geom
    transflow.ru/pkg/geozone v0.0.0 => ../geozone
    transflow.ru/pkg/helpdesk v0.0.0 => ../helpdesk
    transflow.ru/pkg/helpdesk/imap v0.0.0 => ../helpdesk/imap
    transflow.ru/pkg/insight v0.0.0 => ../insight
    transflow.ru/pkg/logger v0.0.0 => ../logger
    transflow.ru/pkg/logger/dblog v0.0.0 => ../logger/dblog
    transflow.ru/pkg/logger/memlog v0.0.0 => ../logger/memlog
    transflow.ru/pkg/mailer v0.0.0 => ../mailer
    transflow.ru/pkg/misc v0.0.0 => ../misc
    transflow.ru/pkg/models v0.0.0 => ../models
    transflow.ru/pkg/notifications v0.0.0 => ../notifications
    transflow.ru/pkg/pacmon v0.0.0 => ../pacmon
    transflow.ru/pkg/proto v0.0.0 => ../proto
    transflow.ru/pkg/realtime v0.0.0 => ../realtime
    transflow.ru/pkg/reports v0.0.0 => ../reports
    transflow.ru/pkg/reports/excel v0.0.0 => ../reports/excel
    transflow.ru/pkg/stats v0.0.0 => ../stats
    transflow.ru/pkg/storage v0.0.0 => ../storage
    transflow.ru/pkg/storage/inmemory v0.0.0 => ../storage/inmemory
    transflow.ru/pkg/storage/pgsql v0.0.0 => ../storage/pgsql
    transflow.ru/pkg/trips v0.0.0 => ../trips
    transflow.ru/pkg/violations v0.0.0 => ../violations
    transflow.ru/pkg/yandex v0.0.0 => ../yandex
)

require (
    github.com/gorilla/mux v1.7.3
    github.com/gorilla/websocket v1.4.1
    github.com/smartystreets/goconvey v1.6.4 // indirect
    transflow.ru/pkg/api v0.0.0
    transflow.ru/pkg/assign v0.0.0
    transflow.ru/pkg/cache v0.0.0
    transflow.ru/pkg/consts v0.0.0
    transflow.ru/pkg/helpdesk v0.0.0
    transflow.ru/pkg/insight v0.0.0
    transflow.ru/pkg/logger v0.0.0
    transflow.ru/pkg/logger/dblog v0.0.0
    transflow.ru/pkg/mailer v0.0.0
    transflow.ru/pkg/models v0.0.0
    transflow.ru/pkg/notifications v0.0.0
    transflow.ru/pkg/pacmon v0.0.0
    transflow.ru/pkg/proto v0.0.0
    transflow.ru/pkg/realtime v0.0.0
    transflow.ru/pkg/stats v0.0.0
    transflow.ru/pkg/storage v0.0.0
    transflow.ru/pkg/storage/inmemory v0.0.0
    transflow.ru/pkg/storage/pgsql v0.0.0
    transflow.ru/pkg/trips v0.0.0
    transflow.ru/pkg/violations v0.0.0
    transflow.ru/pkg/yandex v0.0.0
)
hyangah commented 4 years ago

@DmitriyVTitov Thanks for sharing more details. I think you were caught by https://github.com/golang/go/issues/32394.

The VS Code workspace is C:\Users\dtsp\YandexDisk\go\src and the go.mod file used is GOMOD=C:\Users\dtsp\YandexDisk\go\src\transflow.ru\go.mod, not the one in the transflow.ru\pkg\server folder.

Currently suggested workarounds is to open only the transflow.ru\pkg\server folder, or to manually add C:\Users\dtsp\YandexDisk\go\src\transflow.ru\pkg\server\ to the workspace (Workspaces: Add Folder to Workspace ....

DmitriyVTitov commented 4 years ago

Well I've deleted go.mod from the root of my project tree and VSCode no more highlights local packages as invalid. But Ctrl-Click (Go to code) is not working with local packages. But it works with standard library. Also I'ts strange that VSCode offers me to open link to, for example,

https://pkg.go.dev/transflow.ru/pkg/yandex

despite it's local package specified with replace directive in go.mod.

I've already opened issue https://github.com/microsoft/vscode-go/issues/3072 about Go to definition with Go 1.14. Maybe it's related. @stamblerre helped me a lot with that issuue but it didn't worked well for me anyways.

stamblerre commented 4 years ago

@DmitriyVTitov: Can you please run gopls -rpc.trace -v check path/to/file.go from your module root and share that output here? It definitely sounds related to the replace directive.

As for the pkg.go.dev links, those don't check for replace directives either, so that's a separate bug.

Transferred this issue to the Go issue tracker, as this is definitely a gopls issue.

gopherbot commented 4 years ago

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.

DmitriyVTitov commented 4 years ago

@DmitriyVTitov: Can you please run gopls -rpc.trace -v check path/to/file.go from your module root and share that output here? It definitely sounds related to the replace directive.

As for the pkg.go.dev links, those don't check for replace directives either, so that's a separate bug.

gopls.exe -rpc.trace -v .\server.go

got error:

flag provided but not defined: -rpc
The Go Language source tools.

Usage: gopls [flags] <command> [command-flags] [command-args]
stamblerre commented 4 years ago

Make sure you provide the check subcommand, so gopls.exe -rpc.trace -v check .\server.go.

DmitriyVTitov commented 4 years ago

Make sure you provide the check subcommand, so gopls.exe -rpc.trace -v check .\server.go.

PS C:\Users\dtsp\YandexDisk\go\src\transflow.ru\pkg\server> C:\Users\dtsp\YandexDisk\go\bin\gopls.exe -rpc.trace -v check .\server.go
flag provided but not defined: -rpc
The Go Language source tools.

Usage: gopls [flags] <command> [command-flags] [command-args]
stamblerre commented 4 years ago

This might be a PowerShell issue (https://stackoverflow.com/questions/26614969/how-can-i-pass-batch-arguments-with-dots-into-a-powershell-script). Looks like you may need to do something like gopls.exe --% -rpc.trace -v check path/to/file.go.

DmitriyVTitov commented 4 years ago

You were right:

PS C:\Users\dtsp\YandexDisk\go\src\transflow.ru\pkg\server> C:\Users\dtsp\YandexDisk\go\bin\gopls.exe --% -rpc.trace -v check .\server.go
2020/06/11 21:22:46 Info:2020/06/11 21:22:46 Build info
----------
golang.org/x/tools/gopls 0.4.1
    golang.org/x/tools/gopls@v0.4.1 h1:0e3BPxGV4B3cd0zdMuccwW72SgmHp92lAjOyxX/ScAw=
    github.com/BurntSushi/toml@v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
    github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
    golang.org/x/mod@v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ=
    golang.org/x/sync@v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY=
    golang.org/x/tools@v0.0.0-20200513154647-78b527d18275 h1:e7nYe9s94RHunFJ7b+mmPxiQMOKMVSqYASToWb1EcHs=
    golang.org/x/xerrors@v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
    honnef.co/go/tools@v0.0.1-2020.1.3 h1:sXmLre5bzIR6ypkjXCDI3jHPssRhc8KD/Ome589sc3U=
    mvdan.cc/xurls/v2@v2.1.0 h1:KaMb5GLhlcSX+e+qhbRJODnUUBvlw01jt4yrjFIHAuA=

Go info
-------
go version go1.14.4 windows/amd64

2020/06/11 21:22:47 Info:2020/06/11 21:22:47 go env for C:\Users\dtsp\YandexDisk\go\src\transflow.ru\pkg\server
(valid build configuration = true)
(build flags: [-modfile=C:\Users\dtsp\AppData\Local\Temp\go.server.010609871.mod])
set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\dtsp\AppData\Local\go-build
set GOENV=C:\Users\dtsp\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\dtsp\YandexDisk\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\GO
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\GO\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\dtsp\YandexDisk\go\src\transflow.ru\pkg\server\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\dtsp\AppData\Local\Temp\go-build743783070=/tmp/go-build -gno-record-gcc-switches

2020/06/11 21:22:48 Info:2020/06/11 21:22:48 go/packages.Load
        snapshot=0
        directory=C:\Users\dtsp\YandexDisk\go\src\transflow.ru\pkg\server
        query=[./... builtin]
        packages=4
stamblerre commented 4 years ago

Glad that worked! Looks like the gopls check command does not report any errors, unlike VS Code. Just to confirm, this is the file in which you see errors in VS Code?

DmitriyVTitov commented 4 years ago

Glad that worked! Looks like the gopls check command does not report any errors, unlike VS Code. Just to confirm, this is the file in which you see errors in VS Code?

I don't see ERRORS no more, but 'Go to definition' do not work. When I deleted root go.mod all red underscores disappeared. Still language server is not usable without Ctrl+Click. I mean in screenshot above I cannot Ctrl+Click even Reciever type in the function definition despite this type (Server) is in the same package (module) as server.go.

stamblerre commented 4 years ago

Deleting the go.mod is not a good solution, as that will put you back into GOPATH mode. If you can follow these steps to capture verbose logs, I can help you continue troubleshooting: https://github.com/golang/tools/blob/master/gopls/doc/troubleshooting.md#capturing-logs.

DmitriyVTitov commented 4 years ago

Deleting the go.mod is not a good solution, as that will put you back into GOPATH mode. If you can follow these steps to capture verbose logs, I can help you continue troubleshooting: https://github.com/golang/tools/blob/master/gopls/doc/troubleshooting.md#capturing-logs.

Sorry Rebecca, but I don't have time to switch extension on and off for now. I will continue to work without language server. Thanks for your help!

stamblerre commented 4 years ago

Not a problem. I'll leave this issue open as a reminder to add regression tests that use replace directives.

hyangah commented 4 years ago

@stamblerre the gopls check output has

go env for C:\Users\dtsp\YandexDisk\go\src\transflow.ru\pkg\server
(valid build configuration = true)

while the gopls server output has

go env for C:\Users\dtsp\YandexDisk\go\src\transflow.ru
(valid build configuration = true)

Doesn't it mean the workspace roots used in two cases are different?

stamblerre commented 4 years ago

Yes, it does seem like that's the case.

stamblerre commented 4 years ago

There hasn't been much activity on this issue, and we have a few CLs planned to improve the workflow with replaced modules. The one thing left to address is the hover links showing up for the original module.

Also I'ts strange that VSCode offers me to open link to, for example, https://pkg.go.dev/transflow.ru/pkg/yandex despite it's local package specified with replace directive in go.mod

stamblerre commented 3 years ago

After some discussion, we've concluded that the hover links may still be useful, even if the module has been replaced. We can change the links to point to the replaced module, if possible, however.