maxeth
maxeth

Reputation: 1515

Go Modules importing issue in VSCode ("cannot find package [...] in any of [...]")

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:

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.

Upvotes: 21

Views: 41428

Answers (5)

sev3ryn
sev3ryn

Reputation: 1087

If folder with go modules is placed within GOPATH and GO111MODULE=auto (default) gopls have issues finding modules - it looks for them in $GOPATH/pkg/mod folder and not traverses folders while looking for go.mod

To fix this you have to enforce using go modules everywhere

go env -w GO111MODULE=on

and reload vscode afterwards

Other option is to do in root of repository

go work init
go work use ./path-to-folder-with-go.mod

Upvotes: 1

McGrew
McGrew

Reputation: 952

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

Upvotes: 0

Erik Andersen
Erik Andersen

Reputation: 99

I realize this is an old question, but since go 1.18 and [email protected] 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

Upvotes: 9

Basil K Y
Basil K Y

Reputation: 560

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.

Upvotes: 12

Aruna Herath
Aruna Herath

Reputation: 6531

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

So initialize you go module outside GOPATH.

Upvotes: 6

Related Questions