go: x/tools/gopls: failure to import package

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

$ go version
go version go1.12.6 darwin/amd64

Does this issue reproduce with the latest release?

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/ldu020/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/ldu020/workspace/go:/Users/ldu020/workspace/github.com/mrdulin/golang"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/ldu020/workspace/github.com/mrdulin/golang/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/38/s8g_rsm13yxd26nwyqzdp2shd351xb/T/go-build050424632=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Here is the code:

import (
	"fmt"
	"go-clean-arch/domain/models"
	"go-clean-arch/domain/repositories"
)

I am using vscode and golang extension. When I hover to the import statement, the output of gopls give me this error.

What did you expect to see?

I try it using goland IDE, it works fine.

What did you see instead?

This error when hover to the import statement:

could not import go-clean-arch/domain/repositories (no parsed files for package go-clean-arch/domain/repositories)LSP

[Trace - 11:42:13 AM] Sending request 'textDocument/hover - (36)'.
Params: {"textDocument":{"uri":"file:///Users/ldu020/workspace/github.com/mrdulin/golang/src/go-clean-arch/domain/services/googleAccount.go"},"position":{"line":4,"character":23}}


[Error - 11:42:14 AM] send textDocument/hover#36 no import for "go-clean-arch/domain/models"


[Error - 11:42:14 AM] Request textDocument/hover failed.
  Message: no import for "go-clean-arch/domain/models"
  Code: 0 

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 14
  • Comments: 39 (14 by maintainers)

Most upvoted comments

After update some golang tools, I found this issue solved:

set GO111MODULE=ON go get -u -v github.com/mdempsky/gocode go get -u -v github.com/ramya-rao-a/go-outline go get -u -v golang.org/x/tools/cmd/gorename … I guess that gopls or vscode may need the golang tools to implement some lsp functions. If these tools are not updated to be located at $GOPATH/pkg/, gopls may fail.

28.497661ms for GOROOT= GOPATH=/Users/alan/work/goprojects GO111MODULE=auto PWD=/Users/alan/ go "list" "-m" "-json" "all", stderr: <<go list -m: not using modules

So I think I had a similar problem. What I noticed in the above log line was that the PWD was set to the root of the workspace directory I’d opened, rather than the root of the go project I was working on. I cleared my workspace and opened just the root of the project I was working on and all errors resolved.

Hope that helps.

It looks like gopls was unable to find any files for this import. What happens if you run go list -e -compiled -json /path/to/package where /path/to/package is the absolute path to the package you are working in?

Also, can you try to repro with the latest version of gopls on master (go get -u golang.org/x/tools/gopls@master)? We have recently made some changes that may have fixed this.