18

I'm encountering what probably seems to be a Gopls language server issue: All my external package import statements are being marked as incorrect when using Go Modules with the Go extension in VSCode. Here's exactly what I did so far:

Inside my GOPATH/src/github.com/Kozie1337/projectname:

  • run go mod init github.com/Kozie1337/projectname
  • run go get -u github.com/gorilla/mux

Inside go.main:

package main

import (
    "log"
    "net/http"

    "github.com/gorilla/mux"  // This is being marked as wrong with the err. msg. down below
)

func main() {
  r := mux.NewRouter() // This actually works, even though the go linter says that mux isn't imported
  http.ListenAndServe(":9000", r)) // server starts too with mux routes
}

[...]

When hovering over the github.com/gorilla/mux import statement, I'm getting the error:

could not import github.com/gorilla/mux (cannot find package "github.com/gorilla/mux" in any of 
    C:\Program Files\Go\src\github.com\gorilla\mux (from $GOROOT)
    C\src\github.com\gorilla\mux (from $GOPATH)
    \Users\max\go\src\github.com\gorilla\mux (from $GOPATH))"

It looks like it's looking for the packages the way they were imported without Go modules from go\src even though they are stored in go\pkg\mod now. Is there some config file for VSCode/Gopls regarding this, or am I doing something wrong? I've never used Go/Go Modules before.

The import and code actually works despite the linting error, but the error disables all autocompletion so just ignoring it is not a viable solution.

I reinstalled to Go extension for VSCode and tried restarting the language server but that didn't change anything. The error message appears at all external package import statements, in every directory.

I'd be glad for some advice.

maxeth
  • 1,315
  • 1
  • 8
  • 17
  • Is your GOPATH properly set? Both on windows and on VSCode. – dubonzi May 30 '21 at 16:22
  • @dubonzi, from what I know VSCode uses the GOPATH from the go env file, and mine is set to C:\Users\max\go, the project directory is C:\Users\max\go\src\github.com\kozie1337\projectname so it should be right. – maxeth May 30 '21 at 16:59
  • You should be using modules rather than GOPATH. GOPATH builds are deprecated. – JimB Jun 01 '21 at 13:47

4 Answers4

6

The official go modules blog post specifically says "somewhere outside $GOPATH/src,".

So initialize you go module outside GOPATH.

Aruna Herath
  • 6,241
  • 1
  • 40
  • 59
  • 1
    Thank you, this actually worked. Too bad there is no error message from the Go extension when running ```go mod init``` inside GOPATH – maxeth May 30 '21 at 17:46
6

Same error can come if the Go project is in a subdirectory of the main project directory. To solve this, either open the Go project in workspace root or add the project to workspace in VScode. See this for more info.

Basil K Y
  • 490
  • 5
  • 8
4

I realize this is an old question, but since go 1.18 and gopls@0.8.0 they have introduced a native way to support this, using go workspaces.

In my case, I have my make files, docker-compose files etc in the root and all projects under ./services folder, so I did this:

root-dir> go work init
root-dir> go work use ./services/service1 ./services/service2

This adds a go.work file to the root-dir and I no longer has the error above.

Read more here: https://github.com/golang/tools/blob/master/gopls/doc/workspace.md

Erik Andersen
  • 49
  • 1
  • 1
0

The Go language server "needs a defined scope in which language features like references, rename, and implementation should operate".

There are two main options when working with multiple modules:

  1. "Starting with Go 1.18, Go workspaces are the preferred solution."
  2. In VS Code you can add the modules root folders to a VS Code Workspace.

For more info see: Setting up your workspace

McGrew
  • 812
  • 1
  • 9
  • 16