go: x/tools/cmd/gopls: import organization fails on certain packages

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

$ go version
go version go1.11.5 darwin/amd64

Does this issue reproduce with the latest release?

N/A, I think.

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/alex/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/alex/.go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/opt/go/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/opt/go/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/alex/Projects/Transparensee/enclosure-core/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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/qs/wpmg19r12_9_nz7pvvs2_82r0000gn/T/go-build353164800=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

What did you expect to see?

Saved file in VSCode that contains a reference to a struct in a different package, within the same module. I expected the import to be added to import.

What did you see instead?

Nothing was added. This usually works, but there appears to be some packages that just don’t work with import organization. This works fine with Go’s standard library packages as well as third-party packages.

The file is a skeleton main.go, where I added site := models.Site{}. The models package is a package inside the same module.

I don’t know much about gopls and how to report diagnostics, but here’s the log file with RPC logging enabled: gopls.log.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 58 (21 by maintainers)

Most upvoted comments

I’ve now experienced gopls failing many times in the same way, which I think has not yet been reported. I’ll open a file in a package and typically make some change that renders the file uncompilable. Rather than underlining my change with a red squiggle, VS Code underlines the package statement at the top of the file, and hover tells me that

file:///Users/peter/<whatever>/<file>.go is not part of a package

Every interaction with the file fails, even after I delete the change and return the file to the original state. The gopls log looks like this:

[Info  - 12:55:00 PM] Build info
----------
golang.org/x/tools/cmd/gopls
    golang.org/x/tools@v0.0.0-20190606124116-d0a3d012864b h1:mSUCVIwDx4hfXJfWsOPfdzEHxzb2Xjl6BQ8YgPnazQA=
    golang.org/x/sync@v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=

Go info
-------
go version go1.12.5 darwin/amd64

GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/peter/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/peter"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.12.5/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.12.5/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/peter/<whatever>/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 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/8s/kvkb2dzx37n057vk54y9b_ym0000gn/T/go-build192614899=/tmp/go-build -gno-record-gcc-switches -fno-common"

[Error - 12:55:02 PM] Request textDocument/hover failed.
  Message: no identifier found
  Code: 0 
[Error - 12:55:09 PM] unable to check package for file:///Users/peter/<whatever>/<file>.go: parse: no package found for /Users/peter/<whatever>/<file>.go
[Error - 12:55:09 PM] unable to check package for file:///Users/peter/<whatever>/<file>.go: parse: no package found for /Users/peter/<whatever>/<file>.go
[Error - 12:55:09 PM] Request textDocument/hover failed.
  Message: no AST for file:///Users/peter/<whatever>/<file>.go
  Code: 0 
[Error - 12:55:09 PM] unable to check package for file:///Users/peter/<whatever>/<file>.go: parse: no package found for /Users/peter/<whatever>/<file>.go
[Error - 12:55:09 PM] unable to check package for file:///Users/peter/<whatever>/<file>.go: parse: no package found for /Users/peter/<whatever>/<file>.go
[Error - 12:55:10 PM] unable to check package for file:///Users/peter/<whatever>/<file>.go: parse: no package found for /Users/peter/<whatever>/<file>.go

(repeat ad nauseum)

The only fix is quitting VS Code and starting it again.

For the record, my experience with gopls + VSCode on Mac mirrors @atombender; several times a day, I notice that auto-importing and autocompletion have stopped working. Restarting VSCode (and, therefore, gopls) typically solves the issue for awhile. I’d happily run it in some diagnostic mode and report back, if it helps.

edit: I’m on go version go1.12.4 darwin/amd64, in a project using modules, with a go.mod and go.sum, outside of GOPATH.

edit 2: It may also be worth noting the project vendors via go mod vendor.

I can confirm that as well, even in non-go mod repos.