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

4

21

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.

Invest answered 30/5, 2021 at 16:19 Comment(3)
Is your GOPATH properly set? Both on windows and on VSCode. – Breastsummer
@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. – Invest
You should be using modules rather than GOPATH. GOPATH builds are deprecated. – Reproduction
F
6

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

So initialize you go module outside GOPATH.

Fossette answered 30/5, 2021 at 17:13 Comment(1)
Thank you, this actually worked. Too bad there is no error message from the Go extension when running go mod init inside GOPATH πŸ˜„ – Invest
V
11

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.

Voodoo answered 18/9, 2022 at 9:19 Comment(2)
Thanks, this solved it for me. I just ran code . from within my subdirectory to open vscode in that scoep. Error is gone. – Occasionalism
when go project is in subdirectory of top level repo dir assure your go.mod file lives in repo root directory not subdirectory – Intersex
D
7

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

Diverticulitis answered 20/12, 2022 at 9:3 Comment(0)
F
6

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

So initialize you go module outside GOPATH.

Fossette answered 30/5, 2021 at 17:13 Comment(1)
Thank you, this actually worked. Too bad there is no error message from the Go extension when running go mod init inside GOPATH πŸ˜„ – Invest
W
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

Withe answered 23/8, 2023 at 4:17 Comment(0)

© 2022 - 2025 β€” McMap. All rights reserved.