67

I'm writing a Go project inside my GoPath, and i'm using the Redigo package for connecting to a Redis Server. The application runs fine, however in VSCode there is this annoying error on package import, which is preventing VSCode from giving intellisense suggestions

Could not import github.com/gomodule/redigo/redis (no package data for import path github.com/gomodule/redigo/redis)

This is my VSCode settings.json

{
    "editor.fontSize": 14,
    "editor.formatOnPaste": true,
    "editor.multiCursorModifier": "ctrlCmd",
    "editor.snippetSuggestions": "top",
    "extensions.ignoreRecommendations": false,
    "workbench.statusBar.visible": true,
    "workbench.iconTheme": "vscode-great-icons",
    "files.autoSave": "afterDelay",
    "go.useLanguageServer": true,
    "go.alternateTools": {
        "go-langserver": "bingo"
    },
    "go.toolsEnvVars": {
        "GO111MODULE": "on"
    },
    "go.languageServerExperimentalFeatures": {
        "autoComplete": true,
        "documentSymbols": true,
        "findReferences": true,
        "format": true,
        "goToDefinition": true,
        "goToTypeDefinition": true,
        "hover": true,
        "signatureHelp": true,
        "rename": true,
        "workspaceSymbols": true,
    },
    "go.lintTool": "golangci-lint",
    "go.lintFlags": [
        "--fast",
        "-E", "goimports",
        "-E", "gocritic",
        "-E", "gocyclo",
        "-E", "gosec",
        "-E", "maligned",
        "-E", "scopelint",
        "-E", "interfacer",
        "-E", "goconst",
        "-E", "unconvert",
        "-E", "unparam",
        "-E", "prealloc",
        "-E", "varcheck",
    ],
    "go.formatTool": "goimports",
    "editor.minimap.enabled": false,
    "breadcrumbs.enabled": false,
    "git.autofetch": true,
    "workbench.startupEditor": "newUntitledFile",
    "explorer.confirmDelete": false,
    "git.enableSmartCommit": true,
    "git.confirmSync": false,
    "window.zoomLevel": 0,
    "explorer.confirmDragAndDrop": false
}

I already have the GO111MODULE env var set to on, this is the output of go env

set GOARCH=amd64
set GOBIN=C:\Users\Francesco\Go\bin
set GOCACHE=C:\Users\Francesco\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\Francesco\Go
set GOPROXY=
set GORACE=
set GOROOT=c:\go
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\Francesco\Go\src\test\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\FRANCE~1\AppData\Local\Temp\go-build928398422=/tmp/go-build -gno-record-gcc-switches

What should I change to make this work?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Jimi
  • 1,605
  • 1
  • 16
  • 33

16 Answers16

172

This happens to me in a few specific situations. This is my troubleshooting process :

  1. Did you run go get github.com/gomodule/redigo/redis?

  2. Sometimes I have a similar issue when I open my editor in a root different than my project. (also check the updates at the end of the answer)

.  <- editor open here
| 
|_Folder
  | main.go
  | go.mod
  | go.sum
  1. Make sure your tools are up to date: run ctrl + shift + p, type Go and chose Install/Update tools.

  2. Try moving your project out of the GOPATH, and setting up go.mod for it.

  3. Restart the editor

Update for those with the issue "2":

Go 1.18 has a new feature called workspace! If you are opening your workspace in a root different than where your go mod files are, it is probably because you have multiple projects in the same folder. If that is the case, you can run:

go work init
go work use ./path-to-module  ./path-to-module2

What it looks like:

.  <- editor open here
| go.work
| app (folder)
  | go.mod
  | go.sum
  | main.go
| client (folder)
  | go.mod
  | go.sum
  | main.go
Auyer
  • 2,693
  • 3
  • 15
  • 32
  • 6
    Looks like updating has solved the issue. I did the go get command before, and the editor was open in the root project folder. I'll try to update the go tools more frequently i guess :P – Jimi Oct 24 '19 at 07:24
  • 13
    this issue has been bugging me for over a year, I've tried everything and your second point about opening the editor in root was the solution I least expected to work, but it did - many thanks – Hom Bahrani Nov 09 '20 at 09:29
  • 4
    second part of opening the editor one folder above did the trick for me as well – Saurabh Arora Feb 02 '21 at 10:43
  • Works for me restarting. Thanks. – Diego Virgüez Jul 28 '21 at 18:44
  • This is the definitive solution for developers using VSC with Go modules! Like [Hom Bahrani](https://stackoverflow.com/users/4130114/hom-bahrani), I was in a similar situation. Videos on Go and modules did not provide details on how to resolve this issue. Thanks! – datalifenyc Oct 26 '21 at 14:47
  • 2
    My issue was exactly the point 2: having opened VSCode on the root of a larger project with many go.mod files inside. Opening the editor workspace into one at the time solved it for me – Dylanbob211 Nov 19 '21 at 10:34
  • Same for me - I need vscode to have the root folder of the project open. Is there no way for us to be able to have multiple go.mod projects open at once? – Dois Nov 25 '21 at 09:45
  • Resolved ! I open VSCode as Open Folder at where main.go and go.mod are. IntelliSense can locate the place where a local module is e.i ...../mod/pkg/ – Pham Duc Toan Jul 24 '22 at 06:07
  • #3 worked for me - for whatever reason, updating the tools removed it. Many thanks! – teuber789 Aug 09 '22 at 17:02
  • for me, open folder at the project root does not work, i ended up created a folder wrap the current folder, and open the VSC from newly created one, works like charm.... – Albert Gao Sep 02 '22 at 21:53
  • The update worked like a charm – Rewanth Tammana Sep 17 '22 at 13:34
36

I had a same sort of issue with a different package (on mac),

  1. Update the go tools - ctrl + shift + p or cmd + shift + p and update/install go tools
  2. Restart VScode

Issue solved

Krishan Thisera
  • 521
  • 4
  • 5
  • 1
    Thank you. now name is "Go: install/Update Tools". – DagaReiN May 10 '22 at 20:32
  • Thanks, after update, it gives more clear error message, telling me I should put my project inside GOPATH, I can import local package without error in VScode – sbk201 Oct 16 '22 at 14:22
7

I encountered a similar problem, but the situation is slightly different. I encountered this problem in VSCode remote, and even the basic library like net/http cannot be imported. I found that it was because of some syntax errors in other files under the same package, such as undefined variables. When I solved these syntax errors and restarted VSCode, I found that the problem was solved.

zhushihao
  • 91
  • 1
  • 3
4

go mod init package_name followed by go mod tidy got things working for me. go mod tidy installed the packages and updated the mod file.

RadiantOne
  • 189
  • 5
4

Doing a full restart of vsCode fixed the issue for me.

Note cmd + shift + p -> Developer: Reload Window did NOT fix it, only a full close and re-opening of VS code

Ryan
  • 1,142
  • 8
  • 6
3

This worked for me:

  1. File/New Window
  2. File/Add Folder To Workspace ...
Andrew Betts
  • 338
  • 4
  • 6
2

I ran into this issue when I had two files in one package that had different package names. Eg: one file had package main while the other had package app.

Ensuring all files have the same package name will result in imports across the package to start working again.

Kyle Chadha
  • 3,741
  • 2
  • 33
  • 42
1

I've installed on Fedora 35 gcc-go and after I uninstalled it all worked fine.

sudo dnf remove gcc-go
0

I just recently ran into this same error, my solution was ensuring that my go version of my project's mod file was the same version as the installed version on my system.

Bob Ross
  • 61
  • 1
  • 6
0

I had the same problem it was from go mod init packegeName and a named my package 'time' and the editor give me err so I simply replace it with 'timee' i do not know whats happen actually but it's working fine

  • When posting answers to stackoverflow, it's important to keep them concise and brief -- stackoverflow is not a forum to post messages, it's a Q&A site. – Marco Dec 09 '21 at 08:40
0

if you are using Go 1.18+, here is a new feature for multi-module workspaces, once you setup the go.work file at you root project folder, the issues will no longer exist, you can check the following links for more details

  1. https://stackoverflow.com/a/59485684/7706503
  2. https://github.com/golang/tools/blob/master/gopls/doc/workspace.md
0

Ran into the same issue, reinstalled go with goenv, and then i ran some go mod whys to check if everything is in order and it was

John Huynh
  • 369
  • 3
  • 6
0

Setting up your workspace

gopls supports both Go module and GOPATH modes. However, it needs a defined scope in which language features like references, rename, and implementation should operate.

The following options are available for configuring this scope:

Module mode

One module

If you are working with a single module, you can open the module root (the directory containing the go.mod file), a subdirectory within the module, or a parent directory containing the module.

Note: If you open a parent directory containing a module, it must only contain that single module. Otherwise, you are working with multiple modules.

Multiple modules

Gopls has several alternatives for working on multiple modules simultaneously, described below. Starting with Go 1.18, Go workspaces are the preferred solution.

Go workspaces (Go 1.18+)

Starting with Go 1.18, the go command has native support for multi-module workspaces, via go.work files. These files are recognized by gopls starting with gopls@v0.8.0.

The easiest way to work on multiple modules in Go 1.18 and later is therefore to create a go.work file containing the modules you wish to work on, and set your workspace root to the directory containing the go.work file.

For example, suppose this repo is checked out into the $WORK/tools directory. We can work on both golang.org/x/tools and golang.org/x/tools/gopls simultaneously by creating a go.work file:

cd $WORK
go work init
go work use tools tools/gopls

...followed by opening the $WORK directory in our editor.


Source

Mark
  • 1,337
  • 23
  • 34
  • 1
    I noticed `go work use dirName` may not work if `dirName` fails silently if it doesn't have a `go.mod` file – Mark Oct 25 '22 at 20:12
0

My problem was I had the root folder (My Parent folder) which was the parent of my Go project folder open in VSCode

Ie,

My Parent folder
    My Go Project folder

Instead of opening the parent folder (My Parent folder) you could just open the project folder (My Go Project folder)

In my case, I needed access to files in both folders, so I added both parent & child folders to the VSCode workspace as per here and this got rid of the errors

Jase Whatson
  • 4,179
  • 5
  • 36
  • 45
0

I had the same symptoms. In my case, the root cause was the missing "CGO_ENABLED=1". Cause I had some "C" code in the package.

shapkin
  • 358
  • 3
  • 10
0

If this is a repo that you have pulled from Github, and you are faced with the errors of a package/s could not be imported and have a go.mod in the repo, I did the following to fix:

  1. Open terminal and navigate to the root of the project where your go.mod and go.sum are located and run:
go mod download

Which as the go help menu outlines that the command: downloads modules to local cache

  1. Inside VS Code open the command palette with whatever command that you have for your architecture. For mine command on mac I use control + shift + p and search for Go: Install/Update tools. Press enter and update your go tooling.

After that my main.go updated with packages that I was missing without explicitly running go install thingVSCodeSaidIWasMissing

Dom Hallan
  • 1,057
  • 1
  • 8
  • 16